Commit 566e9ca8 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

hacked dynamic test to handle user types

parent 825efef0
......@@ -346,6 +346,21 @@ aspect Java_Class {
}
}
// TODO: ?
// Code snippet for making samples of user types extend their type
// currently commented out. Is this a good idea?
//
// syn String Type.getTypeName();
// eq Type.getTypeName() = getClass().getName();
// eq PrimType.getTypeName() = getName();
// eq UserType.getTypeName() = getName();
// syn boolean Type.isUserType();
// eq Type.isUserType() = false;
// eq UserType.isUserType() = true;
//
// plus use some lines down
public void SampleDecl.Java_emitClass(Java_env env, String pack) {
env.println("/* ");
pp(env.getPrintStream());
......@@ -362,7 +377,11 @@ aspect Java_Class {
env.println("import se.lth.control.labcomm.LabCommHandler;");
env.println("import se.lth.control.labcomm.LabCommSample;");
env.println();
env.println("public class " + getName() + " implements LabCommSample {");
env.print("public class " + getName());
// if(getType().isUserType()) {
// env.print(" extends "+getType().getTypeName());
// }
env.println(" implements LabCommSample {");
env.println();
env.indent();
getType().Java_emitInstance(env);
......
foo:handler(foo value) {
foo:handler(foo_t value) {
test.HandlerContext ctx = (test.HandlerContext)context;
System.out.println("foo handler from handlers2.txt");
System.out.println("using context "+ctx.str);
......
sample struct {
typedef struct {
int x;
int y;
int z;
} foo;
} foo_t;
sample foo_t foo;
sample int bar;
......@@ -29,6 +29,7 @@ import beaver.Parser.Exception;
public class DynamicPart {
private static final String TYPE_NAME_FOO = "foo_t";
private static final String SAMPLE_NAME_FOO = "foo";
private static final String SAMPLE_NAME_BAR = "bar";
......@@ -223,12 +224,15 @@ public class DynamicPart {
while(i.hasNext()){
final String sampleName = i.next();
final String src = genCode.get(sampleName);
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
String handleM = handlers.get(sampleName);
if(handleM != null) {
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
}
handlerMethods.append(handleM);
handlerMethods.append("\n");
}
handlerMethods.append(handlers.get(sampleName));
handlerMethods.append("\n");
// System.out.println("***"+sampleName+"\n"+src);
irc.compile(sampleName, src); // while iterating, compile the labcomm generated code
}
......@@ -298,18 +302,19 @@ public class DynamicPart {
*/
private void encodeTest(InRAMCompiler irc, HandlerContext ctxt, String tmpFile) {
try {
Class ft = irc.load(TYPE_NAME_FOO);
Class fc = irc.load(SAMPLE_NAME_FOO);
Class bc = irc.load(SAMPLE_NAME_BAR);
/* create sample class and instance objects */
Object f = fc.newInstance();
Object fv = ft.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
Field z = fc.getDeclaredField("z");
x.setInt(f, ctxt.x);
y.setInt(f, ctxt.y);
z.setInt(f, ctxt.z);
Field x = ft.getField("x");
Field y = ft.getField("y");
Field z = ft.getField("z");
x.setInt(fv, ctxt.x);
y.setInt(fv, ctxt.y);
z.setInt(fv, ctxt.z);
FileOutputStream out = new FileOutputStream(tmpFile);
......@@ -319,8 +324,8 @@ public class DynamicPart {
Method regFoo = fc.getDeclaredMethod("register", LabCommEncoder.class);
regFoo.invoke(fc, enc);
Method doEncodeFoo = fc.getDeclaredMethod("encode", LabCommEncoder.class, fc);
doEncodeFoo.invoke(fc, enc, f);
Method doEncodeFoo = fc.getDeclaredMethod("encode", LabCommEncoder.class, ft);
doEncodeFoo.invoke(fc, enc, fv);
/* register and send bar (NB! uses primitive type int) */
Method regBar = bc.getDeclaredMethod("register", LabCommEncoder.class);
......@@ -343,17 +348,17 @@ public class DynamicPart {
Constructor hcc = hc.getDeclaredConstructor(Object.class);
// Object h = hc.newInstance();
Object h = hcc.newInstance(new HandlerContext());
Class fc = irc.load(SAMPLE_NAME_FOO);
Object f = fc.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
Field z = fc.getDeclaredField("z");
Class ft = irc.load(TYPE_NAME_FOO);
Object f = ft.newInstance();
Field x = ft.getDeclaredField("x");
Field y = ft.getDeclaredField("y");
Field z = ft.getDeclaredField("z");
x.setInt(f, 10);
y.setInt(f, 11);
z.setInt(f, 12);
Method m;
try {
m = hc.getDeclaredMethod("handle_"+SAMPLE_NAME_FOO, fc);
m = hc.getDeclaredMethod("handle_"+SAMPLE_NAME_FOO, ft);
m.invoke(h, f);
} catch (SecurityException e) {
// TODO Auto-generated catch block
......
package test;
import gen.foo_t;
import gen.foo;
import gen.bar;
......@@ -31,7 +32,7 @@ public class StaticDecoder implements foo.Handler, bar.Handler
}
public void handle_foo(foo d) throws java.io.IOException {
public void handle_foo(foo_t d) throws java.io.IOException {
System.out.println("Got foo, x="+d.x+", y="+d.y+", z="+d.z);
}
......
......@@ -4,6 +4,7 @@ import java.io.FileOutputStream;
import java.io.OutputStream;
import se.lth.control.labcomm.LabCommEncoderChannel;
import gen.foo_t;
import gen.foo;
import gen.bar;
......@@ -20,7 +21,7 @@ public class StaticEncoder {
}
public void doEncode() throws java.io.IOException {
foo f = new foo();
foo_t f = new foo_t();
f.x = 17;
f.y = 42;
f.z = 37;
......
......@@ -29,6 +29,7 @@ import beaver.Parser.Exception;
public class TestLabcommGen {
private static final String TYPE_NAME_FOO = "foo_t";
private static final String SAMPLE_NAME_FOO = "foo";
private static final String SAMPLE_NAME_BAR = "bar";
......@@ -79,7 +80,7 @@ public class TestLabcommGen {
if(irc != null) {
System.out.println("*** Testing instantiation and invocation of Handler ");
dummyTest(irc);
//dummyTest(irc);
String tmpFile = args[2];
System.out.println("*** Testing writing and reading file "+tmpFile);
......@@ -226,25 +227,35 @@ public class TestLabcommGen {
try {
while(i.hasNext()){
final String sampleName = i.next();
System.out.println("sample: "+sampleName);
final String src = genCode.get(sampleName);
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
String hctmp = handlers.get(sampleName);
if(hctmp != null) {
handlerClass.append(sampleName+".Handler");
if(i.hasNext()) {
handlerClass.append(", ");
}
handlerMethods.append(hctmp);
handlerMethods.append("\n");
}
handlerMethods.append(handlers.get(sampleName));
handlerMethods.append("\n");
//System.out.println("FOOOO:"+sampleName+"++++"+hctmp);
//System.out.println("GOOO: "+sampleName+"----"+handlerMethods);
System.out.println("***"+sampleName+"\n"+src);
irc.compile(sampleName, src); // while iterating, compile the labcomm generated code
}
handlerClass.append("{\n");
handlerClass.append(handlerAttributes);
handlerClass.append(handlerConstr);
handlerClass.append(handlerConstrCtxt);
handlerClass.append(handlerMethods.toString());
if(handlerAttributes != null)
handlerClass.append(handlerAttributes);
if(handlerConstr != null)
handlerClass.append(handlerConstr);
if(handlerConstrCtxt != null)
handlerClass.append(handlerConstrCtxt);
if(handlerMethods != null)
handlerClass.append(handlerMethods.toString());
handlerClass.append("}\n");
System.out.println("-------------------------------------");
System.out.println("--- generated code to compile -------"+ handlerClassName);
final String handlerSrc = handlerClass.toString();
System.out.println(handlerSrc);
......@@ -294,7 +305,7 @@ public class TestLabcommGen {
sb.append("public class gen_"+sampleName+"Handler implements "+sampleName+".Handler {\n");
sb.append(handlers.get(sampleName));
sb.append("}\n");
System.out.println("-------------------------------------");
System.out.println("--- foo -----------------------------");
System.out.println(sb.toString());
try {
irc.compile(sampleName, src);
......@@ -356,15 +367,16 @@ public class TestLabcommGen {
*/
private static void encodeTest(InRAMCompiler irc, String tmpFile) {
try {
Class ft = irc.load(TYPE_NAME_FOO);
Class fc = irc.load(SAMPLE_NAME_FOO);
Class bc = irc.load(SAMPLE_NAME_BAR);
/* create sample class and instance objects */
Object f = fc.newInstance();
Object f = ft.newInstance();
Field x = fc.getDeclaredField("x");
Field y = fc.getDeclaredField("y");
Field z = fc.getDeclaredField("z");
Field x = ft.getDeclaredField("x");
Field y = ft.getDeclaredField("y");
Field z = ft.getDeclaredField("z");
x.setInt(f, 10);
y.setInt(f, 11);
z.setInt(f, 12);
......@@ -377,7 +389,7 @@ public class TestLabcommGen {
Method regFoo = fc.getDeclaredMethod("register", LabCommEncoder.class);
regFoo.invoke(fc, enc);
Method doEncodeFoo = fc.getDeclaredMethod("encode", LabCommEncoder.class, fc);
Method doEncodeFoo = fc.getDeclaredMethod("encode", LabCommEncoder.class, ft);
doEncodeFoo.invoke(fc, enc, f);
/* register and send bar (NB! uses primitive type int) */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment