Example usage for java.lang Class getFields

List of usage examples for java.lang Class getFields

Introduction

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

Prototype

@CallerSensitive
public Field[] getFields() throws SecurityException 

Source Link

Document

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

Usage

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];/*from w ww  . jav a2  s  . com*/
        field.setAccessible(true);
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }
}

From source file:ReflectUtils.java

/**
 * Adds all Fields (from Class.getFields) to the list
 * @param aClass//from w w  w  .  j  av a  2 s .  co  m
 * @param list
 * @return number of fields added
 */
public static int addFields(Class aClass, List<Member> list) {
    Field[] fields = aClass.getFields();
    for (Field f : fields) {
        list.add(f);
    }
    return fields.length;
}

From source file:ReflectUtils.java

/**
 * Adds all static Fields (from Class.getFields) to the list
 * @param aClass/*from  w w  w . ja v  a2 s.  c om*/
 * @param list
 * @return number of fields added
 */
public static int addStaticFields(Class aClass, List<Member> list) {
    Field[] fields = aClass.getFields();
    for (Field f : fields) {
        if (Modifier.isStatic(f.getModifiers())) {
            list.add(f);
        }
    }
    return fields.length;
}

From source file:net.eledge.android.toolkit.db.internal.SQLBuilder.java

public static String getIdField(Class<?> clazz) {
    for (Field field : clazz.getFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            Column column = field.getAnnotation(Column.class);
            return StringUtils.defaultIfBlank(column.name(), field.getName().toLowerCase(Locale.ENGLISH));
        }//from www  .j  a  va  2 s . c o m
    }
    return null;
}

From source file:Main.java

public static String getIDName(View view, Class<?> clazz) {

    try {/*  w  ww.  ja  v  a 2s  .c  om*/
        Integer id = view.getId();
        Field[] ids = clazz.getFields();
        for (int i = 0; i < ids.length; i++) {
            Object val = ids[i].get(null);
            if (val != null && val instanceof Integer && ((Integer) val).intValue() == id.intValue()) {
                return ids[i].getName();
            }
        }
    } catch (Exception e) {
    }
    return "";
}

From source file:Main.java

public static void printAllInform(Class clsShow) {
    try {/*from w w  w  .j  a  v a2 s  .  c o  m*/
        int i;
        for (i = 0; i < clsShow.getMethods().length; i++) {
        }
        for (i = 0; i < clsShow.getFields().length; i++) {
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e2) {
        e2.printStackTrace();
    } catch (Exception e3) {
        e3.printStackTrace();
    }
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {/*from   ww w  .j a v a 2 s  .  co  m*/
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * a helper to get the string fields from the R class
 *
 * @param ctx//from w  w w. j  av  a2 s.  c  o m
 * @return
 */
public static String[] getFields(Context ctx) {
    Class rStringClass = resolveRClass(ctx.getPackageName());
    if (rStringClass != null) {
        return getDefinedFonts(ctx, rStringClass.getFields());
    }
    return new String[0];
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.CommandSetTest.java

private static ImmutableList<CommandSet.Entry> getAvailableCommands() throws IllegalAccessException {
    Class<CommandSet> commandSetClass = CommandSet.class;

    Field[] declaredFields = commandSetClass.getFields();

    ImmutableList.Builder<CommandSet.Entry> builder = ImmutableList.builder();

    for (Field field : declaredFields) {
        if (field.getType().isAssignableFrom(CommandSet.Entry.class)) {
            builder.add((CommandSet.Entry) field.get(null));
        }//from  ww  w  .  j  a va  2s  .  co  m
    }

    return builder.build();
}

From source file:Capabilities.java

/** Extract the names of all the SET capabilities in the object.
* The names (Strings) are appended to the arrayList
* @param obj The object for which to extract the capability strings
* @param capabilityStrings The ArrayList to which the capability names will be appended
*//*from ww  w  . ja  va 2 s.  co m*/
public static void getCapabilities(javax.media.j3d.SceneGraphObject obj,
        java.util.ArrayList capabilityStrings) {

    int value;
    String str;
    Class cl = obj.getClass();

    java.lang.reflect.Field[] fields = cl.getFields();

    try {
        for (int i = 0; i < fields.length; i++) {
            str = fields[i].getName();
            value = fields[i].getInt(fields[i]);
            if (str.indexOf("ALLOW") != -1 || str.indexOf("ENABLE_") != -1) {
                if (obj.getCapability(value))
                    capabilityStrings.add(str);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Internal Error");
    }
}