Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Field;
- public class Eval {
- public final int a = 1;
- @SuppressWarnings("unused")
- private final String b = "abc";
- @SuppressWarnings("unused")
- private final int c = 100;
- @SuppressWarnings("unused")
- private final String d = "def";
- public static void main(String[] args) {
- Eval eval = new Eval();
- for (Field f : eval.getClass().getDeclaredFields()) {
- if (f.getType().equals(int.class)) {
- int val = 0;
- try {
- val = f.getInt(eval);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- e.printStackTrace();
- }
- System.out.println(f.getName() + " = " + val);
- } else if (f.getType().equals(String.class)) {
- String val = "";
- try {
- val = (String) f.get(eval);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- e.printStackTrace();
- }
- System.out.println(f.getName() + " = " + val);
- }
- }
- Field g = null;
- try {
- g = eval.getClass().getField("a");
- } catch (NoSuchFieldException | SecurityException e) {
- e.printStackTrace();
- }
- int v = 0;
- try {
- v = g.getInt(eval);
- } catch (IllegalArgumentException | IllegalAccessException e) {
- e.printStackTrace();
- }
- System.out.println(g.getName() + " = " + v);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement