From 7de617f0c15106e4cc202a6f2d2bd09b0fbfe89e Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 5 Oct 2017 20:02:11 +0200 Subject: [PATCH] test --- src/de/dhbwstuttgart/bytecode/Test.java | 190 ++++++++++-------- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 3 +- .../syntaxtree/statement/ArgumentList.java | 5 + test/.DS_Store | Bin 6148 -> 6148 bytes test/bytecode/JavaTXCompilerTest.java | 4 +- test/typeinference/JavaTXCompilerTest.java | 3 +- 6 files changed, 117 insertions(+), 88 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/Test.java b/src/de/dhbwstuttgart/bytecode/Test.java index baa707e7..f908655f 100644 --- a/src/de/dhbwstuttgart/bytecode/Test.java +++ b/src/de/dhbwstuttgart/bytecode/Test.java @@ -5,8 +5,10 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.invoke.CallSite; +import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; +import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; @@ -18,103 +20,123 @@ import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; +import javassist.runtime.Desc; + public class Test { - private static final String rootDirectory = System.getProperty("user.dir")+"/bin/de/dhbwstuttgart/bytecode/"; - protected static ClassLoader getClassLoader() throws Exception{ + private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; + + protected static ClassLoader getClassLoader() throws Exception { File file = new File(rootDirectory); - URL url = file.toURI().toURL(); - URL[] urls = new URL[]{url}; - System.out.println(urls[0]); - return new URLClassLoader(urls); + URL url = file.toURI().toURL(); + URL[] urls = new URL[] { url }; + System.out.println(urls[0]); + return new URLClassLoader(urls); } - + public static void main(String[] args) { // Test Lambda - ClassWriter cw =new ClassWriter(ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null); - - // Create Constructor - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Ljava/lang/Boolean;)V", null, null); - mv.visitVarInsn(Opcodes.ALOAD, 0); - - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - -// mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable"); - - MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, - String.class, MethodType.class); - - Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", - "metafactory", mt.toMethodDescriptorString()); - - mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap); - - mv.visitVarInsn(Opcodes.ASTORE, 1); - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"); - mv.visitInsn(Opcodes.RETURN); - mv.visitMaxs(1, 2); - mv.visitEnd(); - - cw.visitEnd(); - - byte[] b = cw.toByteArray(); - - -// Test if statement -/* ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); - - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, "java/lang/Object", null); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Ljava/lang/Boolean;)V", null, null); - mv.visitCode(); - -// Label l0 = new Label(); -// mv.visitLabel(l0); - - mv.visitVarInsn(Opcodes.ALOAD, 0); - - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - -// Label l1 = new Label(); -// mv.visitLabel(l1); - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); - - Label label = new Label(); - mv.visitJumpInsn(Opcodes.IFEQ, label); - - mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); - mv.visitLdcInsn("1"); - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); - - Label endLabel = new Label(); - mv.visitJumpInsn(Opcodes.GOTO, endLabel); - - mv.visitLabel(label); - mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); - mv.visitLdcInsn("0"); - mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); - - + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null); - mv.visitLabel(endLabel); + cw.visitSource("TestClass.java", null); + + // Create Constructor + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); + mv.visitVarInsn(Opcodes.ALOAD, 0); + + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); + + // mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable"); + //Call site, which, when invoked, returns an instance of the functional interface to which + //the lambda is being converted + MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, + MethodType.class, MethodType.class, MethodHandle.class, MethodType.class); + + Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", + mt.toMethodDescriptorString()); + Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, "TestClass", "lambda$0", "()V"); + + mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap, + Type.getMethodType("()V"), arg2, + Type.getMethodType("()V")); + + mv.visitVarInsn(Opcodes.ASTORE, 1); + mv.visitVarInsn(Opcodes.ALOAD, 1); + mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"); mv.visitInsn(Opcodes.RETURN); -// Label l2 = new Label(); -// mv.visitLabel(l2); + // creates bridge method, contains lambdas body + MethodVisitor mvl = cw.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "lambda$0", + "()V", null, null); + mvl.visitCode(); + mvl.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); + mvl.visitLdcInsn("lambda"); + mvl.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); + mvl.visitInsn(Opcodes.RETURN); + mvl.visitMaxs(2, 0); + mvl.visitEnd(); -// mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); -// mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1); - mv.visitMaxs(2, 2); + mv.visitMaxs(1, 2); mv.visitEnd(); + + cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", + Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); cw.visitEnd(); + byte[] b = cw.toByteArray(); -*/ + + // Test if statement + /* + * ClassWriter cw = new + * ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); + * + * cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, + * "java/lang/Object", null); MethodVisitor mv = + * cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Ljava/lang/Boolean;)V", null, + * null); mv.visitCode(); + * + * // Label l0 = new Label(); // mv.visitLabel(l0); + * + * mv.visitVarInsn(Opcodes.ALOAD, 0); + * + * mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", + * "()V"); + * + * // Label l1 = new Label(); // mv.visitLabel(l1); + * mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, + * "java/lang/Boolean", "booleanValue", "()Z"); + * + * Label label = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, label); + * + * mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", + * "Ljava/io/PrintStream;"); mv.visitLdcInsn("1"); + * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", + * "(Ljava/lang/String;)V"); + * + * Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.GOTO, endLabel); + * + * mv.visitLabel(label); mv.visitFieldInsn(Opcodes.GETSTATIC, + * "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("0"); + * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", + * "(Ljava/lang/String;)V"); + * + * + * + * mv.visitLabel(endLabel); mv.visitInsn(Opcodes.RETURN); + * + * // Label l2 = new Label(); // mv.visitLabel(l2); + * + * // mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); // + * mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1); + * mv.visitMaxs(2, 2); mv.visitEnd(); + * + * cw.visitEnd(); byte[] b = cw.toByteArray(); + */ FileOutputStream output; - + try { - output = new FileOutputStream(new File(System.getProperty("user.dir")+"/testBytecode/TestClass.class")); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TestClass.class")); output.write(b); output.close(); } catch (FileNotFoundException e) { @@ -124,6 +146,6 @@ public class Test { // TODO Auto-generated catch block e.printStackTrace(); } - - } + + } } diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index e8929729..7ec2b359 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.core; +import de.dhbwstuttgart.bytecode.BytecodeGen; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; @@ -81,5 +82,5 @@ public class JavaTXCompiler { sourceFiles.add(ret); return ret; } - + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java index 75c92370..1e61c3ca 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.syntaxtree.ASTVisitor; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import org.antlr.v4.runtime.Token; @@ -20,6 +21,10 @@ public class ArgumentList extends SyntaxTreeNode return expr; } + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + @Override public void accept(ASTVisitor visitor) { visitor.visit(this); diff --git a/test/.DS_Store b/test/.DS_Store index 732178d7d20ed2b10df9fbc96895a227e2961738..c09b8b052eb716cdf513446dd757b4f607b4292e 100644 GIT binary patch delta 78 zcmZoMXfc=|#>B`mu~2NHo+2a5!~p9_j154#M~y#;p^~A5A(bJSA)g_IA$8-DV{Dt* fIruq%$~S)e&ODi4#E^rLfq|g`h&OwP>|q7~C$|&J delta 63 zcmZoMXfc=|#>CJ*u~2NHo+2aj!~knX=E*#aYMcETcd$)fz&Lj^I|n}pP{rnnjNh3j T^NScVGEBDNk=`65vVs`^F_IBk diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index 16e96e5a..dc066306 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -37,12 +37,12 @@ public class JavaTXCompilerTest extends JavaTXCompiler { @Test public void test() throws IOException, java.lang.ClassNotFoundException { - filesToTest.add(new File(rootDirectory+"EmptyClass.jav")); + filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); for(File f : filesToTest){ SourceFile sf = this.parse(f); String content = readFile(f.getPath(), StandardCharsets.UTF_8); HashMap bytecode = this.getBytecode(sf); - this.writeClassFile(bytecode, "EmptyClass"); + this.writeClassFile(bytecode, "EmptyMethod"); } } diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 2337dad6..64909410 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -35,7 +35,8 @@ public class JavaTXCompilerTest extends JavaTXCompiler { public void test() throws IOException, java.lang.ClassNotFoundException { //filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - filesToTest.add(new File(rootDirectory+"test.jav")); + //filesToTest.add(new File(rootDirectory+"test.jav")); + filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); //filesToTest.add(new File(rootDirectory+"Lambda3.jav"));