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.mycompany.client.bank.utils.CryptMessage.java

public static Object toInternal(String SuperSecret, Object criptStrings) {
    Class curClass = criptStrings.getClass();
    byte[] syperSecretBytes = null;
    try {// w  w  w . j  av a  2  s.c  o  m
        syperSecretBytes = SuperSecret.getBytes("utf-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(CryptMessage.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Field field : curClass.getDeclaredFields()) {
        try {
            String str = null;
            if (field.get(criptStrings) != null)
                str = field.get(criptStrings).toString();
            if (str != null) {
                byte[] mybyte = str.getBytes("utf-8");
                int i = 0;
                ByteTransform(syperSecretBytes, mybyte, i);
                field.set(criptStrings, new String(mybyte, "utf-8"));
            }
        } catch (UnsupportedEncodingException | IllegalArgumentException | IllegalAccessException ex) {
            Logger.getLogger(CryptMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return criptStrings;
}

From source file:com.yiji.openapi.sdk.util.Reflections.java

public static Set<String> getFieldNames(Class<?> pojoClass) {
    Set<String> propertyNames = new HashSet<String>();
    Class<?> clazz = pojoClass;
    do {/*from   w ww  .j a  v a  2s  . c om*/
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers())) {
                propertyNames.add(field.getName());

            }
        }
        clazz = clazz.getSuperclass();
    } while (clazz != null && !clazz.getSimpleName().equalsIgnoreCase("Object"));
    return propertyNames;
}

From source file:com.yiji.openapi.sdk.util.Reflections.java

public static Set<String> getSimpleFieldNames(Class<?> pojoClass) {
    Set<String> propertyNames = new HashSet<String>();
    Class<?> clazz = pojoClass;
    do {//from ww w. j  ava  2  s . co m
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers()) && (field.getType().isPrimitive()
                    || isWrapClass(field.getType()) || field.getType().isAssignableFrom(Timestamp.class)
                    || field.getType().isAssignableFrom(Date.class)
                    || field.getType().isAssignableFrom(String.class)
                    || field.getType().isAssignableFrom(Calendar.class))) {
                propertyNames.add(field.getName());
            }
        }
        clazz = clazz.getSuperclass();
    } while (clazz != null && !clazz.getSimpleName().equalsIgnoreCase("Object"));
    return propertyNames;
}

From source file:com.darkstar.beanCartography.utils.NameUtils.java

/**
 * Return public, private, protected, etc. fields declared by the passed
 * class in a field array.//from ww w.  java2  s. c  om
 *
 * @param clazz class to inspect
 * @param includeSuperClasses <code>true</code> will cause superclasses to be included in the field collection
 * @return array of Fields found
 */
public static Field[] getClassFields(Class<?> clazz, boolean includeSuperClasses) {
    Preconditions.checkNotNull(clazz, "Class cannot be null!");
    List<Field> allFields = new ArrayList<>();
    do {
        allFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    } while (includeSuperClasses && (clazz = clazz.getSuperclass()) != null);

    Field[] fields = new Field[allFields.size()];
    return allFields.toArray(fields);
}

From source file:index.IndexUtils.java

/**
 * ??/*  w w w.j av a  2 s  .c  o m*/
 * @param doc
 * @param entityPath
 * @return
 * @throws ClassNotFoundException
 * @throws java.beans.IntrospectionException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws java.lang.reflect.InvocationTargetException
 * @throws InstantiationException
 * @throws NoSuchMethodException 
 */
public static Object getIndexResult(Document doc, String entityPath)
        throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, InstantiationException, NoSuchMethodException {
    // ??
    Class c2 = Class.forName(entityPath);
    // 
    Object obj = c2.getConstructor(new Class[] {}).newInstance();
    // 
    Field[] fields = c2.getDeclaredFields();
    // ??
    for (Field field : fields) {
        // ??
        PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c2);
        // setget
        Method method = pd.getWriteMethod();
        // 
        method.invoke(obj, new Object[] { doc.get(field.getName()) });
    }
    return obj;
}

From source file:com.github.vanroy.springdata.jest.MappingBuilder.java

private static java.lang.reflect.Field[] retrieveFields(Class clazz) {
    // Create list of fields.
    List<java.lang.reflect.Field> fields = new ArrayList<java.lang.reflect.Field>();

    // Keep backing up the inheritance hierarchy.
    Class targetClass = clazz;
    do {//from   ww w . j av a 2s  . c  o  m
        fields.addAll(Arrays.asList(targetClass.getDeclaredFields()));
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return fields.toArray(new java.lang.reflect.Field[fields.size()]);
}

From source file:com.cons.gps2exif.exif.ReadMetaData.java

private static List<Field> getTagInfoDefinedInClass(Class<?> clazz) {
    Field[] declaredFields = clazz.getDeclaredFields();
    return Arrays.stream(declaredFields).filter(
            field -> TagInfo.class.isAssignableFrom(field.getType()) && Modifier.isStatic(field.getModifiers()))
            .collect(Collectors.toList());
}

From source file:com.ms.commons.test.common.ReflectUtil.java

protected static void getDeclaredFields(Class<?> clazz, List<Field> fieldList) {
    if (clazz == Object.class) {
        return;//from w w  w  . j  av a2s.  co  m
    }
    Field[] fields = clazz.getDeclaredFields();
    if (fields != null) {
        for (Field field : fields) {
            fieldList.add(field);
        }
    }
    getDeclaredFields(clazz.getSuperclass());
}

From source file:com.topsem.common.utils.Reflections.java

/**
 * ?/* ww w  .  ja v  a2s  .  c  om*/
 *
 * @param clazz
 * @return
 */
public static List<Field> getAllDeclaredFields(Class<?> clazz) {
    List<Field> fields = new ArrayList<Field>();
    for (Class c : getAllClasses(clazz)) {
        fields.addAll(Lists.newArrayList(c.getDeclaredFields()));
    }
    return fields;
}

From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java

public static Field getField(Class<?> clazz, String fieldName) {
    Field field = null;// www .  j  av a  2  s  . c o  m
    if (!IGNORED_FIELD_NAMES.contains(fieldName)) {

        String key = clazz.getName() + "#" + fieldName;
        field = FIELD_CACHE.get(key);

        // find it if not cached
        if (field == null && !FIELD_CACHE.containsKey(key)) {
            Class<?> tmpClass = clazz;
            do {
                for (Field tmpField : tmpClass.getDeclaredFields()) {
                    String candidateName = tmpField.getName();
                    if (candidateName.equals(fieldName)) {
                        // field.setAccessible(true);
                        FIELD_CACHE.put(key, tmpField);
                        field = tmpField;
                        break;
                    }
                }
                tmpClass = tmpClass.getSuperclass();
            } while (tmpClass != null && field == null);
        }
        if (field == null) {
            LOGGER.warn("Field '" + fieldName + "' not found on class " + clazz);
            // HashMap handles null values so we can use containsKey to cach
            // invalid fields and hence skip the reflection scan
            FIELD_CACHE.put(key, null);
        }
        // throw new RuntimeException("Field '" + fieldName +
        // "' not found on class " + clazz);
    }

    return field;
}