Advertisement
cd62131

Eval

Jul 13th, 2014
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.37 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2.  
  3. public class Eval {
  4.   public final int a = 1;
  5.   @SuppressWarnings("unused")
  6.   private final String b = "abc";
  7.   @SuppressWarnings("unused")
  8.   private final int c = 100;
  9.   @SuppressWarnings("unused")
  10.   private final String d = "def";
  11.  
  12.   public static void main(String[] args) {
  13.     Eval eval = new Eval();
  14.     for (Field f : eval.getClass().getDeclaredFields()) {
  15.       if (f.getType().equals(int.class)) {
  16.         int val = 0;
  17.         try {
  18.           val = f.getInt(eval);
  19.         } catch (IllegalArgumentException | IllegalAccessException e) {
  20.           e.printStackTrace();
  21.         }
  22.         System.out.println(f.getName() + " = " + val);
  23.       } else if (f.getType().equals(String.class)) {
  24.         String val = "";
  25.         try {
  26.           val = (String) f.get(eval);
  27.         } catch (IllegalArgumentException | IllegalAccessException e) {
  28.           e.printStackTrace();
  29.         }
  30.         System.out.println(f.getName() + " = " + val);
  31.       }
  32.     }
  33.     Field g = null;
  34.     try {
  35.       g = eval.getClass().getField("a");
  36.     } catch (NoSuchFieldException | SecurityException e) {
  37.       e.printStackTrace();
  38.     }
  39.     int v = 0;
  40.     try {
  41.       v = g.getInt(eval);
  42.     } catch (IllegalArgumentException | IllegalAccessException e) {
  43.       e.printStackTrace();
  44.     }
  45.     System.out.println(g.getName() + " = " + v);
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement