Example usage for org.apache.commons.lang3.reflect FieldUtils getAllFields

List of usage examples for org.apache.commons.lang3.reflect FieldUtils getAllFields

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils getAllFields.

Prototype

public static Field[] getAllFields(final Class<?> cls) 

Source Link

Document

Gets all fields of the given class and its parents (if any).

Usage

From source file:org.kuali.rice.krad.data.util.ReferenceLinker.java

/**
* Gets indexes that have been modified./* w w w. j  a  v a  2  s  .c  om*/
*
* <p>
*     Returns a list of cascade links in the field names that are also in the decomposed paths.
* </p>
*
* @param decomposedPaths contains field names to be used.
* @param linked
* @param wrapped used to get all field names.
*/
protected void cascadeLinkingAnnotations(DataObjectWrapper<?> wrapped, Map<String, Set<String>> decomposedPaths,
        Set<Object> linked) {
    Field[] fields = FieldUtils.getAllFields(wrapped.getWrappedClass());
    Map<String, Field> modifiedFieldMap = new HashMap<String, Field>();
    for (Field field : fields) {
        if (decomposedPaths.containsKey(field.getName())) {
            modifiedFieldMap.put(field.getName(), field);
        }
    }
    for (String modifiedFieldName : modifiedFieldMap.keySet()) {
        Field modifiedField = modifiedFieldMap.get(modifiedFieldName);
        Link link = modifiedField.getAnnotation(Link.class);
        if (link == null) {
            // check if they have an @Link on the class itself
            link = AnnotationUtils.findAnnotation(modifiedField.getType(), Link.class);
        }
        if (link != null && link.cascade()) {
            List<String> linkingPaths = assembleLinkingPaths(link);
            for (String linkingPath : linkingPaths) {
                Map<String, Set<String>> decomposedLinkingPath = decomposePropertyPaths(
                        decomposedPaths.get(modifiedFieldName), linkingPath);
                String valuePath = modifiedFieldName;
                if (StringUtils.isNotBlank(linkingPath)) {
                    valuePath = valuePath + "." + link.path();
                }
                Object linkRootObject = wrapped.getPropertyValueNullSafe(valuePath);
                linkChangesInternal(linkRootObject, decomposedLinkingPath, linked);
            }
        }
    }
}

From source file:org.kuali.rice.krad.web.bind.UifServletRequestDataBinder.java

/**
 * Determines the root property paths relative to the given root object type against which to perform automatic
 * linking./*  w w  w  . j  a  v a2 s .  c om*/
 *
 * <p>This will be determined based on the presence of {@link Link} annotations on the given root object type.
 * This method is invoked recursively as it walks the class structure looking for Link annotations. It uses the
 * path
 * and scanned arguments to keep track of how deep into the structure the scanning is and to prevent infinite
 * recursion.</p>
 *
 * @param rootObjectType the root object type from which to perform the scan for auto-linking paths
 * @param path the current property path relative to the original root object type at which the scan began, if null
 * then we are scanning from the root-most object type. Each recursive call of this method will append
 * a new property to this path
 * @param scanned used to track classes that have already been scanned and prevent infinite recursion
 * @return a set of property paths that should be auto linked
 */
protected Set<String> determineRootAutoLinkingPaths(Class<?> rootObjectType, String path,
        Set<Class<?>> scanned) {
    Set<String> autoLinkingPaths = new HashSet<String>();
    if (scanned.contains(rootObjectType)) {
        return autoLinkingPaths;
    } else {
        scanned.add(rootObjectType);
    }
    Link autoLink = AnnotationUtils.findAnnotation(rootObjectType, Link.class);
    if (autoLink != null && autoLink.cascade()) {
        autoLinkingPaths.addAll(assembleAutoLinkingPaths(path, autoLink));
    } else if (autoLink == null) {
        Field[] fields = FieldUtils.getAllFields(rootObjectType);
        for (Field field : fields) {
            autoLink = field.getAnnotation(Link.class);
            if (autoLink != null) {
                if (autoLink.cascade()) {
                    String fieldPath = appendToPath(path, field.getName());
                    autoLinkingPaths.addAll(assembleAutoLinkingPaths(fieldPath, autoLink));
                }
            } else {
                autoLinkingPaths.addAll(determineRootAutoLinkingPaths(field.getType(),
                        appendToPath(path, field.getName()), scanned));
            }
        }
    }
    return autoLinkingPaths;
}

From source file:org.objectpocket.util.IdSupport.java

/**
 * returns id from annotation @Id or existing id or generate random UUID.
 * //ww w .  j  av  a 2s .  c  o  m
 * @param obj
 * @param referenceForAnnotation
 *            when set to true the method will return a referenced
 *            representation of the id instead of the real id. This only
 *            happens for classes with custom id defined by @Id annotation.
 *            For all others the generated Id will be returned.
 * @param put
 *            an existing id here if one exists, otherwise a new might be
 *            generated
 * @return
 */
public static String getId(Object obj, boolean referenceForAnnotation, String existingId) {
    String typeName = obj.getClass().getName();
    if (!idFieldForType_ObjectsInMemory.containsKey(typeName)) {
        Field[] fields = FieldUtils.getAllFields(obj.getClass());
        for (Field field : fields) {
            if (field.isAnnotationPresent(Id.class)) {
                if (field.getType().equals(String.class)) {
                    idFieldForType_ObjectsInMemory.put(typeName, field);
                } else {
                    Logger.getAnonymousLogger().warning("@Id annotated field in class " + typeName
                            + " is not of type java.lang.String. Will generate random ids for this class.");
                }
                break;
            }
        }
        if (!idFieldForType_ObjectsInMemory.containsKey(typeName)) {
            idFieldForType_ObjectsInMemory.put(typeName, null);
        }
    }
    Field field = idFieldForType_ObjectsInMemory.get(typeName);
    if (field != null) {
        try {
            field.setAccessible(true);
            String id = (String) field.get(obj);
            if (id != null) {
                if (referenceForAnnotation) {
                    return OP_REF_STRING + field.getName();
                } else {
                    return id;
                }
            } else {
                Logger.getAnonymousLogger().warning("Id for object " + obj + " has not been set. "
                        + "Will generate random id for this class.");
            }
        } catch (IllegalAccessException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Could not read id from class " + typeName, e);
        }
    }
    if (existingId != null) {
        return existingId;
    }
    return UUID.randomUUID().toString();
}

From source file:org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDataChangeListenerTest.java

void setFinalStatic(Class cls, String fieldName, Object newValue) throws Exception {
    Field fields[] = FieldUtils.getAllFields(cls);
    for (Field field : fields) {
        if (fieldName.equals(field.getName())) {
            field.setAccessible(true);//  ww w. j  a v a2 s  . c  o  m
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, newValue);
            break;
        }
    }
}

From source file:org.openlmis.core.view.activity.BaseActivity.java

public void injectPresenter() {
    Field[] fields = FieldUtils.getAllFields(this.getClass());

    Optional<Field> annotatedFiled = FluentIterable.from(newArrayList(fields))
            .firstMatch(new Predicate<Field>() {
                @Override/*from  w w  w. j a  v  a2 s.  c  o m*/
                public boolean apply(Field field) {
                    return field.getAnnotation(InjectPresenter.class) != null;
                }
            });

    if (annotatedFiled.isPresent()) {
        InjectPresenter annotation = annotatedFiled.get().getAnnotation(InjectPresenter.class);
        if (!Presenter.class.isAssignableFrom(annotation.value())) {
            throw new RuntimeException("Invalid InjectPresenter class :" + annotation.value());
        }

        presenter = initPresenter(annotation.value());
        try {
            annotatedFiled.get().setAccessible(true);
            annotatedFiled.get().set(this, presenter);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(
                    "InjectPresenter type cast failed :" + annotation.value().getSimpleName());
        }
    }
    if (presenter == null) {
        presenter = new DummyPresenter();
    }
}

From source file:org.wso2.carbon.apimgt.core.TestUtil.java

public static <T> String printDiff(T obj1, T obj2) throws IllegalAccessException {
    Field[] fields = FieldUtils.getAllFields(obj1.getClass());

    for (Field field : fields) {
        Object obj1Value = FieldUtils.readField(field, obj1, true);
        Object obj2Value = FieldUtils.readField(field, obj2, true);

        String obj1ValueString = "null";
        String obj2ValueString = "null";
        if (obj1Value != null) {
            obj1ValueString = obj1Value.toString();
        }/*from   w ww  .  ja v  a2  s .c  om*/
        if (obj2Value != null) {
            obj2ValueString = obj2Value.toString();
        }

        if (!Objects.equals(obj1Value, obj2Value)) {
            return "Diff detected for '" + field.getName() + "' " + System.lineSeparator()
                    + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LHS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                    + System.lineSeparator() + obj1ValueString + System.lineSeparator()
                    + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LHS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                    + System.lineSeparator()
                    + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RHS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                    + System.lineSeparator() + obj2ValueString + System.lineSeparator()
                    + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RHS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
                    + System.lineSeparator();
        }
    }

    return null;
}