Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:com.github.sumimakito.quickkv.util.DataProcessor.java

public static void printFieldsOfObject(Object object) {
    System.out.println("[Fields of Object: " + object.getClass().getSimpleName() + "]");
    for (Field field : object.getClass().getDeclaredFields()) {
        field.setAccessible(true); // You might want to set modifier to public first.
        Object value = null;/*from  w  w  w.j av  a2s.co  m*/
        try {
            value = field.get(object);
            if (value != null) {
                System.out.println("- (" + value.getClass().getSimpleName() + ")[" + field.getName() + "]=["
                        + value + "]");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.mit.csail.sdg.alloy4.Terminal.java

private static A4Options.SatSolver getSolver(String name) {
    if (getAvaliableSatSolvers().contains(name)) {
        for (Field f : A4Options.SatSolver.class.getFields()) {
            if (f.getName().equals(name) && A4Options.SatSolver.class.isAssignableFrom(f.getType())) {
                try {
                    return (A4Options.SatSolver) f.get(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }//from w  w w .j a  va  2  s  . c  o m
            }
        }
    }
    return null;
}

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 {//w ww . ja  va2 s  .c  o m
        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:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java

/**
 * Utility method for retrieving a string representation of the value referenced by a JAXB generated object.
 * <p/>/*  w  w w .  j ava  2 s  .  c o m*/
 * <p/>
 * This method assumes that <code>value</code> is a valid declared field of the object being passed to it,
 * and also contains the corresponding read method <code>getValue()</code>.
 * <p/>
 * <p/>
 * <b>This method will work with other non-JAXB generated objects that meet the requirements stated above,
 * but it is not recommended.</b>
 *
 * @param valueObject - an object that represents a JAXB generated value object
 * @return a string representation of <code>Object.getValue()</code>
 */
public static String getJAXBObjectValue(final Object valueObject) {

    String value = null;
    if (valueObject != null) {
        try {
            final Field valueField = valueObject.getClass().getDeclaredField("value");
            final PropertyDescriptor valueObjectDescriptor = new PropertyDescriptor(valueField.getName(),
                    valueObject.getClass());
            value = valueObjectDescriptor.getReadMethod().invoke(valueObject, null).toString();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return value;
        }
    }

    return value;
}

From source file:Main.java

/**
 * Get a list of the name of variables of the class received
 * @param klass Class to get the variable names
 * @return List<String>[] of length 3 with variable names: [0]: primitives, [1]: arrays, [2]: objects
 *//*  ww  w.j  av a 2 s .  c o  m*/
public static List<String>[] getVariableNames(Class klass) {

    //array to return
    List<String>[] varNames = new List[3];
    for (int i = 0; i < 3; i++) {
        varNames[i] = new ArrayList<>();
    }

    //add all valid fields
    do {
        Field[] fields = klass.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isTransient(field.getModifiers())) {

                //get the type
                Class type = field.getType();

                if (type.isPrimitive() || (type == Integer.class) || (type == Float.class)
                        || (type == Double.class) || (type == Boolean.class) || (type == String.class)) {
                    varNames[0].add(field.getName());
                } else if (type.isArray()) {
                    varNames[1].add(field.getName());
                } else {
                    varNames[2].add(field.getName());
                }
            }
        }

        klass = klass.getSuperclass();
    } while (klass != null);

    //return array
    return varNames;

}

From source file:com.smartitengineering.util.bean.BeanFactoryRegistrar.java

private static boolean aggregate(Class<? extends Object> aggregatorClass, Object aggregator)
        throws SecurityException {
    if (aggregatorClass.equals(Object.class)) {
        return true;
    }//from w ww  . j ava2 s. c  o  m
    Class<? extends Object> superClass = aggregatorClass.getSuperclass();
    if (superClass != null) {
        aggregate(superClass, aggregator);
    }
    Aggregator aggregatorAnnotation = aggregatorClass.getAnnotation(Aggregator.class);
    if (aggregatorAnnotation == null || StringUtils.isBlank(aggregatorAnnotation.contextName())) {
        return true;
    }
    BeanFactory beanFactory = getBeanFactorForContext(aggregatorAnnotation.contextName());
    if (beanFactory == null) {
        return true;
    }
    Field[] declaredFields = aggregatorClass.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        InjectableField injectableField = declaredField.getAnnotation(InjectableField.class);
        if (injectableField == null) {
            continue;
        }
        String beanName = StringUtils.isBlank(injectableField.beanName()) && beanFactory.isNameMandatory()
                ? declaredField.getName()
                : injectableField.beanName();
        if (StringUtils.isBlank(beanName) && beanFactory.isNameMandatory()) {
            return true;
        }
        try {
            declaredField.setAccessible(true);
            final Class<?> fieldType = declaredField.getType();
            if (beanFactory.containsBean(beanName, fieldType)) {
                declaredField.set(aggregator, beanFactory.getBean(beanName, fieldType));
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

    }
    return false;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Field findField(Class<?> clazz, String propName) {
    Class<?> c = clazz;/*from  w w w. j av  a  2 s.  c o m*/
    while (c != null) {
        for (Field f : c.getDeclaredFields()) {
            if (!f.getName().equals(propName)) {
                continue;
            }
            int modifiers = f.getModifiers();
            if (Modifier.isStatic(modifiers)) {
                continue;
            }
            return f;
        }
        c = c.getSuperclass();
    }
    return null;
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)//from  ww  w . j a  v a  2 s .co  m
 * 
 * @param clazz
 *            class
 * @param containSuperClazz
 *            ??Class?
 * @return String[]
 */
@SuppressWarnings({ "rawtypes" })
public static String[] getFieldName(Class clazz, boolean containSuperClazz) {
    Set<String> cols = new LinkedHashSet<String>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        for (Field f : fields) {
            if ("serialVersionUID".equals(f.getName()))
                continue;
            cols.add(f.getName());
        }
        searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null;
    }
    return cols.toArray(new String[cols.size()]);
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

/**
 * ?Clazz?(?super class)/*w  w w . j  ava 2  s .c  o  m*/
 * 
 * @param clazz
 *            class
 * @param containSuperClazz
 *            ??Class?
 * @return Field[]
 */
@SuppressWarnings({ "rawtypes" })
public static Field[] getField(Class clazz, boolean containSuperClazz) {
    Set<Field> cols = new LinkedHashSet<Field>();
    Class searchClazz = clazz;
    while (!Object.class.equals(searchClazz) && searchClazz != null) {
        Field[] fields = searchClazz.getDeclaredFields();
        for (Field f : fields) {
            if ("serialVersionUID".equals(f.getName()))
                continue;
            cols.add(f);
        }
        searchClazz = containSuperClazz ? searchClazz.getSuperclass() : null;
    }
    return cols.toArray(new Field[] {});
}

From source file:cn.whereyougo.framework.utils.PackageManagerUtil.java

/**
 * ??,json//from w w  w. j  av  a2 s .c  o m
 * 
 * @param ctx
 */
public static JSONObject collectDeviceInfo(Context ctx) {
    JSONObject result = new JSONObject();
    Field[] fields = Build.class.getDeclaredFields();
    for (Field field : fields) {
        try {
            field.setAccessible(true);
            result.put(field.getName(), field.get(null).toString());
        } catch (Exception e) {
            LogUtil.d(TAG, "an error occured when collect device info " + e.getMessage());
        }
    }

    return result;
}