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:Main.java

public static void main(String[] argv) throws Exception {
    Class cls = java.awt.Point.class;

    Field[] fields = cls.getDeclaredFields();
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.isSynthetic());

    }//  w  w  w.j a va  2s.c  om
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.isEnumConstant());

    }/*from  ww  w .  ja v  a2s. c om*/
}

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.getDeclaringClass());

    }//from ww  w  . j a  va  2  s.  c o m
}

From source file:Main.java

public static void main(String[] args) throws IllegalAccessException {
    Class clazz = Color.class;
    Field[] colorFields = clazz.getDeclaredFields();

    HashMap<String, Color> singleColors = new HashMap<String, Color>();
    for (Field cf : colorFields) {
        int modifiers = cf.getModifiers();
        if (!Modifier.isPublic(modifiers))
            continue;

        Color c = (Color) cf.get(null);
        if (!singleColors.values().contains(c))
            singleColors.put(cf.getName(), c);
    }/*from www  .  ja  v  a  2  s . c o  m*/

    for (String k : singleColors.keySet()) {
        System.out.println(k + ": " + singleColors.get(k));
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("MyClass");
    System.out.println("\nFields:");
    Field fields[] = c.getDeclaredFields();
    for (Field fld : fields)
        System.out.println(" " + fld);
}

From source file:ArrayFind.java

public static void main(String... args) {
    boolean found = false;
    try {/*from  w  w  w.  j a v a 2s .  co  m*/
        Class<?> cls = Class.forName(args[0]);
        Field[] flds = cls.getDeclaredFields();
        for (Field f : flds) {
            Class<?> c = f.getType();
            if (c.isArray()) {
                found = true;
                out.format(
                        "%s%n" + "           Field: %s%n" + "            Type: %s%n" + "  Component Type: %s%n",
                        f, f.getName(), c, c.getComponentType());
            }
        }
        if (!found) {
            out.format("No array fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    try {/*from  w w w  .  ja v a2  s  . co m*/
        MyClass c = new MyClass();
        Class cls = c.getClass();

        // returns the array of Field objects
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println("Field = " + fields[i].toString());
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String names[] = { "ArrayList", "Arrays", "BitSet", "Collection", "Collections", "Dictionary",
            "Enumeration", "HashMap", "HashSet", "WeakHashMap" };

    for (int i = 0, n = names.length; i < n; i++) {
        String className = "java.util." + names[i];
        Class theClass = Class.forName(className);
        Field fields[] = theClass.getDeclaredFields();
        for (int j = 0, m = fields.length; j < m; j++) {
            System.out.println(names[i] + ", " + fields[j].getName() + ", " + fields[j].getType().getName()
                    + ", " + Modifier.toString(fields[j].getModifiers()));
        }//w ww  .ja v a 2s.c  om
    }
}

From source file:AllFieldsSnippet.java

public static void main(String[] args) {
    Object obj = new Object();
    //start extract AllFieldsSnippet

    Class cls = obj.getClass();
    List accum = new LinkedList();
    while (cls != null) {
        Field[] f = cls.getDeclaredFields();
        for (int i = 0; i < f.length; i++) {
            accum.add(f[i]);/*  w  ww.  jav a 2  s.co  m*/
        }
        cls = cls.getSuperclass();
    }
    Field[] allFields = (Field[]) accum.toArray(new Field[accum.size()]);
    //stop extract AllFieldsSnippet
}