View Javadoc

1   package org.astrogrid.common.creator;
2   
3   import java.lang.reflect.Field;
4   import java.lang.reflect.InvocationTargetException;
5   import java.lang.reflect.Method;
6   import java.lang.reflect.Modifier;
7   
8   import org.apache.log4j.Category;
9   
10  /***
11   * @author peter.shillan <mailto:gps@roe.ac.uk />
12   */
13  public class InstanceCreator {
14    private Category logger = Category.getInstance(getClass());
15    
16    // Constants.
17    private static final String DEFAULT_METHOD = "getInstance";
18  
19    public Object getInstance(String className) throws InstanceCreatorException {
20      return getInstance(className, null, null);
21    }
22  
23    public Object getInstance(String className, Object[] parameters) throws InstanceCreatorException {
24      return getInstance(className, null, parameters);
25    }
26    
27    public Object getInstance(String className, String methodName, Object[] parameters) throws InstanceCreatorException {
28      Object result = null;
29      
30      if(methodName == null || methodName.length() < 1) {
31        methodName = DEFAULT_METHOD;
32      }
33      
34      try {
35        Class clazz = Class.forName(className);
36        
37        Class[] classes = getParameterClasses(parameters);
38        Method method = null;
39        
40        try {
41          method = clazz.getMethod(methodName, classes);
42        }
43        catch(NoSuchMethodException e) {
44          classes = convertPrimitives(classes);
45          method = clazz.getMethod(methodName, classes);
46        }
47        
48        int modifiers = method.getModifiers();
49        if(Modifier.isStatic(modifiers)) {
50          try {
51            result = method.invoke(null, parameters);
52          }
53          catch(IllegalAccessException e) {
54            throw new InstanceCreatorException(e);
55          }
56          catch(InvocationTargetException e) {
57            throw new InstanceCreatorException(e);
58          }
59        }
60      }
61      catch(ClassNotFoundException e) {
62        throw new InstanceCreatorException(e);
63      }
64      catch(NoSuchMethodException e) {
65        throw new InstanceCreatorException(e);
66      }
67      
68      return result;
69    }
70  
71    private Class[] getParameterClasses(Object[] parameters) {
72      Class[] result = null;
73      
74      if(parameters != null && parameters.length > 0) {
75        result = new Class[parameters.length];
76        for(int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
77          result[paramIndex] = parameters[paramIndex].getClass();
78        }
79      }    
80      
81      return result;
82    }
83  
84    private Class[] convertPrimitives(Class[] classes) {
85      Class[] result = null;
86      
87      if(classes != null && classes.length > 0) {
88        result = new Class[classes.length];
89        for(int classIndex = 0; classIndex < classes.length; classIndex++) {
90          result[classIndex] = convertPrimitive(classes[classIndex]);
91        }
92      }    
93      
94      return result;
95    }
96    
97    private Class convertPrimitive(Class clazz) {
98      Class result = clazz;
99      
100     logger.debug("class: " + clazz.getName());
101     
102     try {
103       Field field = clazz.getField("TYPE");
104       int modifiers = field.getModifiers();
105       if(Modifier.isStatic(modifiers)) {
106         Class fieldClazz = field.getType();
107 
108         if(fieldClazz.isAssignableFrom(Class.class)){
109           result = (Class) field.get(null);
110         }
111       }
112     }
113     catch(NoSuchFieldException e) {
114       logger.debug("no TYPE for class: " + result.getName());
115     }
116     catch(IllegalAccessException e) {
117       logger.debug("illegal access: " + result.getName());
118     }
119     
120     logger.debug("result: " + result.getName());
121     logger.debug("Integer.TYPE: " + Integer.TYPE.getName());
122 
123     return result;
124   }
125 
126 }