MatrixOP funktioniert (mit JUnit wird class file erzeugt) und dann mit TestBytecode.java getestet

This commit is contained in:
Fayez Abu Alia 2018-12-29 21:49:01 +01:00
parent 90c0cd431f
commit e07b189ba4
3 changed files with 28 additions and 19 deletions

View File

@ -70,6 +70,7 @@ public class BytecodeGen implements ASTVisitor {
// stores generics and their bounds of class // stores generics and their bounds of class
HashMap<String, String> genericsAndBounds = new HashMap<>(); HashMap<String, String> genericsAndBounds = new HashMap<>();
private int constructorPos = 0;
private final TPHExtractor tphExtractor = new TPHExtractor(); private final TPHExtractor tphExtractor = new TPHExtractor();
private final ArrayList<GenericInsertPair> commonPairs = new ArrayList<>(); private final ArrayList<GenericInsertPair> commonPairs = new ArrayList<>();
@ -293,9 +294,13 @@ public class BytecodeGen implements ASTVisitor {
System.out.println("Constructor: " + field.getName() + " Sig: "+ sig + " Desc: " + desc); System.out.println("Constructor: " + field.getName() + " Sig: "+ sig + " Desc: " + desc);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, sig, null); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, sig, null);
mv.visitCode(); mv.visitCode();
Block block = fieldInitializations.get().block; Block block = fieldInitializations.get().block;
constructorPos += 1;
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw, 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)) { if(!field.getParameterList().iterator().hasNext() && !(field.block.statements.get(field.block.statements.size()-1) instanceof ReturnVoid)) {
mv.visitInsn(Opcodes.RETURN); mv.visitInsn(Opcodes.RETURN);
} }

View File

@ -86,12 +86,14 @@ public class BytecodeGenMethod implements StatementVisitor {
private KindOfLambda kindOfLambda; private KindOfLambda kindOfLambda;
private HashMap<String, byte[]> classFiles; private HashMap<String, byte[]> classFiles;
private int constructorPos = 0;
private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();; private ArrayList<RefTypeOrTPHOrWildcardOrGeneric> varsFunInterface = new ArrayList<>();;
// generate bytecode for constructor // generate bytecode for constructor
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv, public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod, HashMap<String, Integer> paramsAndLocals, ClassWriter cw, HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles, HashMap<String, String> genericsAndBounds, boolean isInterface, HashMap<String, byte[]> classFiles,
SourceFile sf,String path, Block block) { SourceFile sf,String path, Block block, int constructorPos) {
this.className = className; this.className = className;
this.superClass = superClass; this.superClass = superClass;
@ -106,6 +108,7 @@ public class BytecodeGenMethod implements StatementVisitor {
this.classFiles = classFiles; this.classFiles = classFiles;
this.sf = sf; this.sf = sf;
this.path = path; this.path = path;
this.constructorPos = constructorPos;
if(block != null) if(block != null)
this.blockFieldInit = block; this.blockFieldInit = block;
this.m.block.accept(this); this.m.block.accept(this);
@ -634,7 +637,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// Desc: (this/nothing)TargetType // Desc: (this/nothing)TargetType
String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet));
mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2, arg3); mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2, arg3);
if(constructorPos<2) {
MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE + staticOrInstance + Opcodes.ACC_SYNTHETIC, MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE + staticOrInstance + Opcodes.ACC_SYNTHETIC,
methodName, newDesc, null, null); methodName, newDesc, null, null);
@ -647,7 +650,7 @@ public class BytecodeGenMethod implements StatementVisitor {
mvLambdaBody.visitEnd(); mvLambdaBody.visitEnd();
cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL);
}
// generateBCForFunN(lambdaExpression, typeErasure); // generateBCForFunN(lambdaExpression, typeErasure);
} }

View File

@ -162,7 +162,7 @@ public class KindOfLambda implements StatementVisitor{
@Override @Override
public void visit(UnaryExpr unaryExpr) { public void visit(UnaryExpr unaryExpr) {
throw new NotImplementedException();
} }
@Override @Override
@ -202,7 +202,8 @@ public class KindOfLambda implements StatementVisitor{
@Override @Override
public void visit(WhileStmt whileStmt) { public void visit(WhileStmt whileStmt) {
// TODO Auto-generated method stub whileStmt.expr.accept(this);
whileStmt.loopBlock.accept(this);
} }