/Users/lyon/j4p/src/net/rmi/rmiSynth/lex/Reflector.java

1    /** 
2     * Class Reflector gets class members (methods,fields,etc) from a class instance 
3     * @author Roman Yedokov 
4     */ 
5     
6    package net.rmi.rmiSynth.lex; 
7     
8    import java.lang.reflect.Field; 
9    import java.lang.reflect.Member; 
10   import java.lang.reflect.Method; 
11   import java.lang.reflect.Modifier; 
12    
13   public class Reflector { 
14       private Class c; 
15       private Object o; 
16    
17       /** 
18        * Constructor 
19        */ 
20       public Reflector(Object _o) { 
21           o = _o; 
22           c = o.getClass(); 
23       } 
24    
25       //********************* Class *************** 
26       /** 
27        * Gets class visibility 
28        * 
29        * @param cls Class 
30        * @return v Visibility 
31        */ 
32       public int getVisibility(Class cls) { 
33           int v = 0;                  //Default 
34           int m = cls.getModifiers(); 
35           if (Modifier.isPublic(m)) { 
36               v = 1; 
37           } 
38           if (Modifier.isPrivate(m)) { 
39               v = 2; 
40           } 
41           if (Modifier.isProtected(m)) { 
42               v = 3; 
43           } 
44           return v; 
45       } 
46    
47       /** 
48        * Gets class modifiers 
49        * 
50        * @return lmd modifiers 
51        */ 
52       public LexModif getModifiers() { 
53           LexModif lmd = new LexModif(); 
54           lmd.setVisibility(getVisibility(c)); 
55           return lmd; 
56       } 
57    
58       /** 
59        * Gets class name 
60        * 
61        * @return c.getName() Class name 
62        */ 
63       public String getName() { 
64           return c.getName(); 
65       } 
66    
67       //**** Modifiers and name for fields and methods ********* 
68       /** 
69        * Gets member visibility 
70        * 
71        * @param mem Member 
72        * @return v Visibility 
73        */ 
74       public int getVisibility(Member mem) { 
75           int v = 0;                  //Default 
76           int m = mem.getModifiers(); 
77           if (Modifier.isPublic(m)) { 
78               v = 1; 
79           } 
80           if (Modifier.isPrivate(m)) { 
81               v = 2; 
82           } 
83           if (Modifier.isProtected(m)) { 
84               v = 3; 
85           } 
86           return v; 
87       } 
88    
89       /** 
90        * Gets member modifiers 
91        * 
92        * @param mem Member 
93        * @return lmd modifiers 
94        */ 
95       public LexModif getModifiers(Member mem) { 
96           LexModif lmd = new LexModif(); 
97           lmd.setVisibility(getVisibility(mem)); 
98           return lmd; 
99       } 
100   
101      /** 
102       * Gets member name 
103       * 
104       * @return mem.getName() Member name 
105       */ 
106      public String getName(Member mem) { 
107          return mem.getName(); 
108      } 
109   
110      //***************** Fields ******************* 
111      /** 
112       * Gets field type 
113       * 
114       * @param fld Field 
115       * @return ltp Field type 
116       */ 
117      public LexType getType(Field fld) { 
118          Class cls = fld.getType(); 
119          LexType ltp = new LexType(); 
120          ltp.setName(removeGarbage(cls.getName())); 
121          ltp.setArray(cls.isArray()); 
122          return ltp; 
123      } 
124   
125      /** 
126       * Gets all fields 
127       * 
128       * @return lfd Fields 
129       */ 
130      public LexField[] getFields() { 
131          Field[] fld = c.getDeclaredFields(); 
132          LexField[] lfd = new LexField[fld.length]; 
133          String name; 
134          for (int i = 0; i < lfd.length; i++) { 
135              lfd[i] = new LexField(); 
136              lfd[i].setModif(getModifiers(fld[i])); 
137              lfd[i].setType(getType(fld[i])); 
138              lfd[i].setName(getName(fld[i])); 
139          } 
140          return lfd; 
141      } 
142   
143      //********************* Methods ******************** 
144      /** 
145       * Gets method return type 
146       * 
147       * @param mtd Method 
148       * @return ltp Method return type 
149       */ 
150      public LexType getType(Method mtd) { 
151          Class cls = mtd.getReturnType(); 
152          LexType ltp = new LexType(); 
153          ltp.setName(removeGarbage(cls.getName())); 
154          ltp.setArray(cls.isArray()); 
155          return ltp; 
156      } 
157   
158      /** 
159       * Gets all methods 
160       * 
161       * @return lmd Methods 
162       */ 
163      public LexMethod[] getMethods() { 
164          Method[] mtd = c.getDeclaredMethods(); 
165          LexMethod[] lmd = new LexMethod[mtd.length]; 
166          for (int i = 0; i < lmd.length; i++) { 
167              lmd[i] = new LexMethod(); 
168              lmd[i].setModif(getModifiers(mtd[i])); 
169              lmd[i].setType(getType(mtd[i])); 
170              lmd[i].setName(getName(mtd[i])); 
171              lmd[i].setParams(getParams(mtd[i])); 
172              lmd[i].getBody().setDelegObj("v"); 
173              lmd[i].getBody().setName( 
174                      lmd[i].getName()); 
175              lmd[i].getBody().setRet( 
176                      !lmd[i].getType().isVoid()); 
177          } 
178          return lmd; 
179      } 
180   
181      //****************** Parameters ***************** 
182      /** 
183       * Gets parameter type 
184       * 
185       * @param prm Parameter 
186       * @return ltp Parameter type 
187       */ 
188      public LexType getType(Class prm) { 
189          LexType ltp = new LexType(); 
190          ltp.setName(removeGarbage(prm.getName())); 
191          ltp.setArray(prm.isArray()); 
192          return ltp; 
193      } 
194   
195      /** 
196       * Gets all parameters 
197       * 
198       * @param mtd Method 
199       * @return lpm Parameters 
200       */ 
201      public LexParam[] getParams(Method mtd) { 
202          Class pmt[] = mtd.getParameterTypes(); 
203          LexParam[] lpm = new LexParam[pmt.length]; 
204          for (int i = 0; i < lpm.length; i++) { 
205              lpm[i] = new LexParam(); 
206              lpm[i].setType(getType(pmt[i])); 
207              lpm[i].setName("p" + i); 
208          } 
209          return lpm; 
210      } 
211   
212      /** 
213       * Removes garabage from name 
214       * 
215       * @param _s String to process 
216       * @return s 
217       */ 
218      public String removeGarbage(String _s) { 
219          String s = _s; 
220          if (s.charAt(0) == '[' && 
221              s.charAt(s.length() - 1) == ';') { 
222              s = s.substring(2, s.length() - 1) + 
223                  "[]"; 
224          } 
225          return s; 
226      } 
227  } 
228