generateBCForFun() um Parameter path erweitert.

This commit is contained in:
Fayez Abu Alia 2018-06-20 18:15:44 +02:00
parent 303cfa5409
commit 11649b39d3
3 changed files with 17 additions and 11 deletions

View File

@ -44,6 +44,9 @@ public class BytecodeGen implements ASTVisitor {
private boolean isInterface;
private List<ResultSet> listOfResultSets;
private ResultSet resultSet;
private String path;
private int indexOfFirstParam = 0;
private String superClass;
@ -64,16 +67,17 @@ public class BytecodeGen implements ASTVisitor {
ArrayList<String> methodNameAndParamsT = new ArrayList<>();
public BytecodeGen(HashMap<String,byte[]> classFiles, List<ResultSet> listOfResultSets) {
public BytecodeGen(HashMap<String,byte[]> classFiles, List<ResultSet> listOfResultSets, String path) {
this.classFiles = classFiles;
this.listOfResultSets = listOfResultSets;
this.path = path;
}
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()) {
System.out.println("in Class: " + cl.getClassName().toString());
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets);
BytecodeGen classGen = new BytecodeGen(classFiles, listOfResultSets,path);
cl.accept(classGen);
classGen.writeClass(cl.getClassName().toString());
}
@ -190,7 +194,7 @@ public class BytecodeGen implements ASTVisitor {
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", desc, sig, null);
mv.visitCode();
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,field, mv,paramsAndLocals,cw,
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles);
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, path);
if(!field.getParameterList().iterator().hasNext()) {
mv.visitInsn(Opcodes.RETURN);
}
@ -264,7 +268,7 @@ public class BytecodeGen implements ASTVisitor {
mv.visitCode();
BytecodeGenMethod gen = new BytecodeGenMethod(className,superClass,resultSet,method, mv,paramsAndLocals,cw,
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles);
genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles, path);
mv.visitMaxs(0, 0);
mv.visitEnd();

View File

@ -57,7 +57,7 @@ public class BytecodeGenMethod implements StatementVisitor {
private HashMap<String, String> genericsAndBounds;
private boolean isBinaryExp = false;
private String superClass;
private String path;
private IStatement statement = null;
// for tests **
@ -73,7 +73,7 @@ public class BytecodeGenMethod implements StatementVisitor {
public BytecodeGenMethod(String className, String superClass,ResultSet resultSet, Method m, MethodVisitor mv,
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, String path) {
this.className = className;
this.superClass = superClass;
@ -86,6 +86,7 @@ public class BytecodeGenMethod implements StatementVisitor {
this.genericsAndBounds = genericsAndBounds;
this.isInterface = isInterface;
this.classFiles = classFiles;
this.path = path;
if (!isInterface)
this.m.block.accept(this);
@ -93,12 +94,13 @@ public class BytecodeGenMethod implements StatementVisitor {
}
public BytecodeGenMethod(LambdaExpression lambdaExpression, ResultSet resultSet, MethodVisitor mv,
int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles) {
int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path) {
this.resultSet = resultSet;
this.mv = mv;
this.isInterface = isInterface;
this.classFiles = classFiles;
this.path = path;
Iterator<FormalParameter> itr = lambdaExpression.params.iterator();
int i = indexOfFirstParamLam;
@ -534,7 +536,7 @@ public class BytecodeGenMethod implements StatementVisitor {
methodName, arg3.toString(), null, null);
new BytecodeGenMethod(lambdaExpression, this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
classFiles);
classFiles,this.path);
mvLambdaBody.visitMaxs(0, 0);
mvLambdaBody.visitEnd();
@ -575,7 +577,7 @@ public class BytecodeGenMethod implements StatementVisitor {
try {
System.out.println("generating " + name + ".class file...");
output = new FileOutputStream(
new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" + name + ".class"));
new File(path + name + ".class"));
output.write(bytecode);
output.close();
System.out.println(name + ".class file generated");

View File

@ -246,7 +246,7 @@ public class JavaTXCompiler {
HashMap<String,byte[]> classFiles = new HashMap<>();
SourceFile sf = sourceFiles.get(f);
List<ResultSet> typeinferenceResult = this.typeInference();
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult);
BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult,path);
// BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult.get(0));
bytecodeGen.visit(sf);
this.writeClassFile(bytecodeGen.getClassFiles(), path);