/Users/lyon/j4p/src/net/rmi/rmiSynth/lex/Transform.java
|
1 /**
2 * Class Transform provides static methods for transformations of LexBase class
3 * @author Roman Yedokov
4 */
5
6 package net.rmi.rmiSynth.lex;
7
8
9 public class Transform {
10 static final String TABS3 = "\t\t\t";
11
12 /**
13 * Substitutes all references to srcClass to
14 * tgtClass in the instance of LexBase class
15 * (except for clone() method)
16 *
17 * @param lb Instance of LexBase class
18 * @param srcClass Name of the class that
19 * needs to be replaced
20 * @param tgtClass Name of the class that will
21 * be put in
22 * @return void
23 */
24 public static void castIt(LexBase lb,
25 String srcClass,
26 String tgtClass) {
27 LexMethod[] mtd; //methods of the class
28 LexParam[] params; //parameters of a method
29 LexBody b; //body of a method
30 LexType mtdType; //method return type
31 String mtdTypeName; //name of the return type
32 String brackets;
33
34 mtd = lb.getMethods(); //get methods from class
35 //scan thru all methods
36 for (int i = 0; i < mtd.length; i++) {
37 brackets = "";
38 //skip clone()
39 if (stripPackageName(
40 mtd[i].getName())
41 .equals("clone")) {
42 continue; //always returns Object
43 }
44 mtdType = mtd[i].getType(); //get method return type
45 mtdTypeName =
46 stripPackageName(mtdType.getName()); //get type name
47 //if this type is type of srcClass replace it with target class
48 if (mtdTypeName.equals(srcClass) ||
49 mtdTypeName.equals(
50 srcClass + "[]")) {
51 brackets = mtdType.isArray() ?
52 "[]" :
53 "";
54 mtd[i].getType().setName(
55 tgtClass + brackets);
56 b = mtd[i].getBody();
57 b.setDelegObj("( " + tgtClass +
58 brackets +
59 " ) " +
60 b.getDelegObj());
61 }
62 //now check types of parameters passed to this method
63 params = mtd[i].getParams();
64 castParams(params,
65 srcClass,
66 tgtClass);
67 }
68 }
69
70 /**
71 * Substitutes all references to srcClass to
72 * tgtClass in the array of parameters (except
73 * for clone() method)
74 *
75 * @param prm Array of parameters
76 * @param srcClass Name of the class that
77 * needs to be replaced
78 * @param tgtClass Name of the class that will
79 * be put in
80 * @return void
81 */
82 public static void castParams(LexParam[] prm,
83 String srcClass,
84 String tgtClass) {
85 LexType prmType; //parameter type
86 String prmTypeName; //name of the parameter type
87 String brackets;
88 for (int j = 0; j < prm.length; j++) {
89 prmType = prm[j].getType();
90 prmTypeName =
91 stripPackageName(prmType.getName());
92 if (prmTypeName.equals(srcClass) ||
93 prmTypeName.equals(
94 srcClass + "[]")) {
95 brackets = prmType.isArray() ?
96 "[]" :
97 "";
98 prm[j].getType().setName(
99 tgtClass + brackets);
100 }
101 }
102 }
103
104 /**
105 * Removes everything before last dot in a
106 * string
107 *
108 * @param String to be stripped - <java.lang.type>
109 * @return Stripped string - <type>
110 */
111 public static String stripPackageName(
112 String s) {
113 int index = s.lastIndexOf('.');
114 if (index == -1) return s;
115 index++;
116 return s.substring(index);
117 }
118
119 /**
120 * Modifies a LexBase class such way that it
121 * complies to RMI interface specifications
122 * Adds <extends java.rmi.Remote> to class
123 * declaration Adds <throw java.rmi.RemoteException>
124 * to method declarations
125 *
126 * @param lb Instance of LexBase class
127 * @return void
128 */
129 public static void toRMIInterface(LexBase lb) {
130 LexMethod[] mtd;
131 lb.addPackage("rmi");
132 lb.addInherit("extends",
133 "java.rmi.Remote");
134 mtd = lb.getMethods();
135 for (int i = 0; i < mtd.length; i++) {
136 mtd[i].addThrow(
137 "java.rmi.RemoteException");
138 }
139
140 }
141
142 /**
143 * Modifies a LexBase class such way that it
144 * complies to RMI server specifications
145 * Imports java.rmi.* and java.rmi.registry.*
146 * Adds <implements java.rmi.Remote> to class
147 * declaration Adds <extends java.rmi.server.UnicastRemoteObject>
148 * to class declaration Adds <throw
149 * java.rmi.RemoteException> to method
150 * declarations
151 *
152 * @param lb Instance of LexBase
153 * class
154 * @param ServerInter Name of interface to
155 * implement
156 * @param ServerClass Name of new class
157 */
158 public static void toRMIServer(LexBase lb,
159 String ServerInter,
160 String ServerClass) {
161 LexMethod[] mtd;
162 String code = "";
163 mtd = lb.getMethods();
164 for (int i = 0; i < mtd.length; i++) {
165 mtd[i].addThrow(
166 "java.rmi.RemoteException");
167 }
168 lb.addPackage("rmi");
169 lb.addImport("java.rmi.*");
170 lb.addImport("java.rmi.registry.*");
171 lb.addInherit("implements", ServerInter);
172 lb.addInherit("extends",
173 "java.rmi.server.UnicastRemoteObject");
174 lb.setConstr(ServerClass);
175 lb.getConstr().addThrow(
176 "java.rmi.RemoteException");
177 code =
178 code + TABS3 +
179 "// Create and install a security manager\n";
180 code =
181 code + TABS3 +
182 "System.setSecurityManager(new RMISecurityManager());\n";
183 code = code + TABS3 + ServerClass +
184 " rs = new " +
185 ServerClass +
186 "();\n";
187 code =
188 code + TABS3 +
189 "//Create the registry and bind the Server class to the registry\n";
190 code =
191 code + TABS3 +
192 "LocateRegistry.createRegistry(1099);\n";
193 code =
194 code + TABS3 +
195 "Registry r= LocateRegistry.getRegistry();\n";
196 code = code + TABS3 + "r.bind(\"" +
197 ServerClass +
198 "\", rs);\n";
199 code = code + TABS3 +
200 "System.out.println(" +
201 "\"" +
202 ServerClass +
203 " is running\");\n";
204
205 lb.setMain(code);
206
207 }
208
209 /**
210 * Modifies a LexBase class such way that it
211 * complies to RMI server specifications
212 * Imports java.rmi.* and java.rmi.registry.*
213 *
214 * @param lb Instance of LexBase
215 * class
216 * @param ServerInter Name of interface to
217 * instanciate
218 * @param ServerClass Name of server class to
219 * connect to
220 * @param ClientClass Name of new class to
221 * create
222 * @param fwdMethod Method from server which
223 * is called
224 * @param IP Address of server to
225 * connect
226 */
227 public static void toRMIClient(LexBase lb,
228 String ServerInter,
229 String ServerClass,
230 String ClientClass,
231 String IP) {
232 String code = "";
233
234 lb.addPackage("rmi");
235 lb.addImport("java.rmi.*");
236 lb.addImport("java.rmi.registry.*");
237 code =
238 code + TABS3 +
239 "//locate the server class on remote machine\n";
240 code = code + TABS3 +
241 "Registry r = LocateRegistry.getRegistry(" +
242 IP +
243 ");\n";
244 code = code + TABS3 + ServerInter +
245 " rs = (" +
246 ServerInter +
247 ")r.lookup(\"" +
248 ServerClass +
249 "\");\n";
250 code =
251 code + TABS3 +
252 "//Type in your remote procedure calls below\n\n";
253
254 lb.setMain(code);
255
256 }
257 }