From 7de617f0c15106e4cc202a6f2d2bd09b0fbfe89e Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 5 Oct 2017 20:02:11 +0200 Subject: [PATCH 1/2] 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")); From c595925112250cc69bc6daaf6d036db82d9b417c Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Fri, 6 Oct 2017 13:31:36 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Fehlende=20Tests=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/de/dhbwstuttgart/.DS_Store | Bin 0 -> 6148 bytes .../dhbwstuttgart/bytecode/BytecodeGen.java | 0 .../bytecode/BytecodeGenLambda.java | 227 ++++++++++++++++++ .../bytecode/BytecodeGenMethod.java | 0 src/de/dhbwstuttgart/bytecode/ClassFile.java | 14 ++ .../dhbwstuttgart/bytecode/Descriptor.java | 0 .../de}/dhbwstuttgart/bytecode/Test.java | 1 - .../de}/dhbwstuttgart/bytecode/TestClass.java | 0 src/de/dhbwstuttgart/bytecode/TestFields.java | 59 +++++ .../de}/dhbwstuttgart/bytecode/TestIf.java | 0 src/de/dhbwstuttgart/bytecode/TestMeth.java | 18 ++ src/de/dhbwstuttgart/bytecode/TetsF.java | 6 + src/de/dhbwstuttgart/core/JavaTXCompiler.java | 7 +- .../syntaxtree/statement/ArgumentList.java | 3 - test/bytecode/JavaTXCompilerTest.java | 13 +- test/javFiles/EmptyClass.jav | 3 + test/javFiles/EmptyMethod.jav | 37 +++ test/javFiles/Op1.jav | 11 + test/javFiles/Subclass.jav | 6 + test/javFiles/Superclass.jav | 6 + test/typeinference/JavaTXCompilerTest.java | 3 - testBytecode/Field.java | 6 + testBytecode/Import.java | 8 + testBytecode/Lam1.java | 10 + testBytecode/LamRun.java | 8 + testBytecode/MethFieldVar.java | 38 +++ testBytecode/Subclass.java | 6 + testBytecode/SuperTest.java | 14 ++ testBytecode/Superclass.java | 6 + testBytecode/TestMyTest.java | 10 + testBytecode/public | 1 + testBytecode/testF.java | 5 + testBytecode/testTets.java | 5 + testBytecode/testTetsF.java | 5 + 35 files changed, 514 insertions(+), 23 deletions(-) create mode 100644 src/de/dhbwstuttgart/.DS_Store rename {de => src/de}/dhbwstuttgart/bytecode/BytecodeGen.java (100%) create mode 100644 src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java rename {de => src/de}/dhbwstuttgart/bytecode/BytecodeGenMethod.java (100%) create mode 100644 src/de/dhbwstuttgart/bytecode/ClassFile.java rename {de => src/de}/dhbwstuttgart/bytecode/Descriptor.java (100%) rename {de => src/de}/dhbwstuttgart/bytecode/Test.java (99%) rename {de => src/de}/dhbwstuttgart/bytecode/TestClass.java (100%) create mode 100644 src/de/dhbwstuttgart/bytecode/TestFields.java rename {de => src/de}/dhbwstuttgart/bytecode/TestIf.java (100%) create mode 100644 src/de/dhbwstuttgart/bytecode/TestMeth.java create mode 100644 src/de/dhbwstuttgart/bytecode/TetsF.java create mode 100644 test/javFiles/EmptyClass.jav create mode 100644 test/javFiles/EmptyMethod.jav create mode 100644 test/javFiles/Op1.jav create mode 100644 test/javFiles/Subclass.jav create mode 100644 test/javFiles/Superclass.jav create mode 100644 testBytecode/Field.java create mode 100644 testBytecode/Import.java create mode 100644 testBytecode/Lam1.java create mode 100644 testBytecode/LamRun.java create mode 100644 testBytecode/MethFieldVar.java create mode 100644 testBytecode/Subclass.java create mode 100644 testBytecode/SuperTest.java create mode 100644 testBytecode/Superclass.java create mode 100644 testBytecode/TestMyTest.java create mode 100644 testBytecode/public create mode 100644 testBytecode/testF.java create mode 100644 testBytecode/testTets.java create mode 100644 testBytecode/testTetsF.java diff --git a/.gitignore b/.gitignore index 77e5f2f1..e9a5b480 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin .classpath *.iml .idea/ +/target/ diff --git a/src/de/dhbwstuttgart/.DS_Store b/src/de/dhbwstuttgart/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f20299e87666851977a6b404d3ad84ba10b1fb67 GIT binary patch literal 6148 zcmeHKyG{c!5S)b+C()#&^e^xSRusM?S@aP+PZk0*V;yZqkGN|-Hr30aENkDjB?C{m*eM1 b%Dm=t?)SnuG3bm3ov5Dy*F`1;{#$`x+h7%W literal 0 HcmV?d00001 diff --git a/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java similarity index 100% rename from de/dhbwstuttgart/bytecode/BytecodeGen.java rename to src/de/dhbwstuttgart/bytecode/BytecodeGen.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java new file mode 100644 index 00000000..4b1557b0 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenLambda.java @@ -0,0 +1,227 @@ +package de.dhbwstuttgart.bytecode; + +import org.objectweb.asm.MethodVisitor; + +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; +import de.dhbwstuttgart.syntaxtree.statement.Assign; +import de.dhbwstuttgart.syntaxtree.statement.AssignToField; +import de.dhbwstuttgart.syntaxtree.statement.Binary; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.CastExpr; +import de.dhbwstuttgart.syntaxtree.statement.DoStmt; +import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt; +import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.ForStmt; +import de.dhbwstuttgart.syntaxtree.statement.IfStmt; +import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.statement.MethodCall; +import de.dhbwstuttgart.syntaxtree.statement.NewArray; +import de.dhbwstuttgart.syntaxtree.statement.NewClass; +import de.dhbwstuttgart.syntaxtree.statement.Receiver; +import de.dhbwstuttgart.syntaxtree.statement.Return; +import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid; +import de.dhbwstuttgart.syntaxtree.statement.StaticClassName; +import de.dhbwstuttgart.syntaxtree.statement.Super; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.syntaxtree.statement.This; +import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus; +import de.dhbwstuttgart.syntaxtree.statement.WhileStmt; +import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; +import de.dhbwstuttgart.syntaxtree.statement.literal.Null; + +public class BytecodeGenLambda implements StatementVisitor{ + private LambdaExpression lambdaExpression; + private MethodVisitor mv; + + public BytecodeGenLambda(LambdaExpression lambdaExpression, MethodVisitor mv) { + this.lambdaExpression = lambdaExpression; + this.mv = mv; + + + } + + @Override + public void visit(ArgumentList argumentList) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(LambdaExpression lambdaExpression) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Assign assign) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Binary binary) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Block block) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(CastExpr castExpr) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(EmptyStmt emptyStmt) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(FieldVar fieldVar) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(ForStmt forStmt) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(IfStmt ifStmt) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(InstanceOf instanceOf) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(LocalVar localVar) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(MethodCall methodCall) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(NewClass methodCall) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(NewArray newArray) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Receiver receiver) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Return aReturn) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(ReturnVoid aReturn) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(StaticClassName staticClassName) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Super aSuper) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(This aThis) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(UnaryPlus unaryPlus) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(WhileStmt whileStmt) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(DoStmt whileStmt) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Null aNull) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(Literal literal) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(AssignToField assignLeftSide) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + // TODO Auto-generated method stub + + } + + @Override + public void visit(SuperCall superCall) { + // TODO Auto-generated method stub + + } +} diff --git a/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java similarity index 100% rename from de/dhbwstuttgart/bytecode/BytecodeGenMethod.java rename to src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java diff --git a/src/de/dhbwstuttgart/bytecode/ClassFile.java b/src/de/dhbwstuttgart/bytecode/ClassFile.java new file mode 100644 index 00000000..fcbad90b --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/ClassFile.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.bytecode; + +public class ClassFile { + + String name; + byte[] bytecode; + + public ClassFile(String name, byte[] bytecode) { + this.name = name; + this.bytecode = bytecode; + } + + +} diff --git a/de/dhbwstuttgart/bytecode/Descriptor.java b/src/de/dhbwstuttgart/bytecode/Descriptor.java similarity index 100% rename from de/dhbwstuttgart/bytecode/Descriptor.java rename to src/de/dhbwstuttgart/bytecode/Descriptor.java diff --git a/de/dhbwstuttgart/bytecode/Test.java b/src/de/dhbwstuttgart/bytecode/Test.java similarity index 99% rename from de/dhbwstuttgart/bytecode/Test.java rename to src/de/dhbwstuttgart/bytecode/Test.java index f908655f..333ec41f 100644 --- a/de/dhbwstuttgart/bytecode/Test.java +++ b/src/de/dhbwstuttgart/bytecode/Test.java @@ -20,7 +20,6 @@ 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/"; diff --git a/de/dhbwstuttgart/bytecode/TestClass.java b/src/de/dhbwstuttgart/bytecode/TestClass.java similarity index 100% rename from de/dhbwstuttgart/bytecode/TestClass.java rename to src/de/dhbwstuttgart/bytecode/TestClass.java diff --git a/src/de/dhbwstuttgart/bytecode/TestFields.java b/src/de/dhbwstuttgart/bytecode/TestFields.java new file mode 100644 index 00000000..652b8d19 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TestFields.java @@ -0,0 +1,59 @@ +package de.dhbwstuttgart.bytecode; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +public class TestFields { + private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; + + public static void main(String[] args) { + // TODO Auto-generated method stub + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TetsF", null, "java/lang/Object", null); + + cw.visitSource("TetsF.java", null); + + FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "z", Type.INT_TYPE.getDescriptor(), null, null); + fv.visitEnd(); + + FieldVisitor fvS = cw.visitField(Opcodes.ACC_PUBLIC, "s", "Ljava/lang/String;", null, null); + fvS.visitEnd(); + // Create Constructor + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); + mv.visitCode(); + + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitLdcInsn(""); + mv.visitFieldInsn(Opcodes.PUTFIELD, "TetsF", "s", "Ljava/lang/String;"); + mv.visitInsn(Opcodes.RETURN); + mv.visitMaxs(2, 1); + mv.visitEnd(); + + byte[] b = cw.toByteArray(); + + FileOutputStream output; + + try { + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TetsF.class")); + output.write(b); + output.close(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/de/dhbwstuttgart/bytecode/TestIf.java b/src/de/dhbwstuttgart/bytecode/TestIf.java similarity index 100% rename from de/dhbwstuttgart/bytecode/TestIf.java rename to src/de/dhbwstuttgart/bytecode/TestIf.java diff --git a/src/de/dhbwstuttgart/bytecode/TestMeth.java b/src/de/dhbwstuttgart/bytecode/TestMeth.java new file mode 100644 index 00000000..51223ea1 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TestMeth.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.bytecode; + +public class TestMeth { + private int z; + public String s; + public TestMeth(int temp) { + this.z = temp; + } + public void m1(int a, int b) { + int y = m2(1,2,3,4); + } + + public int m2(int a, int b, int x, int y) { + Integer c = 55; + Integer g; + return a+b+y+c; + } +} diff --git a/src/de/dhbwstuttgart/bytecode/TetsF.java b/src/de/dhbwstuttgart/bytecode/TetsF.java new file mode 100644 index 00000000..af72a8d3 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TetsF.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.bytecode; + +public class TetsF { + private int z; + public String s = ""; +} diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 99f1badf..fab0922e 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -1,11 +1,8 @@ package de.dhbwstuttgart.core; -<<<<<<< HEAD -import de.dhbwstuttgart.bytecode.BytecodeGen; -import de.dhbwstuttgart.exceptions.DebugException; -======= + import de.dhbwstuttgart.environment.CompilationEnvironment; ->>>>>>> 6738eecdf37ce8827512c7ac8d254341de7fbf9d + import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java index f06ddeb9..ed5a082b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ArgumentList.java @@ -21,9 +21,6 @@ public class ArgumentList extends SyntaxTreeNode return expr; } - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } @Override public void accept(ASTVisitor visitor) { diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index c11fee33..9ca91abb 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -2,7 +2,6 @@ package bytecode; import de.dhbwstuttgart.bytecode.BytecodeGen; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.syntaxtree.SourceFile; import org.junit.Test; @@ -17,7 +16,6 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Set; import static org.junit.Assert.*; @@ -28,21 +26,14 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, java.lang.ClassNotFoundException { -<<<<<<< HEAD - filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); -======= + System.out.println(rootDirectory); filesToTest.add(new File(rootDirectory+"EmptyClass.jav")); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); ->>>>>>> 6738eecdf37ce8827512c7ac8d254341de7fbf9d + System.out.println("test"); for(File f : filesToTest){ String content = readFile(f.getPath(), StandardCharsets.UTF_8); -<<<<<<< HEAD - HashMap bytecode = this.getBytecode(sf); - this.writeClassFile(bytecode, "EmptyMethod"); -======= HashMap bytecode = this.getBytecode(compiler.sourceFiles.get(f)); this.writeClassFile(bytecode, "EmptyClass"); ->>>>>>> 6738eecdf37ce8827512c7ac8d254341de7fbf9d } } diff --git a/test/javFiles/EmptyClass.jav b/test/javFiles/EmptyClass.jav new file mode 100644 index 00000000..8160d7da --- /dev/null +++ b/test/javFiles/EmptyClass.jav @@ -0,0 +1,3 @@ +public class EmptyClass{ + +} \ No newline at end of file diff --git a/test/javFiles/EmptyMethod.jav b/test/javFiles/EmptyMethod.jav new file mode 100644 index 00000000..dd28fc69 --- /dev/null +++ b/test/javFiles/EmptyMethod.jav @@ -0,0 +1,37 @@ +public class EmptyMethod{ + static String s1 =""; + String s2; + public void m1(){ + //String s = ""; + System.out.println("test"); + //Integer ab = Math.abs(1); + //Math.abs(1); + //String lV = "local"; + //s1 = "1"; + //s1.concat("2"); + s2 = s1; + //m2(); + Clazz i = new Clazz(); + Integer i = new Integer(1); + } + + public void m2(){} +} + +class Clazz{} +/* +public class EmptyMethod2{ + public static test = "5"; + public void m1(Integer i, String j, Boolean b){ + //String s = ""; + EmptyMethod em = new EmptyMethod(); + em.m1(); + em.s1 = ""; + //Integer ab = Math.abs(1); + //Math.abs(1); + //String lV = "local"; + //s1 = "1"; + //s1.concat("2"); + //s2 = s1; + } +}*/ \ No newline at end of file diff --git a/test/javFiles/Op1.jav b/test/javFiles/Op1.jav new file mode 100644 index 00000000..800f58b8 --- /dev/null +++ b/test/javFiles/Op1.jav @@ -0,0 +1,11 @@ +public class Op1{ + public Op1() { + + Runnable lam = () -> { + String test = ""; + String b = "b"; + test = b; + System.out.println(test);}; + //lam.run(); + } +} \ No newline at end of file diff --git a/test/javFiles/Subclass.jav b/test/javFiles/Subclass.jav new file mode 100644 index 00000000..fe6e9208 --- /dev/null +++ b/test/javFiles/Subclass.jav @@ -0,0 +1,6 @@ +public class Subclass extends Superclass { + + public void printMethod() { + super.printMethod(); + } +} \ No newline at end of file diff --git a/test/javFiles/Superclass.jav b/test/javFiles/Superclass.jav new file mode 100644 index 00000000..d58089de --- /dev/null +++ b/test/javFiles/Superclass.jav @@ -0,0 +1,6 @@ +public class Superclass { + + public void printMethod() { + System.out.println("Printed in Superclass."); + } +} diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 1a3cb57c..4b79b8c5 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -36,10 +36,7 @@ public class JavaTXCompilerTest { //filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); //filesToTest.add(new File(rootDirectory+"test.jav")); -<<<<<<< HEAD filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); -======= ->>>>>>> 6738eecdf37ce8827512c7ac8d254341de7fbf9d //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); diff --git a/testBytecode/Field.java b/testBytecode/Field.java new file mode 100644 index 00000000..50002082 --- /dev/null +++ b/testBytecode/Field.java @@ -0,0 +1,6 @@ +public class Field{ + public void m(){ + MethFieldVar mF = new MethFieldVar(); + mF.s1 = "Field S1"; + } +} diff --git a/testBytecode/Import.java b/testBytecode/Import.java new file mode 100644 index 00000000..f3519acb --- /dev/null +++ b/testBytecode/Import.java @@ -0,0 +1,8 @@ +import java.util.Vector; + +class Import { + void methode(){ + Vector v = new Vector(); + v.add(v); + } +} diff --git a/testBytecode/Lam1.java b/testBytecode/Lam1.java new file mode 100644 index 00000000..142feed2 --- /dev/null +++ b/testBytecode/Lam1.java @@ -0,0 +1,10 @@ +import java.util.function.Function; +public class Lam1{ + public Lam1() { + Function fun = (x) -> x+"1"; + fun.apply("2"); + + Runnable lam = () -> System.out.println("lambda"); + lam.run(); + } +} diff --git a/testBytecode/LamRun.java b/testBytecode/LamRun.java new file mode 100644 index 00000000..f4920700 --- /dev/null +++ b/testBytecode/LamRun.java @@ -0,0 +1,8 @@ +public class LamRun{ + + public LamRun(){ + + Runnable lam = () -> System.out.println("lambda"); + lam.run(); + } +} diff --git a/testBytecode/MethFieldVar.java b/testBytecode/MethFieldVar.java new file mode 100644 index 00000000..74086a01 --- /dev/null +++ b/testBytecode/MethFieldVar.java @@ -0,0 +1,38 @@ +public class MethFieldVar{ + String s1;// = ""; + String s2; + +/* public void meth(Integer i, String j, Boolean b){ + //String local = "a"; + //int localL = local.length(); + //int l = s.length(); + String s = null; + //s = ""; + //return s.length();//l+localL; + } +*/ + public void mm(){ +// return "mm"; + } + public void m2(){ + System.out.println(""); + // Math.abs(1); + // String lV = "local"; + // s1 = "1"; + // s1.concat("2"); + s2 = s1; + + mm(); + + Clazz i = new Clazz(); + + Runnable lam = ()->{ + String test = ""; + String b = "b"; + test = b; + System.out.println(test); + }; + } +} + +class Clazz{} diff --git a/testBytecode/Subclass.java b/testBytecode/Subclass.java new file mode 100644 index 00000000..3e8b5853 --- /dev/null +++ b/testBytecode/Subclass.java @@ -0,0 +1,6 @@ +public class Subclass extends Superclass { + + public void printMethod() { + super.printMethod(); + } +} diff --git a/testBytecode/SuperTest.java b/testBytecode/SuperTest.java new file mode 100644 index 00000000..1b6ac268 --- /dev/null +++ b/testBytecode/SuperTest.java @@ -0,0 +1,14 @@ +public class Superclass { + + public void printMethod() { + System.out.println("Printed in Superclass."); + } +} + +public class Subclass extends Superclass { + + public void printMethod() { + super.printMethod(); + + } +} diff --git a/testBytecode/Superclass.java b/testBytecode/Superclass.java new file mode 100644 index 00000000..d58089de --- /dev/null +++ b/testBytecode/Superclass.java @@ -0,0 +1,6 @@ +public class Superclass { + + public void printMethod() { + System.out.println("Printed in Superclass."); + } +} diff --git a/testBytecode/TestMyTest.java b/testBytecode/TestMyTest.java new file mode 100644 index 00000000..1b3db588 --- /dev/null +++ b/testBytecode/TestMyTest.java @@ -0,0 +1,10 @@ +class TestMyTest{ +public static void main(String[] a){ + //test1 + //new TestClass(); + //test if statement + //new TestIf(new Boolean(true)); + // test lambda + new TestClass(); +} +} diff --git a/testBytecode/public b/testBytecode/public new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/testBytecode/public @@ -0,0 +1 @@ + diff --git a/testBytecode/testF.java b/testBytecode/testF.java new file mode 100644 index 00000000..fa4a50b7 --- /dev/null +++ b/testBytecode/testF.java @@ -0,0 +1,5 @@ +public class testTets(){ + public static void main(String[] args){ + new tetsF(); + } +} diff --git a/testBytecode/testTets.java b/testBytecode/testTets.java new file mode 100644 index 00000000..4f95cbc0 --- /dev/null +++ b/testBytecode/testTets.java @@ -0,0 +1,5 @@ +public class testTets{ + public static void main(String[] args){ + new TetsF(); + } +} diff --git a/testBytecode/testTetsF.java b/testBytecode/testTetsF.java new file mode 100644 index 00000000..fa4a50b7 --- /dev/null +++ b/testBytecode/testTetsF.java @@ -0,0 +1,5 @@ +public class testTets(){ + public static void main(String[] args){ + new tetsF(); + } +}