public ClassIntrospector(Class<?> clazz) { this.clazz = clazz; fields = findFields(clazz); }
public static <T> T[] newArray(T[] a, int size) { return newArray(getComponentType(a), size); }
public static <T> BeanInstance<T> of(T instance) { return new BeanInstance<T>(instance); }
@SuppressWarnings("unchecked") private BeanInstance(T instance) { this.type = (Class<T>) instance.getClass(); this.instance = instance; List<Field> fields = findFields(type); fieldAnnotations = findFieldsAnnotations(fields); }
private boolean isSingleton(Class<?> field) { ClassIntrospector introspector = new ClassIntrospector(field); FieldWrap<Id> id = introspector.getFieldList(Id.class).get(0); if (id.isFinal() && id.isStatic()) { return true; } return false; }
private void validateField(FieldWrap<Config> field) { if (field.isStatic() && !field.isFinal()) { // non-final static @Property not supported. throw Events.CFG108_ILLEGAL_MODIFIERS(field.getAnnotation().name()); } if (field.isTransient()) { // transient @Property not supported. throw Events.CFG108_ILLEGAL_MODIFIERS(field.getAnnotation().name()); } } }
public static <T> BeanInstance<T> create(Class<T> elementType) { try { return new BeanInstance<T>(newInstance(elementType)); } catch (Exception e) { throw new RuntimeException(e); } }
private BeanId getSingletonId(Schema s, Class<?> configurable) { try { ClassIntrospector introspector = new ClassIntrospector(configurable); FieldWrap<Id> id = introspector.getFieldList(Id.class).get(0); String instanceId = id.getStaticValue().toString(); return BeanId.createSingleton(instanceId, s.getName()); } catch (Exception e) { throw new IllegalArgumentException("Configurable class [" + configurable + "] is not a singleton.", e); } }
public static List<Class<?>> computeEnclosingClasses(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); computeEnclosingClasses(clazz, classes); return classes; }
@SuppressWarnings("unchecked") public Collection<Object> getDefaultValues() { if (!isCollection) { throw new UnsupportedOperationException("This field is not a collection."); } return (Collection<Object>) getDefaultValue(); }
/** * Get all superclasses and interfaces recursively. * * @param clazz The class to start the search with. * * @return List of all super classes and interfaces of {@code clazz}. The list contains the class itself! The empty * list is returned if {@code clazz} is {@code null}. */ public static List<Class<?>> computeClassHierarchy(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); computeClassHierarchy(clazz, classes); return classes; }
public <T extends Annotation> List<FieldWrap<T>> getFieldList(Class<T> clazz) { ArrayList<FieldWrap<T>> wrap = new ArrayList<FieldWrap<T>>(); for (Field f : fields) { if (f.isAnnotationPresent(clazz)) { wrap.add(new FieldWrap<T>(f, f.getAnnotation(clazz))); } } return wrap; }
public List<Class<?>> getMapParamTypes() { if (!isMap) { throw new UnsupportedOperationException("Field [" + field + "] is not a map."); } List<Class<?>> p = getParameterizedType(field); if (p.size() == 0) { throw new UnsupportedOperationException("Map of field [" + field + "] does not have parameterized arguments, which is not allowed."); } return p; }
public BeanFieldInjector injectFieldsAnnotatedWith(Class<? extends Annotation> annotation) { Collection<Field> fields = fieldAnnotations.get(annotation); return new BeanFieldInjector(fields, instance, annotation); }
public static <T> Collection<BeanInstance<T>> of(Collection<T> instances) { ArrayList<BeanInstance<T>> arrayList = new ArrayList<BeanInstance<T>>(); for (T instance : instances) { arrayList.add(of(instance)); } return arrayList; }
private static Class<?> extractClass(Class<?> ownerClass, Type arg) { if (arg instanceof ParameterizedType) { return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); } else if (arg instanceof GenericArrayType) { throw new UnsupportedOperationException("GenericArray types are not supported."); } else if (arg instanceof TypeVariable) { throw new UnsupportedOperationException("GenericArray types are not supported."); } return (arg instanceof Class ? (Class<?>) arg : Object.class); }
private static String getIdField(Class<?> clazz) { for (Field field : findFields(clazz)) { field.setAccessible(true); Annotation annotation = field.getAnnotation(Id.class); if (annotation != null) { return field.getName(); } } throw new RuntimeException("Class [" + clazz + "] does not decalare @Id."); }
private static void computeEnclosingClasses(Class<?> clazz, List<Class<?>> classes) { for (Class<?> current = clazz; current != null; current = current.getEnclosingClass()) { if (classes.contains(current)) { return; } classes.add(current); for (Class<?> currentInterface : current.getInterfaces()) { computeEnclosingClasses(currentInterface, classes); } } }
public <T extends Annotation> Map<String, FieldWrap<T>> getFieldMap(Class<T> clazz) { HashMap<String, FieldWrap<T>> wrap = new HashMap<String, FieldWrap<T>>(); for (Field f : fields) { if (f.isAnnotationPresent(clazz)) { wrap.put(f.getName(), new FieldWrap<T>(f, f.getAnnotation(clazz))); } } return wrap; }
public SourceTargetPair(Converter converter) { List<Class<?>> types = getParameterizedType(converter.getClass(), Converter.class); if (types.size() < 2) { throw new IllegalArgumentException( "Unable to the determine generic source and target type " + "for converter. Please declare these generic types."); } this.source = types.get(0); this.target = types.get(1); this.converter = converter; }