diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java index 8bd512b8..5b6a9e44 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java @@ -39,13 +39,13 @@ public class FunNGenerator implements FunNUtilities{ private final String methodName = "apply"; private final int bytecodeVersion = V1_8; - private final String objectSuperType = Type.getInternalName(Object.class).replace('.','/'); - private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); - private final String objectSignature = applySignature(objectRefType); + private static final String objectSuperType = Type.getInternalName(Object.class).replace('.','/'); + private static final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); + private static final String objectSignature = applySignature(objectRefType); private static String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } private static String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } - private static String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? applySignature(a)+";" : String.format("L%s;", applyDescriptor(a)); } + private static String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? applyNameDescriptor(objectRefType) : String.format("L%s;", applyDescriptor(a)); } @Override public byte[] generateSuperBytecode(int numberArguments) { diff --git a/src/test/java/bytecode/OLFun2Test.java b/src/test/java/bytecode/OLFun2Test.java new file mode 100644 index 00000000..6ec8701b --- /dev/null +++ b/src/test/java/bytecode/OLFun2Test.java @@ -0,0 +1,69 @@ +package bytecode; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.function.Function; + +import general.TestCleanUp; +import org.junit.*; +import de.dhbwstuttgart.core.JavaTXCompiler; +import static org.junit.Assert.*; + +/** + * //ToDo Etienne: Beschreiben + * + * @since Studienarbeit Type Erasure + * @author etiennezink + */ +public class OLFun2Test { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static Class classFun1IntInt; + private static Class classFun1DoubleDouble; + private static Class classFun1StringString; + + private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; + + //ToDo Etienne: Nach Anpassung des Bytecode die Tests hinzufügen + //ToDo Etienne: Aufruf von m testen + @BeforeClass + public static void setUp() throws Exception { + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OLFun2.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(generatedByteCodeDirectory); + loader = new URLClassLoader(new URL[] {new URL("file://"+generatedByteCodeDirectory)}); + classToTest = loader.loadClass("OLFun2"); + classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$"); + classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$"); + classFun1StringString = loader.loadClass("Fun1$$Ljava$lang$String$_$Ljava$lang$String$_$"); + } + + @Test + public void mExistsWithInteger() throws Exception{ + Method m = classToTest.getDeclaredMethod("m", classFun1IntInt); + assertNotNull(m); + } + + @Test + public void mExistsWithDouble() throws Exception{ + Method m = classToTest.getDeclaredMethod("m", classFun1DoubleDouble); + assertNotNull(m); + } + + @Test + public void mExistsWithString() throws Exception{ + Method m = classToTest.getDeclaredMethod("m", classFun1StringString); + assertNotNull(m); + } + + //@AfterClass + public static void cleanUp(){ + TestCleanUp.cleanUpDirectory(new File(generatedByteCodeDirectory), f -> f.getName().contains(".class")); + } +} diff --git a/src/test/java/bytecode/OLFunTest.java b/src/test/java/bytecode/OLFunTest.java index 3142ce26..d1910e07 100644 --- a/src/test/java/bytecode/OLFunTest.java +++ b/src/test/java/bytecode/OLFunTest.java @@ -11,8 +11,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import static org.junit.Assert.*; /** - * Test which only checks if the class {@code OLFun} was correctly compiled. - * This is achived by verifying the existence of the three implementations of {@code m}. + * //ToDo Etienne: Beschreiben * * @since Studienarbeit Type Erasure * @author etiennezink @@ -26,10 +25,11 @@ public class OLFunTest { private static Class classFun1IntInt; private static Class classFun1DoubleDouble; private static Class classFun1StringString; - private static Object instanceOfClass; private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; + //ToDo Etienne: Nach Anpassung des Bytecode die Tests hinzufügen + //ToDo Etienne: Aufruf von m testen @BeforeClass public static void setUp() throws Exception { path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OLFun.jav"; @@ -38,19 +38,18 @@ public class OLFunTest { compiler.generateBytecode(generatedByteCodeDirectory); loader = new URLClassLoader(new URL[] {new URL("file://"+generatedByteCodeDirectory)}); classToTest = loader.loadClass("OLFun"); - instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - //classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$"); - //classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$"); + classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$"); + classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$"); classFun1StringString = loader.loadClass("Fun1$$Ljava$lang$String$_$Ljava$lang$String$_$"); } - //@Test + @Test public void mExistsWithInteger() throws Exception{ Method m = classToTest.getDeclaredMethod("m", classFun1IntInt ,Integer.class); assertNotNull(m); } - //@Test + @Test public void mExistsWithDouble() throws Exception{ Method m = classToTest.getDeclaredMethod("m", classFun1DoubleDouble ,Double.class); assertNotNull(m); diff --git a/src/test/resources/bytecode/javFiles/OLFun.jav b/src/test/resources/bytecode/javFiles/OLFun.jav index e2d6f3f7..4896f948 100644 --- a/src/test/resources/bytecode/javFiles/OLFun.jav +++ b/src/test/resources/bytecode/javFiles/OLFun.jav @@ -4,9 +4,6 @@ import java.lang.String; import java.util.Vector; import java.lang.Boolean; - - - public class OLFun { //f = x -> {return x + x;}; diff --git a/src/test/resources/bytecode/javFiles/OLFun2.jav b/src/test/resources/bytecode/javFiles/OLFun2.jav new file mode 100644 index 00000000..fd57e184 --- /dev/null +++ b/src/test/resources/bytecode/javFiles/OLFun2.jav @@ -0,0 +1,13 @@ +import java.lang.String; +//import java.lang.Integer; +//import java.lang.Double; +import java.util.Vector; +import java.lang.Boolean; + +public class OLFun2 { + + x; + m(f){ + x = f.apply(x + x) + } +} \ No newline at end of file