diff --git a/resources/bytecode/javFiles/Generics3.jav b/resources/bytecode/javFiles/Generics3.jav new file mode 100644 index 000000000..fb354d430 --- /dev/null +++ b/resources/bytecode/javFiles/Generics3.jav @@ -0,0 +1,7 @@ +import java.lang.String; +import java.lang.Integer; +import java.util.List; + +class Generics3> { + +} \ No newline at end of file diff --git a/src/test/java/targetast/ASTToTypedTargetAST.java b/src/test/java/targetast/ASTToTypedTargetAST.java index fd75bd114..0d26fde5f 100644 --- a/src/test/java/targetast/ASTToTypedTargetAST.java +++ b/src/test/java/targetast/ASTToTypedTargetAST.java @@ -1,5 +1,6 @@ package targetast; +import com.google.common.reflect.TypeToken; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.environment.ByteArrayClassLoader; import de.dhbwstuttgart.parser.NullToken; @@ -9,12 +10,14 @@ import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.target.generate.ASTToTargetAST; import de.dhbwstuttgart.target.tree.TargetClass; import de.dhbwstuttgart.typeinference.result.ResultSet; +import org.junit.Ignore; import org.junit.Test; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Vector; import static org.junit.Assert.*; @@ -86,6 +89,21 @@ public class ASTToTypedTargetAST { var infimum = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); } + @Test + public void gen() throws Exception { + var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Gen.jav").toFile(); + var compiler = new JavaTXCompiler(file); + var resultSet = compiler.typeInference(); + var converter = new ASTToTargetAST(resultSet); + var classes = compiler.sourceFiles.get(file).getClasses(); + + var generics = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); + var m = generics.getDeclaredMethod("m", Vector.class); + var mReturnType = m.getGenericReturnType(); + assertEquals(mReturnType, m.getParameters()[0].getParameterizedType()); + assertEquals(mReturnType, new TypeToken>(){}.getType()); + } + @Test public void definedGenerics() throws Exception { var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics.jav").toFile(); @@ -95,6 +113,13 @@ public class ASTToTypedTargetAST { var classes = compiler.sourceFiles.get(file).getClasses(); var generics = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); + var B = generics.getTypeParameters()[0]; + var mt1 = generics.getDeclaredMethod("mt1", Object.class); + var constructor = generics.getDeclaredConstructor(Object.class); + + assertEquals(B, mt1.getGenericReturnType()); + assertEquals(B, mt1.getParameters()[0].getParameterizedType()); + assertEquals(B, constructor.getParameters()[0].getParameterizedType()); } @Test @@ -106,5 +131,21 @@ public class ASTToTypedTargetAST { var classes = compiler.sourceFiles.get(file).getClasses(); var generics2 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); + assertEquals(generics2.getTypeParameters()[0].getBounds()[0], String.class); + var m = generics2.getDeclaredMethod("m1", Object.class); + assertEquals(m.getTypeParameters()[0].getBounds()[0], Integer.class); + var param = m.getTypeParameters()[0]; + } + + @Test + @Ignore("Not implemented") + public void definedGenerics3() throws Exception { + var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics3.jav").toFile(); + var compiler = new JavaTXCompiler(file); + var resultSet = compiler.typeInference(); + var converter = new ASTToTargetAST(resultSet); + var classes = compiler.sourceFiles.get(file).getClasses(); + + var generics3 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader()); } } diff --git a/src/test/java/targetast/TestCodegen.java b/src/test/java/targetast/TestCodegen.java index 104ff7d07..c1c2a5ee7 100644 --- a/src/test/java/targetast/TestCodegen.java +++ b/src/test/java/targetast/TestCodegen.java @@ -330,7 +330,7 @@ public class TestCodegen { } @Test - @Ignore("The lambda class is not generated because we don'T call ASTToTargetAST") + @Ignore("The lambda class is not generated because we don't call ASTToTargetAST") public void testLambda() throws Exception { var classLoader = new ByteArrayClassLoader(); //var fun = classLoader.loadClass(Path.of(System.getProperty("user.dir"), "src/test/java/targetast/Fun1$$.class"));