Merge branch 'bytecode2' into plugin

This commit is contained in:
Michael Uhl 2019-01-04 21:26:29 +01:00
commit 898aedcb4a
6 changed files with 77 additions and 22 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<>();
@ -292,10 +293,14 @@ public class BytecodeGen implements ASTVisitor {
desc = constructor.accept(new DescriptorToString(resultSet)); desc = constructor.accept(new DescriptorToString(resultSet));
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

@ -85,13 +85,15 @@ public class BytecodeGenMethod implements StatementVisitor {
private boolean isRightSideALambda = false; private boolean isRightSideALambda = false;
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,20 +637,20 @@ 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);
ArrayList<String> usedVars = kindOfLambda.getUsedVars(); ArrayList<String> usedVars = kindOfLambda.getUsedVars();
new BytecodeGenMethod(lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, new BytecodeGenMethod(lambdaExpression, usedVars,this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
classFiles,this.path, lamCounter, sf); classFiles,this.path, lamCounter, sf);
mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitMaxs(0, 0);
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);
} }

View File

@ -174,9 +174,11 @@ public class Simplify {
allCons.addAll(result.keySet()); allCons.addAll(result.keySet());
if(!allCons.isEmpty() && allCons.size()<2) { if(!allCons.isEmpty() && allCons.size()<2) {
TPHConstraint cons = allCons.get(0);
if(!result.containsKey(allCons.get(0))) if(!result.containsKey(cons)) {
result.put(allCons.get(0), null); result.put(cons, null);
result.put(new ExtendsConstraint(cons.getRight(), Type.getInternalName(Object.class), Relation.EXTENDS), null);
}
return result; return result;
} }

View File

@ -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();
}
}

View File

@ -0,0 +1,6 @@
public class Id {
id(b){
return b;
}
}