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:com.yimidida.shards.utils.ParameterUtil.java

public static Object generatePrimaryKey(Object object, Serializable id) {
    if (object != null) {
        Assert.notNull(id, "generated id can not be null.");

        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);//from   w w w . j a  v  a2 s . c  o m

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //set id
                    field.set(object, id);

                    return object;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static List<Field> findAllFields(Class<?> element) {
    ArrayList<Field> fields = new ArrayList<Field>();
    if (element != null) {
        for (Field field : element.getDeclaredFields()) {
            fields.add(field);//w  ww.  j  av a 2s.c  o  m
        }
        fields.addAll(findAllFields(element.getSuperclass()));
        return fields;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.braffdev.server.core.container.injection.DependencyInjector.java

/**
 * Determines the list of fields that need to be injected.
 *
 * @param target the object./*from w  w w  . j ava2s  . c  o m*/
 * @return the list of fields.
 */
private static List<Field> getFields(Object target) {
    List<Field> fields = new ArrayList<Field>();

    Class<?> clazz = target.getClass();
    while (!Object.class.equals(clazz)) {

        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {

            // is the Dependency annotation present?
            if (field.isAnnotationPresent(Dependency.class)) {
                fields.add(field);
            }
        }

        clazz = clazz.getSuperclass();
    }

    return fields;
}

From source file:Main.java

public static boolean equals(Object target, Object o) {
    if (target == o)
        return true;
    if (target == null || o == null)
        return false;
    if (!target.getClass().equals(o.getClass()))
        return false;

    Class<?> current = target.getClass();
    do {//from   w  w w  .  j  ava2  s  .c  om
        for (Field f : current.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                    || f.isSynthetic()) {
                continue;
            }

            Object self;
            Object other;
            try {
                f.setAccessible(true);
                self = f.get(target);
                other = f.get(o);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
            if (self == null) {
                if (other != null)
                    return false;
            } else if (self.getClass().isArray()) {
                if (self.getClass().equals(boolean[].class)) {
                    if (!Arrays.equals((boolean[]) self, (boolean[]) other))
                        return false;
                } else if (self.getClass().equals(char[].class)) {
                    if (!Arrays.equals((char[]) self, (char[]) other))
                        return false;
                } else if (self.getClass().equals(byte[].class)) {
                    if (!Arrays.equals((byte[]) self, (byte[]) other))
                        return false;
                } else if (self.getClass().equals(short[].class)) {
                    if (!Arrays.equals((short[]) self, (short[]) other))
                        return false;
                } else if (self.getClass().equals(int[].class)) {
                    if (!Arrays.equals((int[]) self, (int[]) other))
                        return false;
                } else if (self.getClass().equals(long[].class)) {
                    if (!Arrays.equals((long[]) self, (long[]) other))
                        return false;
                } else if (self.getClass().equals(float[].class)) {
                    if (!Arrays.equals((float[]) self, (float[]) other))
                        return false;
                } else if (self.getClass().equals(double[].class)) {
                    if (!Arrays.equals((double[]) self, (double[]) other))
                        return false;
                } else {
                    if (!Arrays.equals((Object[]) self, (Object[]) other))
                        return false;
                }
            } else if (!self.equals(other)) {
                return false;
            }
        }
        current = current.getSuperclass();
    } while (!Object.class.equals(current));

    return true;
}

From source file:gr.abiss.calipso.jpasearch.json.serializer.FormSchemaSerializer.java

private static String getFormFieldConfig(Class domainClass, String fieldName) {
    String formSchemaJson = null;
    Field field = null;//from w  w w .j  a  va  2 s.c  o m
    StringBuffer formConfig = new StringBuffer();
    String key = domainClass.getName() + "#" + fieldName;
    String cached = CONFIG_CACHE.get(key);
    if (StringUtils.isNotBlank(cached)) {
        formConfig.append(cached);
    } else {
        Class tmpClass = domainClass;
        do {
            for (Field tmpField : tmpClass.getDeclaredFields()) {
                String candidateName = tmpField.getName();
                if (candidateName.equals(fieldName)) {
                    field = tmpField;
                    FormSchemas formSchemasAnnotation = null;
                    if (field.isAnnotationPresent(FormSchemas.class)) {
                        formSchemasAnnotation = field.getAnnotation(FormSchemas.class);
                        gr.abiss.calipso.jpasearch.annotation.FormSchemaEntry[] formSchemas = formSchemasAnnotation
                                .value();
                        LOGGER.info("getFormFieldConfig, formSchemas: " + formSchemas);
                        if (formSchemas != null) {
                            for (int i = 0; i < formSchemas.length; i++) {
                                if (i > 0) {
                                    formConfig.append(comma);
                                }
                                gr.abiss.calipso.jpasearch.annotation.FormSchemaEntry formSchemaAnnotation = formSchemas[i];
                                LOGGER.info(
                                        "getFormFieldConfig, formSchemaAnnotation: " + formSchemaAnnotation);
                                appendFormFieldSchema(formConfig, formSchemaAnnotation.state(),
                                        formSchemaAnnotation.json());
                            }
                        }
                        //formConfig = formSchemasAnnotation.json();
                    } else {
                        appendFormFieldSchema(formConfig,
                                gr.abiss.calipso.jpasearch.annotation.FormSchemaEntry.STATE_DEFAULT,
                                gr.abiss.calipso.jpasearch.annotation.FormSchemaEntry.TYPE_STRING);
                    }
                    break;
                }
            }
            tmpClass = tmpClass.getSuperclass();
        } while (tmpClass != null && field == null);
        formSchemaJson = formConfig.toString();
        CONFIG_CACHE.put(key, formSchemaJson);
    }

    return formSchemaJson;
}

From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java

public static List<MockedField> translateObject(Object object, String containerClassName, String fieldName,
        String contextPath) {// www  .j a v  a2  s .  c  o  m
    if (object == null) {
        return emptyList();
    }
    if (isBlackListed(object.getClass())) {
        String fieldType = object.getClass().getName();
        MockedField out = buildMockedField(containerClassName, fieldName, contextPath, fieldType);
        return asList(out);
    }
    DefaultSupportedType type = DefaultSupportedType.evaluate(object);
    if (type != null && type.isPrimitiveOrSimpleValue()) {
        return asList(getSingleFieldFromDefaultSupportedType(object, type, contextPath, containerClassName,
                fieldName));
    }
    List<MockedField> out = new ArrayList<MockedField>();
    Class<?> objectClass = object.getClass();
    String className = objectClass.getName();
    for (Field field : objectClass.getDeclaredFields()) {
        if (!Modifier.isTransient(field.getModifiers()) && !isBlackListed(field.getType())) {
            out.add(translateField(field, object, className, contextPath));
        }
    }
    return out;
}

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static void inspectAndSaveAttribute(Class<?> searchElement, Collection<AttributeEntity> attributes) {
    if (searchElement != null) {
        for (Field field : searchElement.getDeclaredFields()) {
            if (field.isAnnotationPresent(SearchAttribute.class)) {
                AttributeEntity attrDef = new AttributeEntity().setName(field.getName())
                        .setType(field.getType());
                String value = field.getAnnotation(SearchAttribute.class).value();
                if (value != null && !value.isEmpty()) {
                    try {
                        Object ovalue = BeanUtils.instantiateClass(field.getType().getConstructor(String.class),
                                value);/*from   ww  w  . ja va 2 s  . c o  m*/
                        attrDef.setValue(ovalue);
                    } catch (BeanInstantiationException | NoSuchMethodException | SecurityException e) {
                        LOGGER.trace("Could not construct default value (not much of a problem)", e);
                    }
                }
                attributes.add(attrDef);
            }
        }
        inspectAndSaveAttribute(searchElement.getSuperclass(), attributes);
    } else {
        return;
    }
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static List<String> listAllProperties(Class<?> clazz, String rootName) {
    List<String> p = new ArrayList<String>();
    for (Field f : clazz.getDeclaredFields()) {
        String n = f.getName();/*from www .ja  va  2  s  .  com*/
        Class<?> c = f.getType();
        if (null != rootName && !rootName.isEmpty()) {
            n = String.format("%s.%s", rootName, n);
        }
        p.add(n);
        if (!c.getName().startsWith("java.")) {
            p.addAll(listAllProperties(c, n));
        }
    }
    return p;
}

From source file:Main.java

private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
    fields.addAll(Arrays.asList(type.getDeclaredFields()));

    if (type.getSuperclass() != null) {
        fields = getAllFields(fields, type.getSuperclass());
    }/*  w w  w  .  j a va 2 s. c  o  m*/

    return fields;
}

From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java

public static List<Field> getOnlyFields(Class clazz) {

    List<Field> salida = new ArrayList<Field>(0);

    Field[] all = clazz.getDeclaredFields();

    for (int i = 0; i < all.length; i++) {
        Field field = all[i];/* w ww  . j a va 2 s. c  o m*/

        logger.debug(Modifier.toString(field.getModifiers()) + "-" + field.getModifiers());
        if (field.getModifiers() == Modifier.PRIVATE) {

            salida.add(field);
        }
    }

    return salida;
}