1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.ep.db4o.factories;
18
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Proxy;
22
23 public class BasicFactories implements Factories {
24
25
26
27
28 @SuppressWarnings("unchecked")
29 public <T> T createFactory(Class<T> factory) {
30 Method[] faMethods =factory.getDeclaredMethods();
31 if (faMethods.length == 0)
32 throw new InvalidFactoryException("No method found in factory");
33 Method main = faMethods[0];
34 Class<?> targetType = main.getReturnType();
35 if (faMethods.length > targetType.getDeclaredConstructors().length)
36 throw new InvalidFactoryException(faMethods.length + " found in factory but " + targetType.getDeclaredConstructors().length + " should be declared");
37 if (targetType == null)
38 throw new InvalidFactoryException("The return type of '" + main.getName() + "' cannot be void");
39 Class<?> realType = getRealClass(targetType);
40 InvocationHandler invHandler = new FactoryInvocationHandler(factory,targetType,realType, this);
41 return (T) Proxy.newProxyInstance(factory.getClassLoader(), new Class[]{factory},invHandler);
42 }
43
44 protected Class<?> getRealClass(Class<?> targetType) {
45 return targetType;
46 }
47
48 public void onNewInstance(Object inst) {
49
50
51 }
52
53 @SuppressWarnings("unchecked")
54 public <T> T createInstance(Class<T> clazz) {
55 T instance;
56 try {
57 instance = (T) getRealClass(clazz).newInstance();
58 } catch (InstantiationException e) {
59 throw new InvalidFactoryException("No empty constructor found in " + clazz.getName(),e);
60 } catch (IllegalAccessException e) {
61 throw new InvalidFactoryException(e);
62 }
63 onNewInstance(instance);
64 return instance;
65 }
66
67 }