1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.tools;
18
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23
24 import org.apache.commons.discovery.DiscoveryException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31 public class ClassUtils {
32
33 private static Log log = LogFactory.getLog(ClassUtils.class);
34
35
36
37
38
39
40
41 @Deprecated
42 public static void setLog(Log _log) {
43 log = _log;
44 }
45
46
47
48
49
50
51
52
53
54
55
56 public static String getPackageName(Class<?> clazz) {
57 Package clazzPackage = clazz.getPackage();
58 String packageName;
59 if (clazzPackage != null) {
60 packageName = clazzPackage.getName();
61 } else {
62 String clazzName = clazz.getName();
63 packageName = clazzName.substring(0, clazzName.lastIndexOf('.'));
64 }
65 return packageName;
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public static Method findPublicStaticMethod(Class<?> clazz,
79 Class<?> returnType,
80 String methodName,
81 Class<?>[] paramTypes) {
82 boolean problem = false;
83 Method method = null;
84
85
86 try {
87 method = clazz.getDeclaredMethod(methodName, paramTypes);
88 } catch(NoSuchMethodException e) {
89 problem = true;
90 log.debug("Class " + clazz.getName() + ": missing method '" + methodName + "(...)", e);
91 }
92
93
94 if (!problem &&
95 !(Modifier.isPublic(method.getModifiers()) &&
96 Modifier.isStatic(method.getModifiers()) &&
97 method.getReturnType() == returnType)) {
98 if (log.isDebugEnabled()) {
99 if (!Modifier.isPublic(method.getModifiers())) {
100 log.debug(methodName + "() is not public");
101 }
102 if (!Modifier.isStatic(method.getModifiers())) {
103 log.debug(methodName + "() is not static");
104 }
105 if (method.getReturnType() != returnType) {
106 log.debug("Method returns: "
107 + method.getReturnType().getName()
108 + "@@"
109 + method.getReturnType().getClassLoader());
110 log.debug("Should return: "
111 + returnType.getName()
112 + "@@"
113 + returnType.getClassLoader());
114 }
115 }
116 problem = true;
117 method = null;
118 }
119
120 return method;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public static <T> T newInstance(Class<T> impl, Class<?> paramClasses[], Object params[]) throws DiscoveryException,
148 InstantiationException,
149 IllegalAccessException,
150 NoSuchMethodException,
151 InvocationTargetException {
152 if (paramClasses == null || params == null) {
153 return impl.newInstance();
154 }
155
156 Constructor<T> constructor = impl.getConstructor(paramClasses);
157 return constructor.newInstance(params);
158 }
159
160
161
162
163
164
165
166
167
168 public static void verifyAncestory(Class<?> spi, Class<?> impl) throws DiscoveryException {
169 if (spi == null) {
170 throw new DiscoveryException("No interface defined!");
171 }
172
173 if (impl == null) {
174 throw new DiscoveryException("No implementation defined for " + spi.getName());
175 }
176
177 if (!spi.isAssignableFrom(impl)) {
178 throw new DiscoveryException("Class "
179 + impl.getName()
180 + " does not implement "
181 + spi.getName());
182 }
183 }
184
185 }