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 main(String[] argv) throws Exception {
    Class cls = java.awt.Point.class;

    Field[] fields = cls.getFields();
    for (int i = 0; i < fields.length; i++) {
        Class type = fields[i].getType();
        System.out.println(fields[i]);
    }/*from  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.getFields();
    for (Field f : flds) {
        Annotation[] annos = f.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println(anno.toString());
        }//  w w  w. j a v  a 2s . co m

    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("java.awt.Dimension");

    Field fields[] = c.getFields();
    for (int i = 0; i < fields.length; i++) {
        System.out.println(fields[i].toGenericString());
    }//from w  ww.  j av  a2s . com
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("java.awt.Dimension");

    Field fields[] = c.getFields();
    for (int i = 0; i < fields.length; i++) {
        System.out.println(fields[i].toString());
    }/*from w w  w. j av  a 2 s.c o  m*/
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("java.awt.Dimension");

    Field fields[] = c.getFields();
    for (int i = 0; i < fields.length; i++) {
        System.out.println(fields[i].hashCode());
    }/*  ww w  . j a  v a  2 s.c o  m*/
}

From source file:DataBeanTest.java

public static void main(String[] args) {
    Class d = DataBean.class;

    Field fs[] = d.getFields();
    for (Field f : fs) {
        System.out.println(f);/*from ww  w  .ja  va 2 s .co  m*/

        Annotation a = f.getAnnotation(DataField.class);

        if (a != null) {
            System.out.println(f.getName());
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];

    field.setAccessible(true);// w w w  . j ava  2 s  . c o  m
    constructor.setAccessible(true);
    method.setAccessible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;//from   w ww.j a  v  a  2s .c  om

    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;// w  w  w  .  j  a va2 s.  co  m

    name = cls.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = field.getName();
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = method.getName();
    System.out.println(name);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    GetFields object = new GetFields();
    Class clazz = object.getClass();

    // Get all object accessible public fields.
    Field[] fields = clazz.getFields();
    System.out.println("Number of fields = " + fields.length);
    for (Field field : fields) {
        System.out.println("Field name = " + field.getName());
        System.out.println("Field type = " + field.getType().getName());
    }//  w w w  .ja  v a  2 s  .  com

    Field field = clazz.getField("id");
    System.out.println("Field name = " + field.getName());
    System.out.println("Field type = " + field.getType().getName());
}