forked from JavaTX/JavaCompilerCore
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:
parent
0d83ee8505
commit
94b93c39df
@ -471,6 +471,7 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
// ??
|
// ??
|
||||||
@Override
|
@Override
|
||||||
public void visit(FieldVar fieldVar) {
|
public void visit(FieldVar fieldVar) {
|
||||||
|
System.out.println("In FieldVar ---");
|
||||||
// cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString());
|
// 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);
|
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L"+fieldVar.getType()+";", null, null);
|
||||||
fv.visitEnd();
|
fv.visitEnd();
|
||||||
@ -479,7 +480,8 @@ public class BytecodeGen implements ASTVisitor {
|
|||||||
// access flages?? modifiers
|
// access flages?? modifiers
|
||||||
@Override
|
@Override
|
||||||
public void visit(Field field) {
|
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();
|
fv.visitEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +71,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
private SourceFile sf;
|
private SourceFile sf;
|
||||||
private IStatement statement = null;
|
private IStatement statement = null;
|
||||||
private boolean isReturnStmt = false;
|
private boolean isReturnStmt = false;
|
||||||
|
private boolean isParentBinary = false;
|
||||||
|
|
||||||
private boolean needDUP = false;
|
private boolean needDUP = false;
|
||||||
|
|
||||||
@ -211,7 +212,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(BinaryExpr binary) {
|
public void visit(BinaryExpr binary) {
|
||||||
|
isParentBinary = true;
|
||||||
String lexpType = getResolvedType(binary.lexpr.getType());
|
String lexpType = getResolvedType(binary.lexpr.getType());
|
||||||
String rexpType = getResolvedType(binary.rexpr.getType());
|
String rexpType = getResolvedType(binary.rexpr.getType());
|
||||||
|
|
||||||
@ -242,12 +243,14 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
needDUP = true;
|
needDUP = true;
|
||||||
|
|
||||||
binary.rexpr.accept(this);
|
binary.rexpr.accept(this);
|
||||||
|
|
||||||
|
isParentBinary = false;
|
||||||
|
|
||||||
if (!lexpType.equals(rexpType) && !rexpType.equals(largerType))
|
if (!lexpType.equals(rexpType) && !rexpType.equals(largerType))
|
||||||
doCast(rexpType, largerType);
|
doCast(rexpType, largerType);
|
||||||
|
|
||||||
Operator op = binary.operation;
|
Operator op = binary.operation;
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case ADD:
|
case ADD:
|
||||||
doVisitAddOpInsn(largerType);
|
doVisitAddOpInsn(largerType);
|
||||||
@ -665,6 +668,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MethodCall methodCall) {
|
public void visit(MethodCall methodCall) {
|
||||||
|
boolean parentBinary = isParentBinary;
|
||||||
System.out.println("In MethodCall = " + methodCall.name);
|
System.out.println("In MethodCall = " + methodCall.name);
|
||||||
String receiverName = getResolvedType(methodCall.receiver.getType());
|
String receiverName = getResolvedType(methodCall.receiver.getType());
|
||||||
System.out.println("Methods of " + receiverName + " ");
|
System.out.println("Methods of " + receiverName + " ");
|
||||||
@ -681,7 +685,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
|
|
||||||
java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
|
java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods();
|
||||||
System.out.println("Methods of " + receiverName + " ");
|
System.out.println("Methods of " + receiverName + " ");
|
||||||
methodRefl = getMethod(methodCall.name,methods);
|
methodRefl = getMethod(methodCall.name,methodCall.arglist.getArguments().size(),methods);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// try {
|
// try {
|
||||||
@ -736,16 +740,19 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
|
System.out.println("Methodcall type : " + resultSet.resolveType(methodCall.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
|
||||||
String mDesc = "";
|
String mDesc = "";
|
||||||
List<Boolean> argListMethCall = new LinkedList<>();
|
List<Boolean> argListMethCall = new LinkedList<>();
|
||||||
|
String receiverRefl="";
|
||||||
if(methodRefl == null) {
|
if(methodRefl == null) {
|
||||||
MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
|
MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(),
|
||||||
receiverName, genericsAndBoundsMethod, genericsAndBounds);
|
receiverName, genericsAndBoundsMethod, genericsAndBounds);
|
||||||
mDesc = method.accept(new DescriptorToString(resultSet));
|
mDesc = method.accept(new DescriptorToString(resultSet));
|
||||||
methodCall.arglist.accept(this);
|
methodCall.arglist.accept(this);
|
||||||
} else {
|
} else {
|
||||||
|
receiverRefl = methodRefl.getAnnotatedReceiverType().getType().toString();
|
||||||
for(Parameter p:methodRefl.getParameters()) {
|
for(Parameter p:methodRefl.getParameters()) {
|
||||||
System.out.println(p.getName() + " und is Primitive = " + p.getType().isPrimitive());
|
System.out.println(p.getName() + " und is Primitive = " + p.getType().isPrimitive());
|
||||||
argListMethCall.add(p.getType().isPrimitive());
|
argListMethCall.add(p.getType().isPrimitive());
|
||||||
}
|
}
|
||||||
|
System.out.println("Receiver = " + methodRefl.getAnnotatedReceiverType().getType().toString());
|
||||||
mDesc = getMethodDesc(methodRefl);
|
mDesc = getMethodDesc(methodRefl);
|
||||||
for (Expression al : methodCall.arglist.getArguments()) {
|
for (Expression al : methodCall.arglist.getArguments()) {
|
||||||
statement = new ArgumentExpr(al);
|
statement = new ArgumentExpr(al);
|
||||||
@ -761,7 +768,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
// methodCall.arglist.accept(this);
|
// methodCall.arglist.accept(this);
|
||||||
|
|
||||||
// is methodCall.receiver functional Interface)?
|
// 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,
|
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, clazz.replace(".", "/"), methodCall.name,
|
||||||
mDesc, true);
|
mDesc, true);
|
||||||
} else {
|
} else {
|
||||||
@ -780,8 +787,8 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
if(isBinaryExp)
|
if(isBinaryExp)
|
||||||
doUnboxing(getResolvedType(methodCall.getType()));
|
doUnboxing(getResolvedType(methodCall.getType()));
|
||||||
}
|
}
|
||||||
|
System.out.println("ISParent Binary = "+isParentBinary +" -> " + parentBinary);
|
||||||
if(methodRefl == null/* && !isReturnStmt*/) {
|
if(methodRefl == null && (parentBinary || !isReturnStmt)) {
|
||||||
if(isBinaryExp)
|
if(isBinaryExp)
|
||||||
doUnboxing(getResolvedType(methodCall.getType()));
|
doUnboxing(getResolvedType(methodCall.getType()));
|
||||||
}
|
}
|
||||||
@ -790,12 +797,13 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param name name of a method
|
* @param name name of a method
|
||||||
|
* @param i number of parameters
|
||||||
* @param methods all methods of a class
|
* @param methods all methods of a class
|
||||||
* @return the method in the class file which its name equals the given methode name
|
* @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) {
|
for(java.lang.reflect.Method m : methods) {
|
||||||
if(name.equals(m.getName())) {
|
if(name.equals(m.getName()) && i == m.getParameterCount()) {
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1252,6 +1260,7 @@ public class BytecodeGenMethod implements StatementVisitor {
|
|||||||
// array slot onto the top of the operand stack.
|
// array slot onto the top of the operand stack.
|
||||||
assignLeftSide.field.receiver.accept(this);
|
assignLeftSide.field.receiver.accept(this);
|
||||||
this.rightSideTemp.accept(this);
|
this.rightSideTemp.accept(this);
|
||||||
|
System.out.println("Receiver = " + getResolvedType(assignLeftSide.field.receiver.getType()));
|
||||||
mv.visitFieldInsn(Opcodes.PUTFIELD, getResolvedType(assignLeftSide.field.receiver.getType()),
|
mv.visitFieldInsn(Opcodes.PUTFIELD, getResolvedType(assignLeftSide.field.receiver.getType()),
|
||||||
assignLeftSide.field.fieldVarName, getResolvedType(assignLeftSide.field.getType()));
|
assignLeftSide.field.fieldVarName, getResolvedType(assignLeftSide.field.getType()));
|
||||||
}
|
}
|
||||||
|
43
test/bytecode/FieldTest.java
Normal file
43
test/bytecode/FieldTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -32,9 +32,9 @@ public class MatrixOpTest {
|
|||||||
fileToTest = new File(path);
|
fileToTest = new File(path);
|
||||||
compiler = new JavaTXCompiler(fileToTest);
|
compiler = new JavaTXCompiler(fileToTest);
|
||||||
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
||||||
compiler.generateBytecode(pathToClassFile);
|
// compiler.generateBytecode(pathToClassFile);
|
||||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
// loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||||
classToTest = loader.loadClass("MatrixOP");
|
// classToTest = loader.loadClass("MatrixOP");
|
||||||
/*
|
/*
|
||||||
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
|
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
|
||||||
Vector<Integer> v1 = new Vector<Integer> ();
|
Vector<Integer> v1 = new Vector<Integer> ();
|
||||||
|
4
test/bytecode/javFiles/Field.jav
Normal file
4
test/bytecode/javFiles/Field.jav
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
public class Field {
|
||||||
|
x = 5;
|
||||||
|
}
|
@ -2,7 +2,7 @@ import java.util.List;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.lang.String;
|
import java.lang.String;
|
||||||
|
|
||||||
class Sorting{
|
public class Sorting{
|
||||||
merge(a, b){
|
merge(a, b){
|
||||||
a.addAll(b);
|
a.addAll(b);
|
||||||
return a;
|
return a;
|
||||||
|
Loading…
Reference in New Issue
Block a user