1   package debugger;
2   
3   import java.lang.reflect.Field;
4   
5   /**
6    * Copyright (c) Ontos AG (http://www.ontosearch.com).
7    * This class is part of JAPE Debugger component for
8    * GATE (Copyright (c) "The University of Sheffield" see http://gate.ac.uk/) <br>
9    * Contains helper static methods to access private class fields
10   * @author Andrey Shafirin
11   */
12  
13  public class ClassRipper {
14      private static int DebugLevel = 4;
15  
16      private ClassRipper() {
17      }
18  
19      /** Obtains field from given class instance
20       * regardless either this field private or not
21       * @param classInstance class instance which value to extract
22       * @param fieldName field name
23       * @return fileld
24       * */
25      private static Field getField(Object classInstance, String fieldName) {
26          if (DebugLevel >= 5) {
27              System.out.print("DEBUG [" + ClassRipper.class.getName() + "]:");
28              System.out.print(" getField: ");
29              System.out.print(" classInstance = [" + classInstance.getClass().getName() + "]");
30              System.out.print(" fieldName = [" + fieldName + "]");
31              System.out.print("\n");
32          }
33          Field field = null;
34          Class cls = classInstance.getClass();
35          while (!cls.getName().equals("java.lang.Object")) {
36              try {
37                  field = cls.getDeclaredField(fieldName);
38                  field.setAccessible(true);
39                  return field;
40              } catch (NoSuchFieldException e) {
41                  // will try to search in superclass
42                  //cls = classInstance.getClass().getSuperclass();
43                  cls = cls.getSuperclass();
44              } catch (SecurityException e) {
45                  e.printStackTrace();
46              }
47          }
48          return null;
49      }
50  
51      /** Obtains field value from given class instance
52       * regardless either this field private or not
53       * @param classInstance class instance which value to extract
54       * @param fieldName field name
55       * @return fileld value
56       * */
57      public static Object getFieldValue(Object classInstance, String fieldName) throws IllegalAccessException {
58          Field field = getField(classInstance, fieldName);
59          Object obj = field.get(classInstance);
60          field.setAccessible(false);
61          return obj;
62      }
63  
64      public static boolean getBoolean(Object classInstance, String fieldName) throws IllegalAccessException {
65          Field field = getField(classInstance, fieldName);
66          boolean b = field.getBoolean(classInstance);
67          field.setAccessible(false);
68          return b;
69      }
70  
71      public static byte getByte(Object classInstance, String fieldName) throws IllegalAccessException {
72          Field field = getField(classInstance, fieldName);
73          byte b = field.getByte(classInstance);
74          field.setAccessible(false);
75          return b;
76      }
77  
78      public static char getChar(Object classInstance, String fieldName) throws IllegalAccessException {
79          Field field = getField(classInstance, fieldName);
80          char c = field.getChar(classInstance);
81          field.setAccessible(false);
82          return c;
83      }
84  
85      public static int getInt(Object classInstance, String fieldName) throws IllegalAccessException {
86          Field field = getField(classInstance, fieldName);
87          int i = field.getInt(classInstance);
88          field.setAccessible(false);
89          return i;
90      }
91  
92      public static float getFloat(Object classInstance, String fieldName) throws IllegalAccessException {
93          Field field = getField(classInstance, fieldName);
94          float f = field.getFloat(classInstance);
95          field.setAccessible(false);
96          return f;
97      }
98  
99      public static long getLong(Object classInstance, String fieldName) throws IllegalAccessException {
100         Field field = getField(classInstance, fieldName);
101         long l = field.getLong(classInstance);
102         field.setAccessible(false);
103         return l;
104     }
105 
106     public static double getDouble(Object classInstance, String fieldName) throws IllegalAccessException {
107         Field field = getField(classInstance, fieldName);
108         double d = field.getDouble(classInstance);
109         field.setAccessible(false);
110         return d;
111     }
112 }
113