diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 165c888c7..f54c6779e 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -212,6 +212,7 @@ public class BytecodeGen implements ASTVisitor { // TODO: check if the method is static => if static then the first param will be stored in pos 0 // else it will be stored in pos 1 and this will be stored in pos 0 String retType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor()); +// String retType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()); String methParamTypes = retType+method.name+"%%"; method.getParameterList().accept(this); @@ -219,6 +220,7 @@ public class BytecodeGen implements ASTVisitor { while(itr.hasNext()) { FormalParameter fp = itr.next(); methParamTypes += resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+";"; +// methParamTypes += resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToSignature())+";"; } if(methodNameAndParamsT.contains(methParamTypes)) { diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 2714965b3..45d6f7e7d 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -9,6 +9,8 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.Parameter; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -503,6 +505,18 @@ public class BytecodeGenMethod implements StatementVisitor { public void visit(LambdaExpression lambdaExpression) { this.lamCounter++; + String typeErasure = "("; + Iterator itr = lambdaExpression.params.iterator(); + while (itr.hasNext()) { + itr.next(); + typeErasure += "L" + Type.getInternalName(Object.class) + ";"; + } + + typeErasure += ")L" + Type.getInternalName(Object.class) + ";"; + + generateBCForFunN(lambdaExpression, typeErasure); + + Lambda lam = new Lambda(lambdaExpression); String lamDesc = lam.accept(new DescriptorToString(resultSet)); // Call site, which, when invoked, returns an instance of the functional @@ -518,20 +532,12 @@ public class BytecodeGenMethod implements StatementVisitor { // Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die // Typlöschung) - String typeErasure = "("; - Iterator itr = lambdaExpression.params.iterator(); - while (itr.hasNext()) { - itr.next(); - typeErasure += "L" + Type.getInternalName(Object.class) + ";"; - } - - typeErasure += ")L" + Type.getInternalName(Object.class) + ";"; // Type erasure Type arg1 = Type.getMethodType(typeErasure); // Type arg1 = Type.getMethodType(lamDesc); // real Type Type arg3 = Type.getMethodType(lamDesc); - + int staticOrSpecial = 0; int staticOrInstance = 0; int indexOfFirstParamLam = 0; @@ -565,7 +571,7 @@ public class BytecodeGenMethod implements StatementVisitor { cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - generateBCForFunN(lambdaExpression, typeErasure); +// generateBCForFunN(lambdaExpression, typeErasure); } private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { @@ -663,55 +669,65 @@ public class BytecodeGenMethod implements StatementVisitor { String receiverName = getResolvedType(methodCall.receiver.getType()); System.out.println("Methods of " + receiverName + " "); ClassLoader cLoader = ClassLoader.getSystemClassLoader(); + // This will be used if the class is not standard class (not in API) +// ClassLoader cLoader2; java.lang.reflect.Method methodRefl = null; String clazz = receiverName.replace("/", "."); + try { if(receiverName.contains("<")) { clazz = clazz.substring(0, receiverName.indexOf("<")); } + java.lang.reflect.Method[] methods = cLoader.loadClass(clazz).getMethods(); System.out.println("Methods of " + receiverName + " "); - for(java.lang.reflect.Method m : methods) { - if(methodCall.name.equals(m.getName())) { - methodRefl = m; - break; - } - } + methodRefl = getMethod(methodCall.name,methods); + } catch (Exception e) { - String superClass = ""; - // TODO: Test SubMatrix.jav - while(true) { - for(ClassOrInterface cl : sf.getClasses()) { - if(receiverName.equals(cl.getClassName().toString())) { - superClass = cl.getSuperClass().getName().toString(); - break; - } - } - System.out.println(superClass); - - if(superClass.equals("")) - break; - - try { - String superClazz = superClass.replace("/", "."); - if(superClass.contains("<")) { - superClazz = superClazz.substring(0, superClass.indexOf("<")); - } - java.lang.reflect.Method[] methods = cLoader.loadClass(superClazz).getMethods(); - System.out.println("Methods of " + superClass + " "); - - for(java.lang.reflect.Method m : methods) { - if(methodCall.name.equals(m.getName())) { - methodRefl = m; +// try { +// cLoader2 = new URLClassLoader(new URL[] {new URL("file://"+path)}); +// java.lang.reflect.Method[] methods = cLoader2.loadClass(clazz).getMethods(); +// System.out.println("Methods of " + receiverName + " "); +// for(int i = 0; i { @Override public String visit(SuperWildcardType superWildcardType) { // throw new NotImplementedException(); - return "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature()); + String sig = "-" + superWildcardType.getInnerType().acceptTV(new TypeToSignature()); + if(superWildcardType.getInnerType() instanceof TypePlaceholder) + sig += ";"; + return sig; } @Override @@ -60,7 +63,10 @@ public class TypeToSignature implements TypeVisitor { @Override public String visit(ExtendsWildcardType extendsWildcardType) { // throw new NotImplementedException(); - return "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature()); + String sig = "+" + extendsWildcardType.getInnerType().acceptTV(new TypeToSignature()); + if(extendsWildcardType.getInnerType() instanceof TypePlaceholder) + sig += ";"; + return sig; } @Override diff --git a/test/bytecode/FacultyTest.java b/test/bytecode/FacultyTest.java index 7b05ece6d..39b0b008c 100644 --- a/test/bytecode/FacultyTest.java +++ b/test/bytecode/FacultyTest.java @@ -32,16 +32,17 @@ public class FacultyTest { classToTest = loader.loadClass("Faculty"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - Method m = classToTest.getDeclaredMethod("m"); - Class lambda = m.invoke(instanceOfClass).getClass(); - Method apply = lambda.getMethod("apply", Object.class); - - // Damit man auf die Methode zugreifen kann - apply.setAccessible(true); + Method m = classToTest.getDeclaredMethod("m", Integer.class); +// Class lambda = m.invoke(instanceOfClass).getClass(); +// Method apply = lambda.getMethod("apply", Object.class); +// +// // Damit man auf die Methode zugreifen kann +// apply.setAccessible(true); Integer i = 3; - Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i); +// Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i); + Integer result = (Integer) m.invoke(instanceOfClass,i); assertEquals(6, result); } diff --git a/test/bytecode/VectorAddTest.java b/test/bytecode/VectorAddTest.java new file mode 100644 index 000000000..68651a400 --- /dev/null +++ b/test/bytecode/VectorAddTest.java @@ -0,0 +1,34 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class VectorAddTest { + 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 generateBC() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/VectorAdd.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("VectorAdd"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } +} diff --git a/test/bytecode/javFiles/Faculty.jav b/test/bytecode/javFiles/Faculty.jav index 794022ab0..f9ff39d65 100644 --- a/test/bytecode/javFiles/Faculty.jav +++ b/test/bytecode/javFiles/Faculty.jav @@ -22,11 +22,12 @@ public class Faculty { // return 1; // } - if (x == 1) { - return x; - } - else { - return x * m(x-1); - } + if (x < 0) { + return 0; + }else if(x<2) { + return x; + } else { + return x * m(x-1); + } } } diff --git a/test/bytecode/javFiles/VectorAdd.jav b/test/bytecode/javFiles/VectorAdd.jav index 4d3eb8ac8..814f46c24 100644 --- a/test/bytecode/javFiles/VectorAdd.jav +++ b/test/bytecode/javFiles/VectorAdd.jav @@ -4,16 +4,16 @@ import java.lang.String; //import java.lang.Byte; //import java.lang.Boolean; -public class vectorAdd { +public class VectorAdd { add(v1, v2) { - var ret = new Vector(); - var i = 0; - var erg; - while(i < v1.size()) { - erg = v1.elementAt(i) + v2.elementAt(i); - ret.addElement(erg); - } - return ret; - } + var ret = new Vector(); + var i = 0; + var erg; + while(i < v1.size()) { + erg = v1.elementAt(i) + v2.elementAt(i); + ret.addElement(erg); + } + return ret; + } } diff --git a/test/bytecode/vectorAddTest.java b/test/bytecode/vectorAddTest.java index 9aa247276..68651a400 100644 --- a/test/bytecode/vectorAddTest.java +++ b/test/bytecode/vectorAddTest.java @@ -1,9 +1,8 @@ package bytecode; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.io.File; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; @@ -12,7 +11,7 @@ import org.junit.Test; import de.dhbwstuttgart.core.JavaTXCompiler; -public class vectorAddTest { +public class VectorAddTest { private static String path; private static File fileToTest; private static JavaTXCompiler compiler; @@ -23,30 +22,13 @@ public class vectorAddTest { @Test public void generateBC() throws Exception { - path = System.getProperty("user.dir")+"/test/bytecode/javFiles/vectorAdd.jav"; + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/VectorAdd.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("vectorAdd"); - /* + classToTest = loader.loadClass("VectorAdd"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - - Method m = classToTest.getDeclaredMethod("m"); - Class lambda = m.invoke(instanceOfClass).getClass(); - Method apply = lambda.getMethod("apply", Object.class); - - // Damit man auf die Methode zugreifen kann - apply.setAccessible(true); - - Integer i = 77; - - Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i); - - assertEquals(77, result); - */ } - - }