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 happen...
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 = 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 String removeSpaces(String s) {
296 if (s == null)
297 return null;
298 String[] tokens = s.split("\\s", -1);
299 if (tokens.length == 0)
300 return "";
301 if (tokens.length == 1)
302 return tokens[0];
303 StringBuilder sb = new StringBuilder();
304 for (String token : tokens)
305 sb.append(token);
306 return sb.toString();
307 }
308
309 public static boolean isAnnotationPresent(AnnotatedElement elmt, Class<? extends Annotation> annotationClass) {
310 return getAnnotation(elmt, annotationClass) != null;
311 }
312
313 public static <T extends Annotation> DeclaredAnnotation<T> getAnnotation(AnnotatedElement elmt, Class<T> annotationClass) {
314 T annotation = elmt.getAnnotation(annotationClass);
315
316 if (annotation != null) {
317 Class<?> declaringClass = (elmt instanceof Member ? ((Member)elmt).getDeclaringClass() : (Class<?>)elmt);
318 return new DeclaredAnnotation<T>(annotation, elmt, declaringClass);
319 }
320
321 if (elmt instanceof Field)
322 return null;
323
324 if (elmt instanceof Method) {
325 Method m = (Method)elmt;
326 return getMethodAnnotation(m.getDeclaringClass(), m.getName(), m.getParameterTypes(), annotationClass);
327 }
328
329 if (elmt instanceof Constructor) {
330 Constructor<?> c = (Constructor<?>)elmt;
331 return getConstructorAnnotation(c.getDeclaringClass(), annotationClass);
332 }
333
334 if (elmt instanceof Class) {
335 Class<?> c = (Class<?>)elmt;
336 return getClassAnnotation(c.getDeclaringClass(), annotationClass);
337 }
338
339 throw new RuntimeException("Unsupported annotated element: " + elmt);
340 }
341
342 public static <T extends Annotation> DeclaredAnnotation<T> getMethodAnnotation(Class<?> clazz, String name, Class<?>[] parameterTypes, Class<T> annotationClass) {
343 DeclaredAnnotation<T> declaredAnnotation = null;
344
345 try {
346 Method method = clazz.getDeclaredMethod(name, parameterTypes);
347 T annotation = clazz.getDeclaredMethod(name, parameterTypes).getAnnotation(annotationClass);
348 if (annotation != null)
349 declaredAnnotation = new DeclaredAnnotation<T>(annotation, method, clazz);
350 }
351 catch (NoSuchMethodException e) {
352 // fallback...
353 }
354
355 if (declaredAnnotation == null && clazz.getSuperclass() != null)
356 declaredAnnotation = getMethodAnnotation(clazz.getSuperclass(), name, parameterTypes, annotationClass);
357
358 if (declaredAnnotation == null) {
359 for (Class<?> interfaze : clazz.getInterfaces()) {
360 declaredAnnotation = getMethodAnnotation(interfaze, name, parameterTypes, annotationClass);
361 if (declaredAnnotation != null)
362 break;
363 }
364 }
365
366 return declaredAnnotation;
367 }
368
369 public static <T extends Annotation> DeclaredAnnotation<T> getConstructorAnnotation(Class<?> clazz, Class<T> annotationClass) {
370 DeclaredAnnotation<T> declaredAnnotation = null;
371
372 for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
373 T annotation = constructor.getAnnotation(annotationClass);
374 if (annotation != null) {
375 declaredAnnotation = new DeclaredAnnotation<T>(annotation, constructor, clazz);
376 break;
377 }
378 }
379
380 if (declaredAnnotation == null && clazz.getSuperclass() != null)
381 declaredAnnotation = getConstructorAnnotation(clazz.getSuperclass(), annotationClass);
382
383 return declaredAnnotation;
384 }
385
386 public static <T extends Annotation> DeclaredAnnotation<T> getClassAnnotation(Class<?> clazz, Class<T> annotationClass) {
387 DeclaredAnnotation<T> declaredAnnotation = null;
388
389 T annotation = clazz.getAnnotation(annotationClass);
390 if (annotation != null)
391 declaredAnnotation = new DeclaredAnnotation<T>(annotation, clazz, clazz);
392 else {
393 if (clazz.getSuperclass() != null)
394 declaredAnnotation = getClassAnnotation(clazz.getSuperclass(), annotationClass);
395
396 if (declaredAnnotation == null) {
397 for (Class<?> interfaze : clazz.getInterfaces()) {
398 declaredAnnotation = getClassAnnotation(interfaze, annotationClass);
399 if (declaredAnnotation != null)
400 break;
401 }
402 }
403 }
404
405 return declaredAnnotation;
406 }
407
408 public static class DeclaredAnnotation<T extends Annotation> {
409
410 public final T annotation;
411 public final AnnotatedElement annotatedElement;
412 public final Class<?> declaringClass;
413
414 public DeclaredAnnotation(T annotation, AnnotatedElement annotatedElement, Class<?> declaringClass) {
415 this.annotation = annotation;
416 this.annotatedElement = annotatedElement;
417 this.declaringClass = declaringClass;
418 }
419
420 @Override
421 public String toString() {
422 return getClass().getName() + "{annotation=" + annotation + ", annotatedElement=" + annotatedElement + ", declaringClass=" + declaringClass + "}";
423 }
424 }
425 }