modified: src/de/dhbwstuttgart/bytecode/BytecodeGen.java

modified:   src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
	new file:   test/bytecode/FieldTest.java
	modified:   test/bytecode/MatrixOpTest.java
	new file:   test/bytecode/javFiles/Field.jav
	modified:   test/bytecode/javFiles/Sorting.jav
Boxing-Problem bei methodCall geloest und Tests funktionieren
This commit is contained in:
Fayez Abu Alia 2018-10-17 14:29:12 +02:00
parent 0d83ee8505
commit 94b93c39df
6 changed files with 72 additions and 14 deletions

View File

@ -471,6 +471,7 @@ public class BytecodeGen implements ASTVisitor {
// ??
@Override
public void visit(FieldVar fieldVar) {
System.out.println("In FieldVar ---");
// cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString());
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L"+fieldVar.getType()+";", null, null);
fv.visitEnd();
@ -479,7 +480,8 @@ public class BytecodeGen implements ASTVisitor {
// access flages?? modifiers
@Override
public void visit(Field field) {
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, field.getName(), "L"+field.getType().toString().replace(".", "/")+";", null, null);
System.out.println("In Field ---");
FieldVisitor fv = cw.visitField(field.modifier, field.getName(),resultSet.resolveType(field.getType()).resolvedType.acceptTV(new TypeToDescriptor()), null, null);
fv.visitEnd();
}

View File

@ -71,6 +71,7 @@ public class BytecodeGenMethod implements StatementVisitor {
private SourceFile sf;
private IStatement statement = null;
private boolean isReturnStmt = false;
private boolean isParentBinary = false;
private boolean needDUP = false;
@ -211,7 +212,7 @@ public class BytecodeGenMethod implements StatementVisitor {
@Override
public void visit(BinaryExpr binary) {
isParentBinary = true;
String lexpType = getResolvedType(binary.lexpr.getType());
String rexpType = getResolvedType(binary.rexpr.getType());
@ -242,12 +243,14 @@ public class BytecodeGenMethod implements StatementVisitor {
needDUP = true;
binary.rexpr.accept(this);
isParentBinary = false;
if (!lexpType.equals(rexpType) && !rexpType.equals(largerType))
doCast(rexpType, largerType);
Operator op = binary.operation;
switch (op) {
case ADD:
doVisitAddOpInsn(largerType);
@ -665,6 +668,7 @@ public class BytecodeGenMethod implements StatementVisitor {
@Override
public void visit(MethodCall methodCall) {
boolean parentBinary = isParentBinary;
System.out.println("In MethodCall = " + methodCall.name);
String receiverName = getResolvedType(methodCall.receiver.getType());
System.out.println("Methods of " + receiverName + " ");
@ -681,7 +685,7 @@ public class BytecodeGenMethod implements StatementVisitor {
java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
System.out.println("Methods of " + receiverName + " ");
methodRefl = getMethod(methodCall.name,methods);
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
} catch (Exception e) {
// try {
@ -736,16 +740,19 @@ public class BytecodeGenMethod implements StatementVisitor {
System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
String mDesc = "";
List<Boolean> argListMethCall = new LinkedList<>();
String receiverRefl="";
if(methodRefl == null) {
MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
receiverName, genericsAndBoundsMethod, genericsAndBounds);
mDesc = method.accept(new DescriptorToString(resultSet));
methodCall.arglist.accept(this);
} else {
receiverRefl = methodRefl.getAnnotatedReceiverType().getType().toString();
for(Parameter p:methodRefl.getParameters()) {
System.out.println(p.getName() + " und is Primitive = " + p.getType().isPrimitive());
argListMethCall.add(p.getType().isPrimitive());
}
System.out.println("Receiver = " + methodRefl.getAnnotatedReceiverType().getType().toString());
mDesc = getMethodDesc(methodRefl);
for (Expression al : methodCall.arglist.getArguments()) {
statement = new ArgumentExpr(al);
@ -761,7 +768,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// methodCall.arglist.accept(this);
// is methodCall.receiver functional Interface)?
if (varsFunInterface.contains(methodCall.receiver.getType())) {
if (varsFunInterface.contains(methodCall.receiver.getType()) || (methodRefl!= null && receiverRefl.contains("interface"))) {
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, clazz.replace(".", "/"), methodCall.name,
mDesc, true);
} else {
@ -780,8 +787,8 @@ public class BytecodeGenMethod implements StatementVisitor {
if(isBinaryExp)
doUnboxing(getResolvedType(methodCall.getType()));
}
if(methodRefl == null/* && !isReturnStmt*/) {
System.out.println("ISParent Binary = "+isParentBinary +" -> " + parentBinary);
if(methodRefl == null && (parentBinary || !isReturnStmt)) {
if(isBinaryExp)
doUnboxing(getResolvedType(methodCall.getType()));
}
@ -790,12 +797,13 @@ public class BytecodeGenMethod implements StatementVisitor {
/**
* @param name name of a method
* @param i number of parameters
* @param methods all methods of a class
* @return the method in the class file which its name equals the given methode name
*/
private java.lang.reflect.Method getMethod(String name, java.lang.reflect.Method[] methods) {
private java.lang.reflect.Method getMethod(String name, int i, java.lang.reflect.Method[] methods) {
for(java.lang.reflect.Method m : methods) {
if(name.equals(m.getName())) {
if(name.equals(m.getName()) && i == m.getParameterCount()) {
return m;
}
}
@ -1252,6 +1260,7 @@ public class BytecodeGenMethod implements StatementVisitor {
// array slot onto the top of the operand stack.
assignLeftSide.field.receiver.accept(this);
this.rightSideTemp.accept(this);
System.out.println("Receiver = " + getResolvedType(assignLeftSide.field.receiver.getType()));
mv.visitFieldInsn(Opcodes.PUTFIELD, getResolvedType(assignLeftSide.field.receiver.getType()),
assignLeftSide.field.fieldVarName, getResolvedType(assignLeftSide.field.getType()));
}

View File

@ -0,0 +1,43 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Field;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.URL;
import java.net.URLClassLoader;
import de.dhbwstuttgart.core.JavaTXCompiler;
public class FieldTest {
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;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Field.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Field");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void test() {
Field[] fields = classToTest.getFields();
assertEquals(1, fields.length);
}
}

View File

@ -32,9 +32,9 @@ public class MatrixOpTest {
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
compiler.generateBytecode(pathToClassFile);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("MatrixOP");
// compiler.generateBytecode(pathToClassFile);
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
// classToTest = loader.loadClass("MatrixOP");
/*
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
Vector<Integer> v1 = new Vector<Integer> ();

View File

@ -0,0 +1,4 @@
public class Field {
x = 5;
}

View File

@ -2,7 +2,7 @@ import java.util.List;
import java.util.ArrayList;
import java.lang.String;
class Sorting{
public class Sorting{
merge(a, b){
a.addAll(b);
return a;