Example usage for java.lang Class getDeclaredFields

List of usage examples for java.lang Class getDeclaredFields

Introduction

In this page you can find the example usage for java.lang Class getDeclaredFields.

Prototype

@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException 

Source Link

Document

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Usage

From source file:nl.surfnet.coin.teams.control.AbstractControllerTest.java

/**
 * Autowire all dependencies with a annotation autowired with a mock that does
 * nothing/* w  ww .  jav a  2  s. c  o m*/
 *
 * @param target the controller
 */
protected void autoWireRemainingResources(Object target) throws Exception {
    Class<? extends Object> clazz = target.getClass();
    while (!clazz.equals(Object.class)) {
        doAutoWireRemainingResources(target, clazz.getDeclaredFields());
        clazz = clazz.getSuperclass();
    }
}

From source file:com.thesett.util.validation.core.BeanValidationToJsonSchemaImpl.java

private void toJsonSchema(Validator validator, Class clazz, PropertyBuilder propertyBuilder) {
    BeanDescriptor constraintsForClass = validator.getConstraintsForClass(clazz);

    for (Field field : clazz.getDeclaredFields()) {
        String javaFieldName = field.getName();

        // Check if there is a JsonProperty annotation overriding the field name in json, and apply the override
        // if there is.
        JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);

        String fieldName;/*from   www .  j  av a  2  s  .com*/

        if (jsonProperty != null) {
            fieldName = jsonProperty.value();
        } else {
            fieldName = javaFieldName;
        }

        if ("this$0".equals(fieldName)) {
            continue;
        }

        ConstraintBuilder constraintBuilder = propertyBuilder.property(fieldName);

        Class<?> propertyType = field.getType();

        // Used to track when a type has been assigned, so that object type is only assigned later when no
        // other type has been matched.
        boolean typeAssigned = assignType(constraintBuilder, propertyType);

        PropertyDescriptor property = constraintsForClass.getConstraintsForProperty(fieldName);

        if (property != null) {
            // Assign object type and recursively expand the object, only when another type did not already match.
            if (!typeAssigned && Object.class.isAssignableFrom(propertyType)) {
                toJsonSchema(validator, propertyType, constraintBuilder.object());
            }

            for (ConstraintDescriptor constraintDescriptor : property.findConstraints()
                    .getConstraintDescriptors()) {
                Annotation annotation = constraintDescriptor.getAnnotation();
                convertMin(constraintBuilder, annotation);
                convertMax(constraintBuilder, annotation);
                convertLength(constraintBuilder, annotation);
                convertPattern(constraintBuilder, annotation);
                convertTitle(constraintBuilder, annotation);
                convertDescription(constraintBuilder, annotation);
            }
        }
    }
}

From source file:com.github.juanmf.java2plant.render.PlantRenderer.java

private boolean isToTypeInAggregations(Relation r) {
    Class<?> toType = TypesHelper.loadClass(r.getToType(), Parser.CLASS_LOADER);
    Class<?> origin = r.getFromType();
    for (Field f : origin.getDeclaredFields()) {
        // TODO: There migth be cases where toType is a generic Type param and this won't do well e.g. Collection<Type>
        if (f.getType().equals(toType)) {
            return true;
        }//ww w.j ava2 s . c o  m
    }
    return false;
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Copies the fields, where the field names are matches.
 *
 * @param <T> the generic type/*  w  w w. ja va  2  s  .com*/
 * @param targetClass the target class
 * @param source the source
 * @param target the target
 * @param fieldNameMatcher the field name matcher
 */
public static <T> void copyInstanceProperties(Class<?> targetClass, T source, T target,
        Matcher<Field> fieldNameMatcher) {
    if (targetClass == null) {
        return;
    }

    for (Field f : targetClass.getDeclaredFields()) {
        int mod = f.getModifiers();
        if (Modifier.isStatic(mod) == true) {
            continue;
        }
        if (fieldNameMatcher.match(f) == false) {
            continue;
        }
        Object value = readField(source, f.getName());
        writeField(target, f, value);
    }
    copyInstanceProperties(targetClass.getSuperclass(), source, target, fieldNameMatcher);
}

From source file:de.taimos.dvalin.interconnect.core.spring.InterconnectBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {/*from  w w w.j  a  v  a2  s .  c  o  m*/
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(Interconnect.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException(
                            "@Interconnect annotation is not supported on static fields");
                }
                currElements.add(new InterconnectElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(Interconnect.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@Interconnect annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@Interconnect annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new InterconnectElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}

From source file:jp.co.opentone.bsol.linkbinder.dto.AbstractDto.java

/**
 * aClass???List?????.//  w  ww  .j a  v a  2  s.  c o  m
 * @param aClass
 *            class
 * @return 
 */
private List<Field> getInstanceFields(Class<?> aClass) {

    List<Field> fields = new ArrayList<Field>();
    for (Field field : aClass.getDeclaredFields()) {
        if (!Modifier.isStatic(field.getModifiers())) {
            fields.add(field);
        }
    }
    return fields;
}

From source file:com.haulmont.cuba.gui.ControllerDependencyInjector.java

protected List<Field> getAllFields(List<Class> classes) {
    List<Field> list = new ArrayList<>();

    for (Class c : classes) {
        if (c != Object.class) {
            Collections.addAll(list, c.getDeclaredFields());
        }//from   w  ww . ja  va2  s . c  o  m
    }
    return list;
}

From source file:com.webpagebytes.cms.engine.JSONToFromObjectConverter.java

public <T> T objectFromJSONString(String jsonString, Class<T> objClass) {
    try {/*w  w  w .  j  a v a 2s  . c o m*/
        org.json.JSONObject json = new org.json.JSONObject(jsonString);
        T newObj = objClass.newInstance();
        Field[] fields = objClass.getDeclaredFields();
        for (Field field : fields) {
            Object storeAdn = field.getAnnotation(WPBAdminFieldStore.class);
            if (storeAdn == null) {
                storeAdn = field.getAnnotation(WPBAdminFieldKey.class);
                if (storeAdn == null) {
                    storeAdn = field.getAnnotation(WPBAdminFieldTextStore.class);
                    if (storeAdn == null) {
                        storeAdn = field.getAnnotation(WPBAdminField.class);
                    }
                }
            }

            if (storeAdn != null) {
                String fieldName = field.getName();
                try {
                    PropertyDescriptor pd = new PropertyDescriptor(fieldName, objClass);
                    Object fieldValue = fieldFromJSON(json, fieldName, field.getType());
                    if (fieldValue != null) {
                        pd.getWriteMethod().invoke(newObj, fieldFromJSON(json, fieldName, field.getType()));
                    }
                } catch (Exception e) {
                    // do nothing, there is no write method for our field
                }
            }
        }

        return newObj;

    } catch (Exception e) {
        // do nothing
    }
    return null;

}

From source file:com.google.ratel.util.RatelUtils.java

private static List<Field> getAllFields(Class cls) {
    List<Class> fullClassList = new ArrayList<Class>();
    fullClassList.add(cls);/*w  w  w.j a v a  2 s  .c o m*/

    Class parentClass = cls.getSuperclass();
    while (parentClass != null) {
        // Include parent classes up to but excluding Object.class
        if (parentClass.isAssignableFrom(Object.class)) {
            break;
        }
        fullClassList.add(parentClass);
        parentClass = parentClass.getSuperclass();
    }

    List<Field> fullFieldList = new ArrayList<Field>();

    for (Class aPageClass : fullClassList) {

        for (Field field : aPageClass.getDeclaredFields()) {

            fullFieldList.add(field);
            // If field is not public set accessibility true
            //if (!Modifier.isPublic(field.getModifiers())) {
            //  field.setAccessible(true);
            //}
        }
    }
    return fullFieldList;
}