/** * This method return all the fields (including private) from the given class and its super * classes. * * @param fieldClass Class * @return List<Field> * @should return all fields include private and super classes */ public static List<Field> getAllFields(Class<?> fieldClass) { List<Field> fields = ClassDataCacher.getInstance().getClassData(fieldClass).getFields(); return new ArrayList<>(fields); }
/** * Get the class fields analysis of a class which contains information about that class and its fields, * includes annotations, fields/properties, etc. packaged in a way which makes the data easy to get to, * use the {@link ClassData} object to get to the more raw data * * @param <T> * @param cls any {@link Class} * @return the ClassFields analysis object which contains the information about this class */ public <T> ClassFields<T> getClassFields(Class<T> cls) { return getClassFields(cls, this.fieldFindMode); }
/** * Construct and specify the field finding mode and your own cache when caching class data, must implement the standard map interface but * only the following methods are required:<br/> * {@link Map#clear()}, {@link Map#size()}, {@link Map#put(Object, Object)}, {@link Map#get(Object)} <br/> * <br/> * <b>WARNING:</b> if you don't need this control then just use the {@link #getInstance()} method to get this * * @param fieldFindMode the mode when looking up fields in classes (null for default of {@link FieldFindMode#HYBRID}) * @param reflectionCache a map implementation to use as the cache mechanism (null to use internal) */ @SuppressWarnings("unchecked") public ClassDataCacher(FieldFindMode fieldFindMode, Map<Class<?>, ClassFields> reflectionCache) { setFieldFindMode(fieldFindMode); setReflectionCache(reflectionCache); ClassDataCacher.setInstance(this); }
/** * This method returns true if the given annotation is present on the given field. * * @param fieldClass * @param fieldName * @param annotation * @return true if the given annotation is present */ public static boolean isAnnotationPresent(Class<?> fieldClass, String fieldName, Class<? extends Annotation> annotation) { ClassFields<?> classFields = ClassDataCacher.getInstance().getClassFields(fieldClass); try { return classFields.getFieldAnnotation(annotation, fieldName) != null; } catch (FieldnameNotFoundException e) { return false; } }
protected ClassDataCacher getClassDataCacher() { return ClassDataCacher.getInstance(); }
constructors = getClassDataCacher().getClassData(type).getConstructors(); } catch (IllegalArgumentException e) { try {
/** * Clears all cached objects */ public void clear() { getReflectionCache().clear(); }
/** * Set the option to include the "class" field when reflecting over objects, * the default for this is false (do not include) * @param includeClassField if true then include the value from the "getClass()" method as "class" when encoding beans and maps */ public void setIncludeClassField(boolean includeClassField) { getClassDataCacher().setIncludeClassField(includeClassField); }
/** * Set the cache to be used for holding the reflection data, * this allows control over where the reflection caches are stored, * this should store the data in a way that it will not hold open the classloader the class comes from <br/> * Note that you can set this to a map implementation which does not store anything to disable caching if you like * * @param reflectionCache a cache for holding class cache data (implements map), null to use the default internal cache */ @SuppressWarnings("unchecked") public void setReflectionCache(Map<Class<?>, ClassFields> reflectionCache) { getClassDataCacher().setReflectionCache(reflectionCache); }
/** * Set the mode used to find fields on classes (default {@link FieldFindMode#HYBRID}) <br/> * <b>WARNING</b>: changing modes will clear the existing cache * * @param fieldFindMode see {@link FieldFindMode} enum for details, null for default * @see FieldFindMode */ public void setFieldFindMode(FieldFindMode fieldFindMode) { getClassDataCacher().setFieldFindMode(fieldFindMode); }
/** * Get a singleton instance of this class to work with (stored statically) <br/> * <b>WARNING</b>: do not hold onto this object or cache it yourself, call this method again if you need it again * @return a singleton instance of this class */ public static ClassDataCacher getInstance() { ClassDataCacher instance = (instanceStorage == null ? null : instanceStorage.get()); if (instance == null) { instance = ClassDataCacher.setInstance(null); } return instance; } /**
/** * Set the singleton instance of the class which will be stored statically * @param newInstance the instance to use as the singleton instance */ public static ClassDataCacher setInstance(ClassDataCacher newInstance) { ClassDataCacher instance = newInstance; if (instance == null) { instance = new ClassDataCacher(); instance.singleton = true; } ClassDataCacher.timesCreated++; instanceStorage = new SoftReference<ClassDataCacher>(instance); return instance; } public static void clearInstance() {
/** * Convenience Method: <br/> * Gets the class data object which contains information about this objects class, * will retrieve this from the class data cache if available or generate it if not<br/> * This is also available from the {@link ClassFields} object * * @param <T> * @param obj any {@link Object} * @return the raw ClassData cache object which contains reflection data about this objects class * @throws IllegalArgumentException if obj is null */ public <T> ClassData<T> getClassData(Object obj) { ClassFields<T> cf = getClassFieldsFromObject(obj); return cf.getClassData(); }
protected ClassDataCacher getClassDataCacher() { return ClassDataCacher.getInstance(); }
constructors = getClassDataCacher().getClassData(type).getConstructors(); } catch (IllegalArgumentException e) { try {
/** * Clears all cached objects */ public void clear() { getReflectionCache().clear(); }
/** * Set the option to include the "class" field when reflecting over objects, * the default for this is false (do not include) * @param includeClassField if true then include the value from the "getClass()" method as "class" when encoding beans and maps */ public void setIncludeClassField(boolean includeClassField) { getClassDataCacher().setIncludeClassField(includeClassField); }
/** * Set the cache to be used for holding the reflection data, * this allows control over where the reflection caches are stored, * this should store the data in a way that it will not hold open the classloader the class comes from <br/> * Note that you can set this to a map implementation which does not store anything to disable caching if you like * * @param reflectionCache a cache for holding class cache data (implements map), null to use the default internal cache */ @SuppressWarnings("unchecked") public void setReflectionCache(Map<Class<?>, ClassFields> reflectionCache) { getClassDataCacher().setReflectionCache(reflectionCache); }
/** * Set the mode used to find fields on classes (default {@link FieldFindMode#HYBRID}) <br/> * <b>WARNING</b>: changing modes will clear the existing cache * * @param fieldFindMode see {@link FieldFindMode} enum for details, null for default * @see FieldFindMode */ public void setFieldFindMode(FieldFindMode fieldFindMode) { getClassDataCacher().setFieldFindMode(fieldFindMode); }
/** * Get a singleton instance of this class to work with (stored statically) <br/> * <b>WARNING</b>: do not hold onto this object or cache it yourself, call this method again if you need it again * @return a singleton instance of this class */ public static ClassDataCacher getInstance() { ClassDataCacher instance = (instanceStorage == null ? null : instanceStorage.get()); if (instance == null) { instance = ClassDataCacher.setInstance(null); } return instance; } /**