From e17f08263e34c09c1e722a7686ab87c9a9f25f7b Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Wed, 13 Mar 2024 11:42:32 +0100 Subject: [PATCH] Fix Wildcard not working in STG, fixes #287 --- resources/bytecode/javFiles/Wildcards.jav | 8 ++++++++ .../parser/SyntaxTreeGenerator/TypeGenerator.java | 5 ++++- src/test/java/TestComplete.java | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/Wildcards.jav diff --git a/resources/bytecode/javFiles/Wildcards.jav b/resources/bytecode/javFiles/Wildcards.jav new file mode 100644 index 00000000..7dca0519 --- /dev/null +++ b/resources/bytecode/javFiles/Wildcards.jav @@ -0,0 +1,8 @@ +import java.util.List; +import java.lang.Number; + +public class Wildcards { + public void m1(List a) {} + public void m2(List a) {} + public void m3(List a) {} +} diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 3eb720b5..d227c167 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; +import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeArgumentsContext; @@ -121,8 +122,10 @@ public class TypeGenerator { if (wildcardContext.getChildCount() < 3) { if (!Objects.isNull(wildcardContext.extendsWildcardType())) { return new ExtendsWildcardType(convert(wildcardContext.extendsWildcardType().typeType(), reg, generics), wildcardContext.getStart()); - } else { + } else if (!Objects.isNull(wildcardContext.superWildcardType())) { return new SuperWildcardType(convert(wildcardContext.superWildcardType().typeType(), reg, generics), wildcardContext.getStart()); + } else { + return new ExtendsWildcardType(new RefType(new JavaClassName("java.lang.Object"), new NullToken()), wildcardContext.getStart()); } } else { throw new NotImplementedException(); // Wildcard ohne Bound diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index fd8c3188..f5fab9ec 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -881,6 +881,14 @@ public class TestComplete { var instance = clazz.getDeclaredConstructor().newInstance(); } + @Test + public void testWilcards() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Wildcards.jav"); + var clazz = classFiles.get("Wildcards"); + var instance = clazz.getDeclaredConstructor().newInstance(); + } + + @Test public void testBug122() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug122.jav");