Example usage for java.lang.reflect Field get

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

Introduction

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

Prototype

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

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:Main.java

public static void main(String[] st) throws Exception {
    Private p = new Private();
    Field[] fs = p.getClass().getDeclaredFields();
    for (Field f : fs) {
        f.setAccessible(true);//from ww w  .  j  av a  2s . c  o  m
        System.out.println(f.get(p));
    }
}

From source file:Main.java

public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();//from   w  ww.  j  av  a 2 s  .c  om
    try {
        Field heightField = c.getField("height");
        Integer 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);
    }
}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> ppClass = MyClass.class;
    try {/* w w w .  ja  va2 s  . com*/
        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 {
    Bean demo = new Bean();
    Class clazz = demo.getClass();

    Field field = clazz.getField("id");
    field.set(demo, new Long(10));
    Object value = field.get(demo);
    System.out.println("Value = " + value);

    field = clazz.getField("now");
    field.set(null, new Date());
    value = field.get(null);//from w  w  w .ja v a2 s. co m
    System.out.println("Value = " + value);
}

From source file:com.music.tools.InstrumentExtractor.java

public static void main(String[] args) {
    Map<Integer, String> instrumentNames = new HashMap<>();
    Field[] fields = ProgramChanges.class.getDeclaredFields();
    try {/*  ww  w .j  a  v a 2 s.c  o  m*/
        for (Field field : fields) {
            Integer value = (Integer) field.get(null);
            if (!instrumentNames.containsKey(value)) {
                instrumentNames.put(value,
                        StringUtils.capitalize(field.getName().toLowerCase()).replace('_', ' '));
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }

    Score score = new Score();
    Read.midi(score, "C:\\Users\\bozho\\Downloads\\7938.midi");
    for (Part part : score.getPartArray()) {
        System.out.println(part.getChannel() + " : " + part.getInstrument() + ": "
                + instrumentNames.get(part.getInstrument()));
    }
}

From source file:Main.java

public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();/*from  ww  w. ja va2s .c om*/
    try {
        Field heightField = c.getField("height");
        heightField.setInt(r, 1000);
        Integer heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:MyClass.java

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

From source file:TraceLevel.java

public static void main(String... args) {
    TraceLevel newLevel = TraceLevel.valueOf(args[0]);

    try {//from   w w  w.  j a  v a2 s. co m
        MyServer svr = new MyServer();
        Class<?> c = svr.getClass();
        Field f = c.getDeclaredField("level");
        f.setAccessible(true);
        TraceLevel oldLevel = (TraceLevel) f.get(svr);
        out.format("Original trace level:  %s%n", oldLevel);

        if (oldLevel != newLevel) {
            f.set(svr, newLevel);
            out.format("    New  trace level:  %s%n", f.get(svr));
        }

        // production code should handle these exceptions more gracefully
    } catch (IllegalArgumentException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}

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);
    }//w  w w . jav  a2s  . co  m

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

From source file:Main.java

public static void main(String... args) {
    Constructor[] ctors = Console.class.getDeclaredConstructors();
    Constructor ctor = null;/*from  ww  w  .j  a v a 2s  .c  o m*/
    for (int i = 0; i < ctors.length; i++) {
        ctor = ctors[i];
        if (ctor.getGenericParameterTypes().length == 0)
            break;
    }

    try {
        ctor.setAccessible(true);
        Console c = (Console) ctor.newInstance();
        Field f = c.getClass().getDeclaredField("cs");
        f.setAccessible(true);
        out.format("Console charset         :  %s%n", f.get(c));
        out.format("Charset.defaultCharset():  %s%n", Charset.defaultCharset());

        // production code should handle these exceptions more gracefully
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    }
}