1 package iotruntime.stub;
4 import java.io.BufferedWriter;
5 import java.io.PrintWriter;
6 import java.io.FileWriter;
7 import java.io.IOException;
9 import java.net.HttpURLConnection;
10 import java.net.MalformedURLException;
11 import java.net.ProtocolException;
14 import java.lang.Class;
15 import java.lang.reflect.*;
18 /** IoTStubGenerator class that takes an interface,
19 * instrument it using Java Reflection, and generate
20 * static code that uses IoTRemoteCall as a stub object
22 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
26 public class IoTStubCodeGenerator {
29 * IoTStubCodeGenerator properties
32 String strInterfaceName;
33 String strNewClassName;
34 String strCallbackIntName;
39 public IoTStubCodeGenerator(String[] strArgs) throws Exception {
41 this.strInterfaceName = strArgs[0];
42 if (strArgs.length == 1) {
43 // If callback interface is not specified
44 this.strCallbackIntName = "Object";
46 this.strCallbackIntName = strArgs[1];
48 strNewClassName = strInterfaceName + "Implementation";
49 FileWriter fw = new FileWriter(strNewClassName + ".java");
50 pw = new PrintWriter(new BufferedWriter(fw));
54 * Instrument interface class
56 public void instrumentInterfaceClass() {
60 // Write the class header
61 println("public final class " + strNewClassName +
62 " extends IoTJSONStub " +
63 " implements " + strInterfaceName + " {");
67 generateConstructor();
71 Class<?>cls = Class.forName(strInterfaceName);
72 for (Method mtd : cls.getDeclaredMethods()) {
76 } catch (ClassNotFoundException ex) {
79 generateMethodCallback();
87 * Generate import statements
89 private void generateImports() {
90 // Write the class properties
91 println("import iotruntime.stub.IoTRemoteCall;");
92 println("import iotruntime.stub.IoTJSONStub;");
93 println("import iotruntime.slave.IoTDeviceAddress;");
100 private void generateFields() {
101 // Write the class properties
102 println("private IoTRemoteCall iotremotecall;");
103 println("private " + strCallbackIntName + " callbackObject;");
107 * Generate constructor
109 private void generateConstructor() {
110 // Write the constructor
111 println("public " + strNewClassName +
112 "(IoTDeviceAddress _iotDevAdd) {");
113 println("super(_iotDevAdd);");
114 println("this.iotremotecall = new IoTRemoteCall();");
115 print("String[] arrMethodName = { \"");
116 // Get the interface class
118 Class<?>cls = Class.forName(strInterfaceName);
119 Method[] method = cls.getDeclaredMethods();
120 for (Method mtd: method) {
121 print(mtd.getName());
122 // Check if this is the last element
123 if (!mtd.equals(method[method.length-1])) {
128 println("this.iotremotecall.startHttpServer(arrMethodName, " +
129 "iotDevAddress.getDestinationPortNumber());");
130 } catch (ClassNotFoundException ex) {
131 ex.printStackTrace();
137 * Generate method body
139 private void generateMethod(Method mtd) {
141 Class<?> clsReturn = mtd.getReturnType();
142 // Write the method declaration
143 print("public " + clsReturn.getSimpleName() + " " + mtd.getName() + "(");
144 Parameter[] params = mtd.getParameters();
145 // Write the method params
146 for (Parameter param:params) {
147 print(param.getType().getSimpleName() + " " + param.getName());
148 // Check if this is the last element
149 if (!param.equals(params[params.length-1])) {
154 // Write the method body
155 // Handle return value
156 println("String strMethodName = \"" + mtd.getName() + "\";");
158 print("Object[] arrInpValue = { ");
159 for (Parameter param:params) {
160 print(param.getName());
161 // Check if this is the last element
162 if (!param.equals(params[params.length-1])) {
167 print("String[] arrInpType = { \"");
168 for (Parameter param:params) {
169 print(param.getType().getSimpleName());
170 // Check if this is the last element
171 if (!param.equals(params[params.length-1])) {
176 println("Object _retval=iotremotecall.callMethod(strMethodName, iotDevAddress.getHostAddress(), iotDevAddress.getDestinationPortNumber(), arrInpValue, arrInpType, \""+ clsReturn.getSimpleName()+ "\");");
177 if (!clsReturn.equals(Void.class)) {
178 println("return ("+clsReturn.getSimpleName()+") _retval;");
184 private void generateMethodCallback() {
187 println("public void registerCallback(Object objCallback) {");
188 println("this.callbackObject = (" + strCallbackIntName + ") objCallback;");
193 boolean newline=true;
196 private void print(String str) {
201 for(int i=0; i<tab; i++)
209 private void println(String str) {
214 for(int i=0; i<tab; i++)
222 private void updatetabbing(String str) {
223 tablevel+=count(str,'{')-count(str,'}');
226 private int count(String str, char key) {
227 char[] array = str.toCharArray();
229 for(int i=0; i<array.length; i++) {
236 public static void main(String[] args) throws Exception {
237 // args[0] = normal interface name
238 // args[1] = callback interface name
239 IoTStubCodeGenerator stub = new IoTStubCodeGenerator(args);
240 stub.instrumentInterfaceClass();