public static Constructor<?> getConstructor(String type, Class<?>[] paramTypes) throws ClassNotFoundException, NoSuchMethodException { return getConstructor(forName(type), paramTypes); }
@Override protected boolean internalCanConvert(Object value, Type targetType) { Class<?> targetClass = TypeUtil.classOfType(targetType); return (targetClass.equals(Document.class)) && (value == null || (value instanceof String)); }
public Configuration newConfiguration() { try { return TypeUtil.newInstance("org.granite.client.configuration.SimpleConfiguration", Configuration.class); } catch (Throwable t) { throw new RuntimeException("Could not create simple configuration", t); } }
public static URL findResource(Class<?> clazz) { while (clazz.isArray()) clazz = clazz.getComponentType(); if (clazz.isPrimitive()) return null; URL url = getClassLoader(clazz).getResource(toResourceName(clazz)); String path = url.toString(); if (path.indexOf(' ') != -1) { try { url = new URL(path.replace(" ", "%20")); } catch (MalformedURLException e) { // should never happen... } } return url; }
private static Class<?> initClass(String type) { try { return TypeUtil.forName(type); } catch (Throwable t) { // No Java 8 } return null; }
public static <T extends Annotation> DeclaredAnnotation<T> getAnnotation(AnnotatedElement elmt, Class<T> annotationClass) { T annotation = elmt.getAnnotation(annotationClass); if (annotation != null) { Class<?> declaringClass = (elmt instanceof Member ? ((Member)elmt).getDeclaringClass() : (Class<?>)elmt); return new DeclaredAnnotation<T>(annotation, elmt, declaringClass); } if (elmt instanceof Field) return null; if (elmt instanceof Method) { Method m = (Method)elmt; return getMethodAnnotation(m.getDeclaringClass(), m.getName(), m.getParameterTypes(), annotationClass); } if (elmt instanceof Constructor) { Constructor<?> c = (Constructor<?>)elmt; return getConstructorAnnotation(c.getDeclaringClass(), annotationClass); } if (elmt instanceof Class) { Class<?> c = (Class<?>)elmt; return getClassAnnotation(c.getDeclaringClass(), annotationClass); } throw new RuntimeException("Unsupported annotated element: " + elmt); }
Introspector.flushFromCaches(clazz); PropertyDescriptor[] propertyDescriptors = TypeUtil.getProperties(clazz); Converters converters = ((ConvertersConfig)GraniteContext.getCurrentInstance().getGraniteConfig()).getConverters(); if (getter != null && !allFieldNames.contains(property.getName())) { DeclaredAnnotation<Include> annotation = TypeUtil.getAnnotation(getter, Include.class); if (annotation == null || (annotation.declaringClass != c && !annotation.declaringClass.isInterface())) continue;
/** * Converts the supplied object to the supplied target type. * * @param value the object to be converted. * @param targetType the target type. * @return the converted object. */ public final Object convert(Object value, Type targetType) { if (targetType instanceof TypeVariable<?>) targetType = TypeUtil.getBoundType((TypeVariable<?>)targetType); return internalConvert(value, targetType); }
public static boolean isAnnotationPresent(AnnotatedElement elmt, Class<? extends Annotation> annotationClass) { return getAnnotation(elmt, annotationClass) != null; }
private static void collectGenericInterfaces(Type[] types, List<ParameterizedType> supertypes) { if (types == null) return; for (Type t : types) { if (t instanceof ParameterizedType) supertypes.add((ParameterizedType)t); else collectGenericInterfaces(((Class<?>)t).getGenericInterfaces(), supertypes); } }
private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException { in.defaultReadObject(); if (selectorClassName != null) { try { messageSelectorConstructor = TypeUtil.getConstructor(selectorClassName, new Class<?>[] { String.class }); } catch (NoSuchMethodException e) { throw new IOException("Could not get message selector: " + selectorClassName); } } parseSelector(); }
public static <T extends Annotation> DeclaredAnnotation<T> getConstructorAnnotation(Class<?> clazz, Class<T> annotationClass) { DeclaredAnnotation<T> declaredAnnotation = null; for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { T annotation = constructor.getAnnotation(annotationClass); if (annotation != null) { declaredAnnotation = new DeclaredAnnotation<T>(annotation, constructor, clazz); break; } } if (declaredAnnotation == null && clazz.getSuperclass() != null) declaredAnnotation = getConstructorAnnotation(clazz.getSuperclass(), annotationClass); return declaredAnnotation; }
public static <T extends Annotation> DeclaredAnnotation<T> getClassAnnotation(Class<?> clazz, Class<T> annotationClass) { DeclaredAnnotation<T> declaredAnnotation = null; T annotation = clazz.getAnnotation(annotationClass); if (annotation != null) declaredAnnotation = new DeclaredAnnotation<T>(annotation, clazz, clazz); else { if (clazz.getSuperclass() != null) declaredAnnotation = getClassAnnotation(clazz.getSuperclass(), annotationClass); if (declaredAnnotation == null) { for (Class<?> interfaze : clazz.getInterfaces()) { declaredAnnotation = getClassAnnotation(interfaze, annotationClass); if (declaredAnnotation != null) break; } } } return declaredAnnotation; }
private static Class<?> forName(String name, String instantiator) { if (name.length() == 0) return HashMap.class; String className = (instantiator != null ? instantiator : name); try { return TypeUtil.forName(className); } catch (Throwable t) { throw new RuntimeException("Class not found: " + className); } }
public static <T extends Annotation> DeclaredAnnotation<T> getAnnotation(AnnotatedElement elmt, Class<T> annotationClass) { T annotation = elmt.getAnnotation(annotationClass); if (annotation != null) { Class<?> declaringClass = (elmt instanceof Member ? ((Member)elmt).getDeclaringClass() : (Class<?>)elmt); return new DeclaredAnnotation<T>(annotation, elmt, declaringClass); } if (elmt instanceof Field) return null; if (elmt instanceof Method) { Method m = (Method)elmt; return getMethodAnnotation(m.getDeclaringClass(), m.getName(), m.getParameterTypes(), annotationClass); } if (elmt instanceof Constructor) { Constructor<?> c = (Constructor<?>)elmt; return getConstructorAnnotation(c.getDeclaringClass(), annotationClass); } if (elmt instanceof Class) { Class<?> c = (Class<?>)elmt; return getClassAnnotation(c.getDeclaringClass(), annotationClass); } throw new RuntimeException("Unsupported annotated element: " + elmt); }
public static URL findResource(Class<?> clazz) { while (clazz.isArray()) clazz = clazz.getComponentType(); if (clazz.isPrimitive()) return null; URL url = getClassLoader(clazz).getResource(toResourceName(clazz)); String path = url.toString(); if (path.indexOf(' ') != -1) { try { url = new URL(path.replace(" ", "%20")); } catch (MalformedURLException e) { // should never happen... } } return url; }
Introspector.flushFromCaches(clazz); PropertyDescriptor[] propertyDescriptors = TypeUtil.getProperties(clazz); Converters converters = ((ConvertersConfig)GraniteContext.getCurrentInstance().getGraniteConfig()).getConverters(); if (getter != null && !allFieldNames.contains(property.getName())) { DeclaredAnnotation<Include> annotation = TypeUtil.getAnnotation(getter, Include.class); if (annotation == null || (annotation.declaringClass != c && !annotation.declaringClass.isInterface())) continue;
/** * Converts the supplied object to the supplied target type. * * @param value the object to be converted. * @param targetType the target type. * @return the converted object. */ public final Object convert(Object value, Type targetType) { if (targetType instanceof TypeVariable<?>) targetType = TypeUtil.getBoundType((TypeVariable<?>)targetType); return internalConvert(value, targetType); }
public static boolean isAnnotationPresent(AnnotatedElement elmt, Class<? extends Annotation> annotationClass) { return getAnnotation(elmt, annotationClass) != null; }
private static void collectGenericInterfaces(Type[] types, List<ParameterizedType> supertypes) { if (types == null) return; for (Type t : types) { if (t instanceof ParameterizedType) supertypes.add((ParameterizedType)t); else collectGenericInterfaces(((Class<?>)t).getGenericInterfaces(), supertypes); } }