From e07b189ba467429685f4d692561be97eed8b2b63 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Sat, 29 Dec 2018 21:49:01 +0100 Subject: [PATCH 1/2] MatrixOP funktioniert (mit JUnit wird class file erzeugt) und dann mit TestBytecode.java getestet --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 9 +++-- .../bytecode/BytecodeGenMethod.java | 33 ++++++++++--------- .../bytecode/utilities/KindOfLambda.java | 5 +-- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java index f67b9c4d..0dea801d 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -70,6 +70,7 @@ public class BytecodeGen implements ASTVisitor { // stores generics and their bounds of class HashMap genericsAndBounds = new HashMap<>(); + private int constructorPos = 0; private final TPHExtractor tphExtractor = new TPHExtractor(); private final ArrayList commonPairs = new ArrayList<>(); @@ -292,10 +293,14 @@ public class BytecodeGen implements ASTVisitor { desc = constructor.accept(new DescriptorToString(resultSet)); System.out.println("Constructor: " + field.getName() + " Sig: "+ sig + " Desc: " + desc); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, sig, null); - mv.visitCode(); + mv.visitCode(); + Block block = fieldInitializations.get().block; + + constructorPos += 1; + BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw, - genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path, block); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, sf,path, block, constructorPos); if(!field.getParameterList().iterator().hasNext() && !(field.block.statements.get(field.block.statements.size()-1) instanceof ReturnVoid)) { mv.visitInsn(Opcodes.RETURN); } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 6fc80347..d4670289 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -85,13 +85,15 @@ public class BytecodeGenMethod implements StatementVisitor { private boolean isRightSideALambda = false; private KindOfLambda kindOfLambda; private HashMap classFiles; + + private int constructorPos = 0; private ArrayList varsFunInterface = new ArrayList<>();; // generate bytecode for constructor public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface, HashMap classFiles, - SourceFile sf,String path, Block block) { + SourceFile sf,String path, Block block, int constructorPos) { this.className = className; this.superClass = superClass; @@ -106,6 +108,7 @@ public class BytecodeGenMethod implements StatementVisitor { this.classFiles = classFiles; this.sf = sf; this.path = path; + this.constructorPos = constructorPos; if(block != null) this.blockFieldInit = block; this.m.block.accept(this); @@ -634,20 +637,20 @@ public class BytecodeGenMethod implements StatementVisitor { // Desc: (this/nothing)TargetType String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2, arg3); - - MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE + staticOrInstance + Opcodes.ACC_SYNTHETIC, - methodName, newDesc, null, null); - - ArrayList usedVars = kindOfLambda.getUsedVars(); - - new BytecodeGenMethod(lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, - classFiles,this.path, lamCounter, sf); - - mvLambdaBody.visitMaxs(0, 0); - mvLambdaBody.visitEnd(); - cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", - Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - + if(constructorPos<2) { + MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE + staticOrInstance + Opcodes.ACC_SYNTHETIC, + methodName, newDesc, null, null); + + ArrayList usedVars = kindOfLambda.getUsedVars(); + + new BytecodeGenMethod(lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, + classFiles,this.path, lamCounter, sf); + + mvLambdaBody.visitMaxs(0, 0); + mvLambdaBody.visitEnd(); + cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", + Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); + } // generateBCForFunN(lambdaExpression, typeErasure); } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/KindOfLambda.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/KindOfLambda.java index b897684a..aeefda62 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/KindOfLambda.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/KindOfLambda.java @@ -162,7 +162,7 @@ public class KindOfLambda implements StatementVisitor{ @Override public void visit(UnaryExpr unaryExpr) { - throw new NotImplementedException(); + } @Override @@ -202,7 +202,8 @@ public class KindOfLambda implements StatementVisitor{ @Override public void visit(WhileStmt whileStmt) { - // TODO Auto-generated method stub + whileStmt.expr.accept(this); + whileStmt.loopBlock.accept(this); } From 9e0a6151fd52e2bb99603fbc7caa9b9ceb7f75ee Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 3 Jan 2019 21:05:36 +0100 Subject: [PATCH 2/2] Bug in der Implemintierung von Simplify Algorithmus gefixt --- .../bytecode/utilities/Simplify.java | 8 ++-- src/test/java/bytecode/IdTest.java | 38 +++++++++++++++++++ src/test/resources/bytecodeJavFiles/Id.jav | 6 +++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/test/java/bytecode/IdTest.java create mode 100644 src/test/resources/bytecodeJavFiles/Id.jav diff --git a/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java b/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java index 49b1b4f4..d19d38dc 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/utilities/Simplify.java @@ -174,9 +174,11 @@ public class Simplify { allCons.addAll(result.keySet()); if(!allCons.isEmpty() && allCons.size()<2) { - - if(!result.containsKey(allCons.get(0))) - result.put(allCons.get(0), null); + TPHConstraint cons = allCons.get(0); + if(!result.containsKey(cons)) { + result.put(cons, null); + result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS), null); + } return result; } diff --git a/src/test/java/bytecode/IdTest.java b/src/test/java/bytecode/IdTest.java new file mode 100644 index 00000000..e60e6990 --- /dev/null +++ b/src/test/java/bytecode/IdTest.java @@ -0,0 +1,38 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Field; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class IdTest { + + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + + @Test + public void test() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecodeJavFiles/Id.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"); + pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Id"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + +} diff --git a/src/test/resources/bytecodeJavFiles/Id.jav b/src/test/resources/bytecodeJavFiles/Id.jav new file mode 100644 index 00000000..6cb5d4b0 --- /dev/null +++ b/src/test/resources/bytecodeJavFiles/Id.jav @@ -0,0 +1,6 @@ +public class Id { + + id(b){ + return b; + } +} \ No newline at end of file