View Javadoc

1   package com.wikihouse.wildcats0201.jdbcchart.reflectionutil;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.Method;
5   import java.util.Arrays;
6   import java.util.Iterator;
7   import java.util.LinkedList;
8   import java.util.List;
9   
10  import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.ClassNotFoundExceptionWrapper;
11  import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.IllegalAccessExceptionWrapper;
12  import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.InstantiationExceptionWrapper;
13  import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.NoSuchMethodExceptionWrapper;
14  import com.wikihouse.wildcats0201.jdbcchart.reflectionutil.exception.SecurityExceptionWrapper;
15  
16  /***
17   * <B>ClassWrapper </B>. Classをラッピングするクラスです。
18   * 
19   * @see java.lang.Class
20   * @author $Author: wildcats $
21   * @version $Revision: 5 $
22   */
23  public class ClassWrapper {
24  
25      private Class clazz;
26  
27      private ClassWrapper() {
28      }
29  
30      public ClassWrapper(String className) {
31          this(getClass(className));
32      }
33  
34      private static Class getClass(String className) {
35          Class clazz = null;
36          try {
37              clazz = Class.forName(className);
38          } catch (java.lang.ClassNotFoundException e) {
39              throw new ClassNotFoundExceptionWrapper(e);
40          }
41          return clazz;
42      }
43  
44      public ClassWrapper(Class clazz) {
45          this.clazz = clazz;
46      }
47  
48      public Class get() {
49          return this.clazz;
50      }
51  
52      public Object newInstance() {
53          Object obj = null;
54          try {
55              obj = clazz.newInstance();
56          } catch (java.lang.InstantiationException e) {
57              throw new InstantiationExceptionWrapper(e);
58          } catch (java.lang.IllegalAccessException e) {
59              throw new IllegalAccessExceptionWrapper(e);
60          }
61          return obj;
62      }
63  
64      private ConstructorWrapper getConstructorWrapper(Constructor constructor) {
65          return new ConstructorWrapper(constructor);
66      }
67  
68      public ConstructorWrapper getConstructor(Class clazz) {
69          return getConstructor(new Class[] { clazz });
70      }
71  
72      public ConstructorWrapper getConstructor(Class[] classes) {
73          Constructor constructor = null;
74          try {
75              constructor = this.clazz.getConstructor(classes);
76          } catch (java.lang.SecurityException e) {
77              throw getSecurityException(e);
78          } catch (java.lang.NoSuchMethodException e) {
79              throw getNoSucheMethodException(e);
80          }
81          return getConstructorWrapper(constructor);
82      }
83  
84      /***
85       * @see java.lang.Object#toString()
86       */
87      public String toString() {
88          return clazz.getName();
89      }
90  
91      public Method[] getMethods() {
92          return this.clazz.getMethods();
93      }
94  
95      public Method[] getMethods(String methodName) {
96          List list = new LinkedList();
97          for (Iterator it = Arrays.asList(this.getMethods()).iterator(); it
98                  .hasNext();) {
99              Method method = (Method) it.next();
100             if (method.getName().equals(methodName)) {
101                 list.add(method);
102             }
103         }
104         return (Method[]) list.toArray(new Method[0]);
105     }
106 
107     public int getMethodCount(String methodName) {
108         return getMethods(methodName).length;
109     }
110 
111     public Method getMethod(String methodName, Class[] clazz) {
112         Method method = null;
113         try {
114             method = this.clazz.getMethod(methodName, clazz);
115         } catch (java.lang.SecurityException e) {
116             throw getSecurityException(e);
117         } catch (java.lang.NoSuchMethodException e) {
118             throw getNoSucheMethodException(e);
119         }
120         return method;
121     }
122 
123     private SecurityExceptionWrapper getSecurityException(
124             java.lang.SecurityException exception) {
125         return new SecurityExceptionWrapper(exception);
126     }
127 
128     private NoSuchMethodExceptionWrapper getNoSucheMethodException(
129             java.lang.NoSuchMethodException exception) {
130         return new NoSuchMethodExceptionWrapper(exception);
131     }
132 }