From e07b189ba467429685f4d692561be97eed8b2b63 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Sat, 29 Dec 2018 21:49:01 +0100 Subject: [PATCH] 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 f67b9c4de..0dea801d8 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 6fc803479..d46702891 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 b897684ab..aeefda627 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); }