Example usage for java.lang.reflect Field getInt

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

Usage

From source file:X.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("X");
    X x = (X) clazz.newInstance();//from  w ww. java  2s.co m
    Field f = clazz.getField("i");
    System.out.println(f.getInt(x)); // Output: 10
    f.setInt(x, 20);
    System.out.println(f.getInt(x)); // Output: 20
    f = clazz.getField("PI");
    System.out.println(f.getDouble(null)); // Output: 3.14
    f.setDouble(x, 20);
}

From source file:Main.java

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

    Field field = cls.getField("x");

    // Get value//from   w  w  w  .  j a  v  a 2 s .  c  o  m
    //    field.getInt(object);

    // Set value
    //  field.setInt(object, 123);

    // Get value of a static field
    field.getInt(null);

    // Set value of a static field
    field.setInt(null, 123);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object clazz = new TestClass();
    String lookingForValue = "firstValue";

    Field field = clazz.getClass().getField(lookingForValue);
    Class clazzType = field.getType();
    if (clazzType.toString().equals("double"))
        System.out.println(field.getDouble(clazz));
    else if (clazzType.toString().equals("int"))
        System.out.println(field.getInt(clazz));

    //System.out.println(field.get(clazz));
}

From source file:Main.java

public static int getStaticFieldIntSafely(Field field) {
    try {/* w w w .  j  a  va 2s .co  m*/
        return field.getInt(null);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static int getResId(String resName, Class<?> c) {
    try {/*from   www . j a  va 2  s.  c o m*/
        Field idField = c.getField(resName);
        return idField.getInt(null);
    } catch (Exception e) {
        return -1;
    }
}

From source file:RobotUtilities.java

public static void sendKeysCombo(String keys[]) {
    try {//from w  w  w .ja  v a  2 s. c o m

        Robot robot = new Robot();

        Class<?> cl = KeyEvent.class;

        int[] intKeys = new int[keys.length];

        for (int i = 0; i < keys.length; i++) {
            Field field = cl.getDeclaredField(keys[i]);
            intKeys[i] = field.getInt(field);
            robot.keyPress(intKeys[i]);
        }

        for (int i = keys.length - 1; i >= 0; i--)
            robot.keyRelease(intKeys[i]);
    } catch (Throwable e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static int getResId(String name, Class<?> c) {
    try {/* w w w  .  j av a 2 s  .c om*/
        Field idField = c.getDeclaredField(name);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static int getId(String resourceName, Class<?> c) {
    try {/*from w ww . j  av a  2s.  c o m*/
        Field idField = c.getDeclaredField(resourceName);
        return idField.getInt(idField);
    } catch (Exception e) {
        throw new RuntimeException("No resource ID found for: " + resourceName + " / " + c, e);
    }
}

From source file:Main.java

public static int getResId(String resName, Class<?> c) {

    try {/*  w  w  w.j  a  v  a2s.c  o  m*/
        Field idField = c.getDeclaredField(resName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }

}

From source file:Main.java

public static int getResId(String variableName, Class<?> c) {

    try {//from ww w  .  j a  va 2s . co m
        Field idField = c.getDeclaredField(variableName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}