From c65102d89a3cef669a5f0c31df91f9eec3c70bf3 Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Tue, 29 Mar 2022 17:00:06 +0200 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20von=20OLFun2=20und=20=C3=84nd?= =?UTF-8?q?erung=20der=20Spezialisierungen=20von=20FunN,=20dass=20diese=20?= =?UTF-8?q?statt=20des=20TPH=20im=20Namen=20Object=20haben.=20Ansonsten=20?= =?UTF-8?q?werden=20diese=20nicht=20wieder=20gefunden.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/funN/FunNGenerator.java | 8 +-- src/test/java/bytecode/OLFun2Test.java | 69 +++++++++++++++++++ src/test/java/bytecode/OLFunTest.java | 15 ++-- .../resources/bytecode/javFiles/OLFun.jav | 3 - .../resources/bytecode/javFiles/OLFun2.jav | 13 ++++ 5 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 src/test/java/bytecode/OLFun2Test.java create mode 100644 src/test/resources/bytecode/javFiles/OLFun2.jav diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java index 8bd512b80..5b6a9e442 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 000000000..6ec8701bd --- /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 3142ce26c..d1910e07e 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 e2d6f3f71..4896f948f 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 000000000..fd57e1841 --- /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