Example usage for java.lang Class getField

List of usage examples for java.lang Class getField

Introduction

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

Prototype

@CallerSensitive
public Field getField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getBoolean(x));

    f.setBoolean(x, false);// w  w  w.  ja  v  a  2 s  .  c o m
    System.out.println(f.getBoolean(x));

}

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getFloat(x));

    f.setFloat(x, 9.99F);/*from  w  w  w.  j  a v a  2 s.co m*/
    System.out.println(f.getFloat(x));

}

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getLong(x));

    f.setLong(x, 9L);/*from  w  w  w  .  ja  va2s. c o m*/
    System.out.println(f.getLong(x));

}

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getDouble(x));

    f.setDouble(x, 9.99);/*from   www.j a v a2 s.c  om*/
    System.out.println(f.getDouble(x));

}

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getChar(x));

    f.setChar(x, 'a');
    System.out.println(f.getBoolean(x));

}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> ppClass = MyClass.class;
    try {/*from w ww . j  av a  2  s  .  co m*/
        MyClass p = ppClass.newInstance();
        Field name = ppClass.getField("name");
        String nameValue = (String) name.get(p);
        System.out.println("Current name is " + nameValue);
        name.set(p, "abc");
        nameValue = (String) name.get(p);
        System.out.println("New  name is " + nameValue);
    } catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
            | IllegalArgumentException e) {
        System.out.println(e.getMessage());
    }
}

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());
    }//from  w w  w. j  a v  a2 s .c  o  m

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

From source file:at.tuwien.ifs.somtoolbox.doc.RunnablesReferenceCreator.java

public static void main(String[] args) {
    ArrayList<Class<? extends SOMToolboxApp>> runnables = SubClassFinder.findSubclassesOf(SOMToolboxApp.class,
            true);//from   w w  w .  j a  v a 2s.  c  o m
    Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR);

    StringBuilder sbIndex = new StringBuilder(runnables.size() * 50);
    StringBuilder sbDetails = new StringBuilder(runnables.size() * 200);

    sbIndex.append("\n<table border=\"0\">\n");

    Type lastType = null;

    for (Class<? extends SOMToolboxApp> c : runnables) {
        try {
            // Ignore abstract classes and interfaces
            if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
                continue;
            }

            Type type = Type.getType(c);
            if (type != lastType) {
                sbIndex.append("  <tr> <td colspan=\"2\"> <h5> " + type + " Applications </h5> </td> </tr>\n");
                sbDetails.append("<h2> " + type + " Applications </h2>\n");
                lastType = type;
            }
            String descr = "N/A";
            try {
                descr = (String) c.getDeclaredField("DESCRIPTION").get(null);
            } catch (Exception e) {
            }
            String longDescr = "descr";
            try {
                longDescr = (String) c.getDeclaredField("LONG_DESCRIPTION").get(null);
            } catch (Exception e) {
            }

            sbIndex.append("  <tr>\n");
            sbIndex.append("    <td> <a href=\"#").append(c.getSimpleName()).append("\">")
                    .append(c.getSimpleName()).append("</a> </td>\n");
            sbIndex.append("    <td> ").append(descr).append(" </td>\n");
            sbIndex.append("  </tr>\n");

            sbDetails.append("<h3 id=\"").append(c.getSimpleName()).append("\">").append(c.getSimpleName())
                    .append("</h3>\n");
            sbDetails.append("<p>").append(longDescr).append("</p>\n");

            try {
                Parameter[] options = (Parameter[]) c.getField("OPTIONS").get(null);
                JSAP jsap = AbstractOptionFactory.registerOptions(options);
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(os);
                AbstractOptionFactory.printHelp(jsap, c.getName(), ps);
                sbDetails.append("<pre>").append(StringEscapeUtils.escapeHtml(os.toString())).append("</pre>");
            } catch (Exception e1) { // we didn't find the options => let the class be invoked ...
            }

        } catch (SecurityException e) {
            // Should not happen - no Security
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
    sbIndex.append("</table>\n\n");
    System.out.println(sbIndex);
    System.out.println(sbDetails);
}

From source file:SampleSet.java

static void modifyWidth(Rectangle r, Integer widthParam) {
    Field widthField;/*from  w ww  . ja  v a  2s. c o  m*/
    Integer widthValue;
    Class c = r.getClass();
    try {
        widthField = c.getField("width");
        widthField.set(r, widthParam);
    } catch (NoSuchFieldException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    }
}

From source file:SampleGet.java

static void printHeight(Rectangle r) {
    Field heightField;/*from  w  w  w.j  a v  a  2 s  .c  o  m*/
    Integer heightValue;
    Class c = r.getClass();
    try {
        heightField = c.getField("height");
        heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (NoSuchFieldException e) {
        System.out.println(e);
    } catch (SecurityException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    }
}