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

1    /** 
2     * Class LexMethod - string representation of a method 
3     * @author Roman Yedokov 
4     * modifier return_type method_name(parameters){ 
5     *  body; 
6     * } 
7     */ 
8     
9    package net.rmi.rmiSynth.lex; 
10    
11    
12   public class LexMethod extends LexStructure { 
13    
14       private StringVector throwsEx;  //Vector of thrown exceptions 
15       private LexParam[] params;      //Parameters 
16       private LexBody body;           //Method body 
17    
18       /** 
19        * Constructor 
20        */ 
21       public LexMethod() { 
22           params = new LexParam[0]; 
23           body = new LexBody(); 
24           throwsEx = new StringVector(); 
25       } 
26    
27       /** 
28        * Gets parameters 
29        * 
30        * @return params 
31        */ 
32       public LexParam[] getParams() { 
33           return params; 
34       } 
35    
36       /** 
37        * Sets parameters 
38        * 
39        * @param _params Parameters 
40        */ 
41       public void setParams(LexParam[] _params) { 
42           params = _params; 
43       } 
44    
45       /** 
46        * Gets 1 parameter 
47        * 
48        * @param i Index of parameter 
49        */ 
50       public LexParam getParam(int i) { 
51           return params[i]; 
52       } 
53    
54       /** 
55        * Gets body 
56        * 
57        * @return body 
58        */ 
59       public LexBody getBody() { 
60           return body; 
61       } 
62    
63       /** 
64        * Sets body 
65        * 
66        * @param _body Body 
67        */ 
68       public void setBody(LexBody _body) { 
69           body = _body; 
70       } 
71    
72       /** 
73        * Adds exception 
74        * 
75        * @param exName Exception name 
76        */ 
77       public void addThrow(String exName) { 
78           throwsEx.add(exName); 
79       } 
80    
81       /** 
82        * Returns true if methods throws any 
83        * exception 
84        */ 
85       public boolean ifThrows() { 
86           return throwsEx.size() > 0 ? 
87                  true : 
88                  false; 
89       } 
90    
91       /** 
92        * LexMethod to string 
93        * 
94        * @param forClass Class vs interface 
95        * @param forMain  Main method vs all others 
96        * @return s 
97        */ 
98       public String toString(boolean forClass, 
99                              boolean forMain) { 
100          String s = ""; 
101          boolean withType = true; 
102          boolean withoutType = false; 
103   
104          s = s + "\t" + getHeader(); 
105          s = s + "("; 
106          s = s + paramsToString(withType); 
107          s = s + ")"; 
108          s = 
109          s + 
110          (ifThrows() ? 
111           " throws " + throwsEx.toCSV() : 
112           ""); 
113          if (forClass) {             //Class 
114              //withType = false; 
115              s = s + "{\n"; 
116              s = s + "\t\t"; 
117              if (forMain) { 
118                  s = s + getBody().codeToString(); 
119              } else { 
120                  s = s + getBody().toString(); 
121                  s = s + "("; 
122                  s = s + 
123                      paramsToString(withoutType); 
124                  s = s + ");"; 
125              } 
126              s = s + "\n"; 
127              s = s + "\t}\n"; 
128          } else {                        //Interface 
129              s = s + ";"; 
130          } 
131          s = s + "\n"; 
132          return s; 
133   
134      } 
135   
136      /** 
137       * Parameters to string 
138       * 
139       * @param withType If we need it with type or 
140       *                 not 
141       * @return s 
142       */ 
143      public String paramsToString( 
144              boolean withType) { 
145          String s = ""; 
146          for (int i = 0; i < params.length; i++) { 
147              s = s + " " + 
148                  params[i].toString(withType) + 
149                  ","; 
150          } 
151          s = 
152          s.substring(0, 
153                      Math.max(s.length() - 1, 0)); 
154          return s; 
155      } 
156   
157  } 
158