001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.util;
022
023 import java.beans.Introspector;
024 import java.beans.PropertyDescriptor;
025 import java.lang.annotation.Annotation;
026 import java.lang.reflect.AnnotatedElement;
027 import java.lang.reflect.Array;
028 import java.lang.reflect.Constructor;
029 import java.lang.reflect.Field;
030 import java.lang.reflect.GenericArrayType;
031 import java.lang.reflect.InvocationTargetException;
032 import java.lang.reflect.Member;
033 import java.lang.reflect.Method;
034 import java.lang.reflect.Modifier;
035 import java.lang.reflect.ParameterizedType;
036 import java.lang.reflect.Type;
037 import java.lang.reflect.TypeVariable;
038 import java.lang.reflect.WildcardType;
039 import java.net.MalformedURLException;
040 import java.net.URL;
041 import java.util.Collections;
042 import java.util.List;
043 import java.util.Map;
044 import java.util.Set;
045
046 /**
047 * @author Franck WOLFF
048 */
049 public abstract class ClassUtil {
050
051 public static Object newInstance(String type)
052 throws ClassNotFoundException, InstantiationException, IllegalAccessException {
053 return forName(type).newInstance();
054 }
055
056 public static <T> T newInstance(String type, Class<T> cast)
057 throws ClassNotFoundException, InstantiationException, IllegalAccessException {
058 return forName(type, cast).newInstance();
059 }
060
061 public static Object newInstance(String type, Class<?>[] argsClass, Object[] argsValues)
062 throws ClassNotFoundException, InstantiationException, IllegalAccessException {
063 return newInstance(forName(type), argsClass, argsValues);
064 }
065
066 @SuppressWarnings("unchecked")
067 public static <T> T newInstance(Class<?> type, Class<T> cast)
068 throws InstantiationException, IllegalAccessException {
069 return (T)type.newInstance();
070 }
071
072 public static <T> T newInstance(Class<T> type, Class<?>[] argsClass, Object[] argsValues)
073 throws InstantiationException, IllegalAccessException {
074 T instance = null;
075 try {
076 Constructor<T> constructorDef = type.getConstructor(argsClass);
077 instance = constructorDef.newInstance(argsValues);
078 } catch (SecurityException e) {
079 throw new InstantiationException(e.getMessage());
080 } catch (NoSuchMethodException e) {
081 throw new InstantiationException(e.getMessage());
082 } catch (IllegalArgumentException e) {
083 throw new InstantiationException(e.getMessage());
084 } catch (InvocationTargetException e) {
085 throw new InstantiationException(e.getMessage());
086 }
087 return instance;
088 }
089
090 public static Class<?> forName(String type) throws ClassNotFoundException {
091 try {
092 return ClassUtil.class.getClassLoader().loadClass(type);
093 }
094 catch (ClassNotFoundException e) {
095 return Thread.currentThread().getContextClassLoader().loadClass(type);
096 }
097 }
098
099 @SuppressWarnings("unchecked")
100 public static <T> Class<T> forName(String type, Class<T> cast) throws ClassNotFoundException {
101 try {
102 return (Class<T>)ClassUtil.class.getClassLoader().loadClass(type);
103 }
104 catch (ClassNotFoundException e) {
105 return (Class<T>)Thread.currentThread().getContextClassLoader().loadClass(type);
106 }
107 }
108
109 public static Constructor<?> getConstructor(String type, Class<?>[] paramTypes)
110 throws ClassNotFoundException, NoSuchMethodException {
111 return getConstructor(forName(type), paramTypes);
112 }
113
114 public static <T> Constructor<T> getConstructor(Class<T> type, Class<?>[] paramTypes)
115 throws NoSuchMethodException {
116 return type.getConstructor(paramTypes);
117 }
118
119 public static <T> List<T> emptyList(Class<T> type) {
120 return Collections.emptyList();
121 }
122
123 public static <T> Set<T> emptySet(Class<T> type) {
124 return Collections.emptySet();
125 }
126
127 public static <T, U> Map<T, U> emptyMap(Class<T> keyType, Class<U> valueType) {
128 return Collections.emptyMap();
129 }
130
131 public static boolean isPrimitive(Type type) {
132 return type instanceof Class<?> && ((Class<?>)type).isPrimitive();
133 }
134
135 public static Class<?> classOfType(Type type) {
136 if (type instanceof Class<?>)
137 return (Class<?>)type;
138 if (type instanceof ParameterizedType)
139 return (Class<?>)((ParameterizedType)type).getRawType();
140 if (type instanceof WildcardType) {
141 // Forget lower bounds and only deal with first upper bound...
142 Type[] ubs = ((WildcardType)type).getUpperBounds();
143 if (ubs.length > 0)
144 return classOfType(ubs[0]);
145 }
146 if (type instanceof GenericArrayType) {
147 Class<?> ct = classOfType(((GenericArrayType)type).getGenericComponentType());
148 return (ct != null ? Array.newInstance(ct, 0).getClass() : Object[].class);
149 }
150 if (type instanceof TypeVariable<?>) {
151 // Only deal with first (upper) bound...
152 Type[] ubs = ((TypeVariable<?>)type).getBounds();
153 if (ubs.length > 0)
154 return classOfType(ubs[0]);
155 }
156 // Should never append...
157 return Object.class;
158 }
159
160 public static Type getBoundType(TypeVariable<?> typeVariable) {
161 Type[] ubs = typeVariable.getBounds();
162 if (ubs.length > 0)
163 return ubs[0];
164
165 // should never happen...
166 if (typeVariable.getGenericDeclaration() instanceof Type)
167 return (Type)typeVariable.getGenericDeclaration();
168 return typeVariable;
169 }
170
171 public static String getPackageName(Class<?> clazz) {
172 return clazz.getPackage() != null ? clazz.getPackage().getName() : "";
173 }
174
175 public static PropertyDescriptor[] getProperties(Class<?> clazz) {
176 try {
177 PropertyDescriptor[] properties = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
178 Field[] fields = clazz.getDeclaredFields();
179 for (Field field : fields) {
180 if (Boolean.class.equals(field.getType())) {
181 boolean found = false;
182 for (PropertyDescriptor property : properties) {
183 if (property.getName().equals(field.getName())) {
184 found = true;
185 if (property.getReadMethod() == null) {
186 try {
187 Method readMethod = clazz.getDeclaredMethod(getIsMethodName(field.getName()));
188 if (Modifier.isPublic(readMethod.getModifiers()) && !Modifier.isStatic(readMethod.getModifiers()))
189 property.setReadMethod(readMethod);
190 }
191 catch (NoSuchMethodException e) {
192 }
193 }
194 break;
195 }
196 }
197 if (!found) {
198 try {
199 Method readMethod = clazz.getDeclaredMethod(getIsMethodName(field.getName()));
200 if (Modifier.isPublic(readMethod.getModifiers()) && !Modifier.isStatic(readMethod.getModifiers())) {
201 PropertyDescriptor[] propertiesTmp = new PropertyDescriptor[properties.length + 1];
202 System.arraycopy(properties, 0, propertiesTmp, 0, properties.length);
203 propertiesTmp[properties.length] = new PropertyDescriptor(field.getName(), readMethod, null);
204 properties = propertiesTmp;
205 }
206 }
207 catch (NoSuchMethodException e) {
208 }
209 }
210 }
211 }
212 return properties;
213 } catch (Exception e) {
214 throw new RuntimeException("Could not introspect properties of class: " + clazz, e);
215 }
216 }
217
218 private static String getIsMethodName(String name) {
219 return "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
220 }
221
222 public static ClassLoader getClassLoader(Class<?> clazz) {
223 return (clazz.getClassLoader() != null ? clazz.getClassLoader() : ClassLoader.getSystemClassLoader());
224 }
225
226 public static URL findResource(Class<?> clazz) {
227 while (clazz.isArray())
228 clazz = clazz.getComponentType();
229 if (clazz.isPrimitive())
230 return null;
231 URL url = getClassLoader(clazz).getResource(toResourceName(clazz));
232 String path = url.toString();
233 if (path.indexOf(' ') != -1) {
234 try {
235 url = new URL(path.replace(" ", "%20"));
236 } catch (MalformedURLException e) {
237 // should never happen...
238 }
239 }
240 return url;
241 }
242
243 public static String toResourceName(Class<?> clazz) {
244 return clazz.getName().replace('.', '/').concat(".class");
245 }
246
247 public static String getMethodSignature(Method method) {
248 StringBuilder sb = new StringBuilder();
249 sb.append(method.getName()).append('(');
250 Class<?>[] params = method.getParameterTypes();
251 for (int i = 0; i < params.length; i++) {
252 if (i > 0)
253 sb.append(',');
254 sb.append(getTypeSignature(params[i]));
255 }
256 sb.append(')');
257 return sb.toString();
258 }
259
260 public static String getTypeSignature(Class<?> type) {
261 if (type.isArray()) {
262 try {
263 int dimensions = 1;
264 Class<?> clazz = type.getComponentType();
265 while (clazz.isArray()) {
266 dimensions++;
267 clazz = clazz.getComponentType();
268 }
269
270 StringBuffer sb = new StringBuffer(clazz.getName());
271 while (dimensions-- > 0)
272 sb.append("[]");
273 return sb.toString();
274 } catch (Throwable e) {
275 // fallback...
276 }
277 }
278 return type.getName();
279 }
280
281 public static Method getMethod(Class<?> clazz, String signature) throws NoSuchMethodException {
282 signature = StringUtil.removeSpaces(signature);
283
284 if (!signature.endsWith(")"))
285 signature += "()";
286
287 for (Method method : clazz.getMethods()) {
288 if (signature.equals(getMethodSignature(method)))
289 return method;
290 }
291
292 throw new NoSuchMethodException("Could not find method: " + signature + " in class: " + clazz);
293 }
294
295 public static boolean isAnnotationPresent(AnnotatedElement elmt, Class<? extends Annotation> annotationClass) {
296 return getAnnotation(elmt, annotationClass) != null;
297 }
298
299 public static <T extends Annotation> DeclaredAnnotation<T> getAnnotation(AnnotatedElement elmt, Class<T> annotationClass) {
300 T annotation = elmt.getAnnotation(annotationClass);
301
302 if (annotation != null) {
303 Class<?> declaringClass = (elmt instanceof Member ? ((Member)elmt).getDeclaringClass() : (Class<?>)elmt);
304 return new DeclaredAnnotation<T>(annotation, elmt, declaringClass);
305 }
306
307 if (elmt instanceof Field)
308 return null;
309
310 if (elmt instanceof Method) {
311 Method m = (Method)elmt;
312 return getMethodAnnotation(m.getDeclaringClass(), m.getName(), m.getParameterTypes(), annotationClass);
313 }
314
315 if (elmt instanceof Constructor) {
316 Constructor<?> c = (Constructor<?>)elmt;
317 return getConstructorAnnotation(c.getDeclaringClass(), annotationClass);
318 }
319
320 if (elmt instanceof Class) {
321 Class<?> c = (Class<?>)elmt;
322 return getClassAnnotation(c.getDeclaringClass(), annotationClass);
323 }
324
325 throw new RuntimeException("Unsupported annotated element: " + elmt);
326 }
327
328 public static <T extends Annotation> DeclaredAnnotation<T> getMethodAnnotation(Class<?> clazz, String name, Class<?>[] parameterTypes, Class<T> annotationClass) {
329 DeclaredAnnotation<T> declaredAnnotation = null;
330
331 try {
332 Method method = clazz.getDeclaredMethod(name, parameterTypes);
333 T annotation = clazz.getDeclaredMethod(name, parameterTypes).getAnnotation(annotationClass);
334 if (annotation != null)
335 declaredAnnotation = new DeclaredAnnotation<T>(annotation, method, clazz);
336 }
337 catch (NoSuchMethodException e) {
338 // fallback...
339 }
340
341 if (declaredAnnotation == null && clazz.getSuperclass() != null)
342 declaredAnnotation = getMethodAnnotation(clazz.getSuperclass(), name, parameterTypes, annotationClass);
343
344 if (declaredAnnotation == null) {
345 for (Class<?> interfaze : clazz.getInterfaces()) {
346 declaredAnnotation = getMethodAnnotation(interfaze, name, parameterTypes, annotationClass);
347 if (declaredAnnotation != null)
348 break;
349 }
350 }
351
352 return declaredAnnotation;
353 }
354
355 public static <T extends Annotation> DeclaredAnnotation<T> getConstructorAnnotation(Class<?> clazz, Class<T> annotationClass) {
356 DeclaredAnnotation<T> declaredAnnotation = null;
357
358 for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
359 T annotation = constructor.getAnnotation(annotationClass);
360 if (annotation != null) {
361 declaredAnnotation = new DeclaredAnnotation<T>(annotation, constructor, clazz);
362 break;
363 }
364 }
365
366 if (declaredAnnotation == null && clazz.getSuperclass() != null)
367 declaredAnnotation = getConstructorAnnotation(clazz.getSuperclass(), annotationClass);
368
369 return declaredAnnotation;
370 }
371
372 public static <T extends Annotation> DeclaredAnnotation<T> getClassAnnotation(Class<?> clazz, Class<T> annotationClass) {
373 DeclaredAnnotation<T> declaredAnnotation = null;
374
375 T annotation = clazz.getAnnotation(annotationClass);
376 if (annotation != null)
377 declaredAnnotation = new DeclaredAnnotation<T>(annotation, clazz, clazz);
378 else {
379 if (clazz.getSuperclass() != null)
380 declaredAnnotation = getClassAnnotation(clazz.getSuperclass(), annotationClass);
381
382 if (declaredAnnotation == null) {
383 for (Class<?> interfaze : clazz.getInterfaces()) {
384 declaredAnnotation = getClassAnnotation(interfaze, annotationClass);
385 if (declaredAnnotation != null)
386 break;
387 }
388 }
389 }
390
391 return declaredAnnotation;
392 }
393
394 public static class DeclaredAnnotation<T extends Annotation> {
395
396 public final T annotation;
397 public final AnnotatedElement annotatedElement;
398 public final Class<?> declaringClass;
399
400 public DeclaredAnnotation(T annotation, AnnotatedElement annotatedElement, Class<?> declaringClass) {
401 this.annotation = annotation;
402 this.annotatedElement = annotatedElement;
403 this.declaringClass = declaringClass;
404 }
405
406 @Override
407 public String toString() {
408 return getClass().getName() + "{annotation=" + annotation + ", annotatedElement=" + annotatedElement + ", declaringClass=" + declaringClass + "}";
409 }
410 }
411 }