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

1    /** 
2     * Class LexBase - string representation of a class/an interface 
3     * in the following layout: 
4     * package 
5     * import 
6     * modifier classname extends implements{ 
7     *  fields; 
8     *  methods; 
9     * } 
10    */ 
11    
12   package net.rmi.rmiSynth.lex; 
13    
14   public class LexBase extends LexStructure { 
15       private final String CLASS = "class"; 
16       private final String INTERFACE = "interface"; 
17       private final String EXT = "extends"; 
18       private final String IMP = "implements"; 
19       private final String IMPORT = "import"; 
20       private final String PACK = "package"; 
21       //private Object o;             //Object which class will be represented 
22       private boolean kind;           //Class (true) or interface (false) 
23       private Reflector ref; 
24       private StringVector packages;  //"package" clause 
25       private StringVector imports;   //"import" clauses 
26       private StringVector extend;    //"extends" clauses 
27       private StringVector implem;    //"implements" clauses 
28       private LexMethod[] methods;    //Methods of a class 
29       private LexField[] fields;      //Fields of a class 
30       private boolean hasConstructor; 
31       private LexMethod constr;       //constructor method 
32       private boolean hasMain; 
33       private LexMethod m;            //main method 
34    
35       /** 
36        * Constructor 
37        * 
38        * @param isInterface Class or interface 
39        * @param name Class/interface name 
40        * @param o    Object which will be hacked 
41        */ 
42       public LexBase(boolean isInterface, 
43                      String name, 
44                      Object o) { 
45           kind = isInterface; 
46           ref = new Reflector(o); 
47           if (isInterface) 
48               getType().setName(INTERFACE); 
49           else 
50               getType().setName(CLASS); 
51    
52           getType().setArray(false); 
53           setName(name); 
54           setModif(ref.getModifiers()); 
55           setFields(ref.getFields()); 
56           setMethods(ref.getMethods()); 
57           extend = new StringVector(); 
58           implem = new StringVector(); 
59           imports = new StringVector(); 
60           packages = new StringVector(); 
61           constr = new LexMethod(); 
62           hasConstructor = false; 
63           hasMain = false; 
64       } 
65    
66       /** 
67        * Returns true if class 
68        * 
69        * @return kind 
70        */ 
71       public boolean isClass() { 
72           return kind; 
73       } 
74    
75       /** 
76        * Returns true if interface 
77        * 
78        * @return !kind 
79        */ 
80       public boolean isInterface() { 
81           return !kind; 
82       } 
83    
84       /** 
85        * Adds package statement 
86        * 
87        * @param packName Package name 
88        */ 
89       public void addPackage(String packName) { 
90           packages.add(packName); 
91       } 
92    
93       /** 
94        * Returns package statement 
95        * 
96        * @return s Package name 
97        */ 
98       public String packToString() { 
99           String s = ""; 
100          for (int i = 0; i < packages.size(); i++) { 
101              s = s + PACK + " " + 
102                  packages.elementAt(i) + 
103                  ";\n"; 
104          } 
105          return s; 
106      } 
107   
108      /** 
109       * Adds import statement 
110       * 
111       * @param impPack Import name 
112       */ 
113      public void addImport(String impPack) { 
114          imports.add(impPack); 
115      } 
116   
117      /** 
118       * Returns import statement 
119       * 
120       * @return s Import names 
121       */ 
122      public String importToString() { 
123          String s = ""; 
124          for (int i = 0; i < imports.size(); i++) { 
125              s = s + IMPORT + " " + 
126                  imports.elementAt(i) + 
127                  ";\n"; 
128          } 
129          return s; 
130      } 
131   
132      /** 
133       * Adds extends/implements statement 
134       * 
135       * @param action    Extend or implement 
136       * @param className Inherited class 
137       */ 
138      public void addInherit(String action, 
139                             String className) { 
140          if (action.equals(EXT)) {   //extends 
141              extend.add(className); 
142          } 
143          if (action.equals(IMP)) {   //implements 
144              implem.add(className); 
145          } 
146      } 
147   
148      /** 
149       * Gets fields 
150       * 
151       * @return fileds 
152       */ 
153      public LexField[] getFields() { 
154          return fields; 
155      } 
156   
157      /** 
158       * Sets fields 
159       * 
160       * @param _fields Fields 
161       */ 
162      public void setFields(LexField[] _fields) { 
163          fields = _fields; 
164      } 
165   
166      /** 
167       * Gets methods 
168       * 
169       * @return methods 
170       */ 
171      public LexMethod[] getMethods() { 
172          return methods; 
173      } 
174   
175      /** 
176       * Sets methods 
177       * 
178       * @param _methods Methods 
179       */ 
180      public void setMethods(LexMethod[] _methods) { 
181          methods = _methods; 
182      } 
183   
184      /** 
185       * Sets constructor for output code 
186       * 
187       * @param exName Class name 
188       */ 
189      public void setConstr(String exName) { 
190          constr = new LexMethod(); 
191          constr.getModif().setVisibility(1);     //public 
192          constr.setName(this.getName());         //ClassName 
193          constr.getBody().setRet(false);         //{ 
194          constr.getBody().setDelegObj("");       //this. 
195          constr.getBody().setName("super");      //super(); 
196          hasConstructor = true;                  //} 
197      } 
198   
199      /** 
200       * Gets constructor for output code 
201       * 
202       * @return constr Constructor 
203       */ 
204      public LexMethod getConstr() { 
205          return constr; 
206      } 
207   
208      /** 
209       * Sets main method for output code 
210       * 
211       * @param code Main method code 
212       */ 
213      public void setMain(String code) { 
214          m = new LexMethod(); 
215          m.getModif().setVisibility(1);      //public 
216          m.getModif().setStatic(true);       //static 
217          m.getType().setName("void");        //void 
218          m.setName("main");                  //main 
219          m.setParams(new LexParam[1]);       //(String args[]) 
220          LexParam p[] = m.getParams();       //{ 
221          p[0] = new LexParam(); 
222          p[0].setName("args[]"); 
223          p[0].getType().setName("String"); 
224          p[0].getType().setArray(true); 
225          LexBody b = m.getBody(); 
226          b.setTryCatch(true);                //try/catch 
227          b.setCode(code);                    //assign code 
228          hasMain = true;                     //} 
229      } 
230   
231      /** 
232       * Gets main method for output code 
233       * 
234       * @return m Main method 
235       */ 
236      public LexMethod getMain() { 
237          return m; 
238      } 
239   
240      /** 
241       * Class/interface to string 
242       */ 
243      public String toString() { 
244          return toString(true, true, false); 
245      } 
246   
247      /** 
248       * Class/interface to string 
249       * 
250       * @param onlyPublic   Public members only 
251       * @param noMain       No main method 
252       * @param withDelegate With cutils.delegate 
253       *                     field 
254       * @return s 
255       */ 
256      public String toString(boolean onlyPublic, 
257                             boolean noMain, 
258                             boolean withDelegate) { 
259          String s = ""; 
260          int i; 
261          s = 
262          s + 
263          "//Created automatically by CentiJ\n"; 
264          //s = s + packToString();   ??? 
265          s = s + "package graphics.raytracer;\n"; 
266   
267          s = s + importToString();                   //imports 
268          s = s + getHeader();                        //modifiers + name 
269          s = s + getExtendsImplementsString(EXT) + 
270              getExtendsImplementsString(IMP);    //extends and implements 
271          s = s + "{\n\n"; 
272          if (withDelegate) {                         //cutils.delegate field 
273              s = s + getDelegateString() + "\n"; 
274          } 
275          if (isClass() && hasConstructor) {          //constructor 
276              s = s + constr.toString(true, false); 
277          } 
278          for (i = 0; i < fields.length; i++) {       //fields 
279              if (onlyPublic && 
280                  !fields[i].getModif().isPublic()) { 
281                  continue; 
282              } 
283              s = s + fields[i].toString(); 
284          } 
285          s = s + "\n"; 
286          for (i = 0; i < methods.length; i++) {      //general methods 
287              if (onlyPublic && 
288                  !methods[i].getModif().isPublic()) { 
289                  continue; 
290              } 
291              if (noMain && 
292                  methods[i].getName().equals( 
293                          "main")) { 
294                  continue; 
295              } 
296              if (methods[i].getName().equals( 
297                      "hashCode")) { 
298                  continue; 
299              }   //rmic 
300              if (methods[i].getName().equals( 
301                      "clone")) { 
302                  continue; 
303              }       //rmic 
304              if (methods[i].getName().equals( 
305                      "toString")) { 
306                  continue; 
307              }   //rmic 
308              s = s + 
309                  methods[i].toString(kind, false); 
310          } 
311          if (isClass() && hasMain) {                 //main method 
312              s = s + m.toString(true, true); 
313          } 
314   
315          s = s + "}"; 
316          return s; 
317      } 
318   
319      /** 
320       * Returns string of extends/implements 
321       * 
322       * @return s 
323       */ 
324      public String getExtendsImplementsString( 
325              String action) { 
326          String s = ""; 
327          StringVector sv = new StringVector(); 
328          if (action.equals(EXT)) {   //extends 
329              sv = extend; 
330          } 
331          if (action.equals(IMP)) {   //implements 
332              sv = implem; 
333          } 
334          int svSize = sv.size(); 
335          if (svSize > 0) { 
336              s = s + "\n\t" + action + " " + 
337                  sv.toCSV(); 
338          } 
339          return s; 
340      } 
341   
342      /** 
343       * Get cutils.delegate field 
344       * 
345       * @return s 
346       */ 
347      public String getDelegateString() { 
348          String s = ""; 
349          if (isClass()) { 
350              s = s + "\tprivate "; 
351              s = s + ref.getName(); 
352              s = s + " v = new "; 
353              s = s + ref.getName(); 
354              s = s + "();\n"; 
355              ; 
356          } 
357          return s; 
358      } 
359  } 
360