/** * Set the singleton instance of the class which will be stored statically * @param newInstance the instance to use as the singleton instance */ public static FieldUtils setInstance(FieldUtils newInstance) { FieldUtils instance = newInstance; if (instance == null) { instance = new FieldUtils(); instance.singleton = true; } FieldUtils.timesCreated++; instanceStorage = new SoftReference<FieldUtils>(instance); return instance; } public static void clearInstance() {
/** * Get the types of the fields of a specific class type <br/> * returns the method names as fields (without the "get"/"is" part and camelCased) * @param type any class * @param filter (optional) indicates the fields to return the types for, can be null for defaults * @return a map of field name -> class type */ public Map<String, Class<?>> getFieldTypes(Class<?> type, FieldsFilter filter) { ClassFields<?> cf = analyzeClass(type, findFieldFindMode(filter)); Map<String, Class<?>> types = cf.getFieldTypes(filter); return types; }
/** * Analyze a class and produce an object which contains information about it and its fields * @param <T> * @param cls any class * @return the ClassFields analysis object which contains the information about this object class * @throws IllegalArgumentException if class is null or primitive */ public <T> ClassFields<T> analyzeClass(Class<T> cls) { ClassFields<T> cf = getClassDataCacher().getClassFields(cls); return cf; }
/** * Get the value of a field on an object as a specific type, * name can be nested, indexed, or mapped * @param obj any object * @param name the name of a field on this object * @param asType the type to return the value as (converts as needed) * @return the value in the field as the type requested * @throws FieldnameNotFoundException if this field name is invalid for this object * @throws FieldGetValueException if the field is not readable or not visible * @throws UnsupportedOperationException if the value cannot be converted to the type requested * @throws IllegalArgumentException if there is a failure getting the value */ public <T> T getFieldValue(Object obj, String name, Class<T> asType) { Object o = getFieldValue(obj, name); T value = getConversionUtils().convert(o, asType); return value; }
Holder holder = unpackNestedName(name, obj, true); name = holder.getName(); obj = holder.getObject(); Class<?> type = getFieldType(obj, name); value = getConversionUtils().convert(value, type); setValueOfMap((Map) obj, name, value); } else if (getResolver().isMapped(name)) { setMappedValue(obj, name, value); } else if (getResolver().isIndexed(name)) { setIndexedValue(obj, name, value); } else { setSimpleValue(obj, name, value);
try { while (getResolver().hasNested(name)) { String next = getResolver().next(name); Object nestedBean; if (Map.class.isAssignableFrom(obj.getClass())) { nestedBean = getValueOfMap((Map) obj, next); } else if (getResolver().isMapped(next)) { nestedBean = getMappedValue(obj, next); } else if (getResolver().isIndexed(next)) { nestedBean = getIndexedValue(obj, next); } else { nestedBean = getSimpleValue(obj, next); Class<?> type = getFieldType(obj, next); if (Object.class.equals(type)) { nestedBean = getConstructorUtils().constructClass(type); setFieldValue(obj, next, nestedBean, false); // need to put this new object into the parent } catch (RuntimeException e) { throw new IllegalArgumentException("Nested path failure: Could not create nested object (" name = getResolver().remove(name);
return getFieldType((Class<?>)obj, name); // EXIT while (getResolver().hasNested(name)) { String next = getResolver().next(name); Object nestedBean; if (Map.class.isAssignableFrom(obj.getClass())) { nestedBean = getValueOfMap((Map) obj, next); } else if (getResolver().isMapped(next)) { nestedBean = getMappedValue(obj, next); } else if (getResolver().isIndexed(next)) { nestedBean = getIndexedValue(obj, next); } else { nestedBean = getSimpleValue(obj, next); name = getResolver().remove(name); String targetName = getResolver().getProperty(name); // simple name of target field ClassFields<?> cf = analyzeObject(obj); try { ClassProperty cp = cf.getClassProperty(targetName); Field field = getFieldIfPossible(cd, name); if (field == null) { throw new FieldnameNotFoundException("Could not find field with name ("+name+") on object (" + obj + ") after extended look into non-visible fields", fnfe); if (getResolver().isIndexed(name) || getResolver().isMapped(name)) { if ( ConstructorUtils.isClassArray(fieldType) ) {
Resolver resolver = getResolver(); } else { ClassFields cf = analyzeObject(obj); ClassProperty cp = cf.getClassProperty(name); if (! cp.isMapped()) { Object o = findFieldValue(obj, cp); if (o == null) { map = (Map) type.newInstance(); setSimpleValue(obj, name, map); } catch (Exception e) {
Resolver resolver = getResolver(); } else { ClassFields cf = analyzeObject(obj); ClassProperty cp = cf.getClassProperty(name); if (! cp.isMapped()) { Object o = findFieldValue(obj, cp); if (! Map.class.isAssignableFrom(o.getClass())) { throw new IllegalArgumentException("Field (" + name + ") does not appear to be a map (not instance of Map)");
} else { ClassFields<?> cf = analyzeObject(obj); try { ClassProperty cp = cf.getClassProperty(name); assignFieldValue(obj, cp, value); } catch (FieldnameNotFoundException fnfe) { Field field = getFieldIfPossible(cd, name); if (field == null) { throw new FieldnameNotFoundException("Could not find field with name ("+name+") on object (" + obj + ") after extended look into non-visible fields", fnfe); value = getConversionUtils().convert(value, field.getType()); field.set(obj, value); } catch (Exception e) {
} else { ClassFields<?> cf = analyzeObject(obj); try { value = findFieldValue(obj, cp); } catch (FieldnameNotFoundException fnfe) { Field field = getFieldIfPossible(cd, name); if (field == null) { throw new FieldnameNotFoundException("Could not find field with name ("+name+") on object (" + obj + ") after extended look into non-visible fields", fnfe);
while (getResolver().hasNested(name)) { String next = getResolver().next(name); Class<?> nestedClass; if (Object.class.equals(type) || Map.class.isAssignableFrom(type) || getResolver().isMapped(next) || getResolver().isIndexed(next) ) { ClassFields<?> cf = analyzeClass(type); nestedClass = cf.getFieldType(name); name = getResolver().remove(name); String targetName = getResolver().getProperty(name); // simple name of target field } else { ClassFields<?> cf = analyzeClass(type); try { fieldType = cf.getFieldType(targetName); Field field = getFieldIfPossible(cd, name); if (field == null) { throw new FieldnameNotFoundException("Could not find field with name ("+name+") in class (" + type + ") after extended look into non-visible fields", fnfe); if (getResolver().isIndexed(name) || getResolver().isMapped(name)) { if ( ConstructorUtils.isClassArray(fieldType) ) {
internalDeepClone(value, dest, maxDepth, null, currentDepth, ignoreNulls, ignoreTransient) ); } else { getFieldUtils().setFieldValue(copy, name, internalDeepClone(value, dest, maxDepth, null, currentDepth, ignoreNulls, ignoreTransient) ); ClassFields<?> cf = getFieldUtils().analyzeClass(beanClass); Map<String, Class<?>> types = cf.getFieldTypes(filter); for (String name : types.keySet()) { Object value = getFieldUtils().getFieldValue(bean, name); if (value == null && ignoreNulls) { continue; internalDeepClone(value, dest, maxDepth, null, currentDepth, ignoreNulls, ignoreTransient) ); } else { getFieldUtils().setFieldValue(copy, name, internalDeepClone(value, dest, maxDepth, null, currentDepth, ignoreNulls, ignoreTransient) );
/** * Analyze an object and produce an object which contains information about it and its fields * @param obj any object * @return the ClassFields analysis object which contains the information about this object class * @throws IllegalArgumentException if obj is null */ @SuppressWarnings("unchecked") public <T> ClassFields<T> analyzeObject(Object obj) { if (obj == null) { throw new IllegalArgumentException("obj cannot be null"); } if (Class.class.equals(obj)) { // this is a class so we should pass it over to the other method return analyzeClass((Class)obj); } Class<T> cls = (Class<T>) obj.getClass(); return analyzeClass(cls); }
protected FieldAdapter getFieldAdapter() { return getFieldUtils().getFieldAdapter(); }
Holder holder = unpackNestedName(name, obj, true); name = holder.getName(); obj = holder.getObject(); Class<?> type = getFieldType(obj, name); value = getConversionUtils().convert(value, type); setValueOfMap((Map) obj, name, value); } else if (getResolver().isMapped(name)) { setMappedValue(obj, name, value); } else if (getResolver().isIndexed(name)) { setIndexedValue(obj, name, value); } else { setSimpleValue(obj, name, value);
try { while (getResolver().hasNested(name)) { String next = getResolver().next(name); Object nestedBean; if (Map.class.isAssignableFrom(obj.getClass())) { nestedBean = getValueOfMap((Map) obj, next); } else if (getResolver().isMapped(next)) { nestedBean = getMappedValue(obj, next); } else if (getResolver().isIndexed(next)) { nestedBean = getIndexedValue(obj, next); } else { nestedBean = getSimpleValue(obj, next); Class<?> type = getFieldType(obj, next); if (Object.class.equals(type)) { nestedBean = getConstructorUtils().constructClass(type); setFieldValue(obj, next, nestedBean, false); // need to put this new object into the parent } catch (RuntimeException e) { throw new IllegalArgumentException("Nested path failure: Could not create nested object (" name = getResolver().remove(name);
return getFieldType((Class<?>)obj, name); // EXIT while (getResolver().hasNested(name)) { String next = getResolver().next(name); Object nestedBean; if (Map.class.isAssignableFrom(obj.getClass())) { nestedBean = getValueOfMap((Map) obj, next); } else if (getResolver().isMapped(next)) { nestedBean = getMappedValue(obj, next); } else if (getResolver().isIndexed(next)) { nestedBean = getIndexedValue(obj, next); } else { nestedBean = getSimpleValue(obj, next); name = getResolver().remove(name); String targetName = getResolver().getProperty(name); // simple name of target field ClassFields<?> cf = analyzeObject(obj); try { ClassProperty cp = cf.getClassProperty(targetName); Field field = getFieldIfPossible(cd, name); if (field == null) { throw new FieldnameNotFoundException("Could not find field with name ("+name+") on object (" + obj + ") after extended look into non-visible fields", fnfe); if (getResolver().isIndexed(name) || getResolver().isMapped(name)) { if ( ConstructorUtils.isClassArray(fieldType) ) {
Resolver resolver = getResolver(); } else { ClassFields cf = analyzeObject(obj); ClassProperty cp = cf.getClassProperty(name); if (! cp.isMapped()) { Object o = findFieldValue(obj, cp); if (o == null) { map = (Map) type.newInstance(); setSimpleValue(obj, name, map); } catch (Exception e) {
Resolver resolver = getResolver(); } else { ClassFields cf = analyzeObject(obj); ClassProperty cp = cf.getClassProperty(name); if (! cp.isMapped()) { Object o = findFieldValue(obj, cp); if (! Map.class.isAssignableFrom(o.getClass())) { throw new IllegalArgumentException("Field (" + name + ") does not appear to be a map (not instance of Map)");