Rekursiver Aufruf von Atrributen funktioniert

This commit is contained in:
Fayez Abu Alia 2018-11-08 13:02:33 +01:00
parent ea65a87336
commit d27e0af57c
6 changed files with 82 additions and 57 deletions

View File

@ -69,8 +69,11 @@ public class ArgumentVisitor implements StatementVisitor {
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
binary.accept(bytecodeGenMethod); binary.accept(bytecodeGenMethod);
if(argListMethCall.get(0)) if(argListMethCall.get(0)) {
bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolvedType(binary.getType())); bytecodeGenMethod.doUnboxing(bytecodeGenMethod.getResolvedType(binary.getType()));
} else {
bytecodeGenMethod.doBoxing(bytecodeGenMethod.getResolvedType(binary.getType()));
}
argListMethCall.remove(0); argListMethCall.remove(0);
} }

View File

@ -374,7 +374,10 @@ public class BytecodeGen implements ASTVisitor {
@Override @Override
public void visit(Field field) { public void visit(Field field) {
System.out.println("In Field ---"); System.out.println("In Field ---");
cw.visitField(field.modifier, field.getName(),resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToSignature()), null, null); cw.visitField(field.modifier, field.getName(),
"L"+resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToDescriptor())+";",
resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToSignature()),
null);
} }
@Override @Override

View File

@ -110,14 +110,14 @@ public class BytecodeGenMethod implements StatementVisitor {
} }
public BytecodeGenMethod(LambdaExpression lambdaExpression, ResultSet resultSet, MethodVisitor mv, public BytecodeGenMethod(LambdaExpression lambdaExpression, ResultSet resultSet, MethodVisitor mv,
int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path) { int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path, int lamCounter) {
this.resultSet = resultSet; this.resultSet = resultSet;
this.mv = mv; this.mv = mv;
this.isInterface = isInterface; this.isInterface = isInterface;
this.classFiles = classFiles; this.classFiles = classFiles;
this.path = path; this.path = path;
this.lamCounter = lamCounter;
Iterator<FormalParameter> itr = lambdaExpression.params.iterator(); Iterator<FormalParameter> itr = lambdaExpression.params.iterator();
int i = indexOfFirstParamLam; int i = indexOfFirstParamLam;
while (itr.hasNext()) { while (itr.hasNext()) {
@ -567,7 +567,7 @@ public class BytecodeGenMethod implements StatementVisitor {
methodName, arg3.toString(), null, null); methodName, arg3.toString(), null, null);
new BytecodeGenMethod(lambdaExpression, this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface, new BytecodeGenMethod(lambdaExpression, this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
classFiles,this.path); classFiles,this.path, lamCounter);
mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitMaxs(0, 0);
mvLambdaBody.visitEnd(); mvLambdaBody.visitEnd();
@ -674,7 +674,7 @@ public class BytecodeGenMethod implements StatementVisitor {
System.out.println("Methods of " + receiverName + " "); System.out.println("Methods of " + receiverName + " ");
ClassLoader cLoader = ClassLoader.getSystemClassLoader(); ClassLoader cLoader = ClassLoader.getSystemClassLoader();
// This will be used if the class is not standard class (not in API) // This will be used if the class is not standard class (not in API)
// ClassLoader cLoader2; ClassLoader cLoader2;
java.lang.reflect.Method methodRefl = null; java.lang.reflect.Method methodRefl = null;
String clazz = receiverName.replace("/", "."); String clazz = receiverName.replace("/", ".");
@ -688,15 +688,15 @@ public class BytecodeGenMethod implements StatementVisitor {
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods); methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
} catch (Exception e) { } catch (Exception e) {
// try { try {
// cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)}); cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)});
// java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods(); java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods();
// System.out.println("Methods of " + receiverName + " "); System.out.println("Methods of " + receiverName + " ");
// for(int i = 0; i<methods.length; i++) { for(int i = 0; i<methods.length; i++) {
// System.out.println(methods[i]); System.out.println(methods[i]);
// } }
// methodRefl = getMethod(methodCall.name,methods); methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
// }catch (Exception e2) { }catch (Exception e2) {
String superClass = ""; String superClass = "";
// TODO: Test SubMatrix.jav // TODO: Test SubMatrix.jav
while(true) { while(true) {
@ -731,7 +731,7 @@ public class BytecodeGenMethod implements StatementVisitor {
receiverName = superClass; receiverName = superClass;
continue; continue;
} }
// } }
} }
} }
@ -1124,7 +1124,7 @@ public class BytecodeGenMethod implements StatementVisitor {
} }
// Boxing: prim -> RefType // Boxing: prim -> RefType
private void doBoxing(String type) { public void doBoxing(String type) {
switch (type) { switch (type) {
case "java/lang/String": case "java/lang/String":
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",

View File

@ -46,8 +46,8 @@ public class KindOfLambda implements StatementVisitor{
@Override @Override
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
// TODO Auto-generated method stub binary.lexpr.accept(this);
binary.rexpr.accept(this);
} }
@Override @Override
@ -82,8 +82,9 @@ public class KindOfLambda implements StatementVisitor{
@Override @Override
public void visit(IfStmt ifStmt) { public void visit(IfStmt ifStmt) {
// TODO Auto-generated method stub ifStmt.expr.accept(this);
ifStmt.then_block.accept(this);
ifStmt.else_block.accept(this);
} }
@Override @Override
@ -156,8 +157,10 @@ public class KindOfLambda implements StatementVisitor{
@Override @Override
public void visit(This aThis) { public void visit(This aThis) {
this.isInstanceCapturingLambda = true; if(!isInstanceCapturingLambda) {
this.argumentList.add(aThis.getType()); this.isInstanceCapturingLambda = true;
this.argumentList.add(aThis.getType());
}
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package bytecode;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.File; import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
@ -32,17 +33,19 @@ public class FacultyTest {
classToTest = loader.loadClass("Faculty"); classToTest = loader.loadClass("Faculty");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
Method m = classToTest.getDeclaredMethod("m", Integer.class); // Method m = classToTest.getDeclaredMethod("m", Integer.class);
Field fact = classToTest.getDeclaredField("fact");
// Class<?> lambda = m.invoke(instanceOfClass).getClass(); // Class<?> lambda = m.invoke(instanceOfClass).getClass();
// Method apply = lambda.getMethod("apply", Object.class); Class<?> lambda = fact.getType();
Method apply = lambda.getMethod("apply", Object.class);
// //
// // Damit man auf die Methode zugreifen kann // // Damit man auf die Methode zugreifen kann
// apply.setAccessible(true); apply.setAccessible(true);
Integer i = 3; Integer i = 3;
// Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i); Integer result = (Integer) apply.invoke(instanceOfClass, i);
Integer result = (Integer) m.invoke(instanceOfClass,i); // Integer result = (Integer) m.invoke(instanceOfClass,i);
assertEquals(6, result); assertEquals(6, result);
} }

View File

@ -1,33 +1,46 @@
import java.lang.Integer; import java.lang.Integer;
public class Faculty { public class Faculty {
public fact;
m (x) { Faculty() {
fact = (x) -> {
// var fact = (x) -> { if(x<0) {
// if (x == 1) { return 0;
// return x; }else if (x < 1) {
// } return x;
// else { }
// return x * (fact.apply(x-1)); else {
// } return x * (fact.apply(x-1));
// }; }
// return fact; };
// var x = 13; }
// if(x>22) {
// return 0; // m (x) {
// }else if(x <1){ //
// return x; //// var fact = (x) -> {
// }else { //// if (x == 1) {
// return 1; //// return x;
// } //// }
//// else {
if (x < 0) { //// return x * (fact.apply(x-1));
return 0; //// }
}else if(x<2) { //// };
return x; //// return fact;
} else { //// var x = 13;
return x * m(x-1); //// if(x>22) {
} //// return 0;
} //// }else if(x <1){
//// return x;
//// }else {
//// return 1;
//// }
//
// if (x < 0) {
// return 0;
// }else if(x<2) {
// return x;
// } else {
// return x * m(x-1);
// }
// }
} }