Skip to content

Commit

Permalink
fix(getTypeDeclaration): getTypeDeclaration('?') should point to Object
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Nov 14, 2016
1 parent a98b96d commit 85e29ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ private CtTypeParameter getRecursiveDeclaration(CtElement element) {

@Override
public CtType<Object> getTypeDeclaration() {
if ("?".equals(getSimpleName())) {
return getFactory().Type().get(Object.class);
}
return getDeclaration();
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
Expand All @@ -28,6 +29,7 @@
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.ReferenceTypeFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.SnippetCompilationHelper;
import spoon.test.reference.testclasses.EnumValue;
import spoon.test.reference.testclasses.Panini;
import spoon.testing.utils.ModelUtils;
Expand Down Expand Up @@ -580,4 +582,19 @@ public void testGetTypeDeclaration() throws Exception {
assertNotNull(iBar);
assertEquals("compilation.IBar", iBar.getQualifiedName());
}

@Test
public void testTypeDeclarationWildcard() throws Exception {
// contract1: getTypeDeclaration nevers returns null, even for wilddards
// contract2: getTypeDeclaration returns a CtTYpe representing Object as the compiler does
CtLocalVariable<?> s = new Launcher().getFactory().Code().createCodeSnippetStatement("java.util.List<?> l = null").compile();
assertEquals("?", s.getType().getActualTypeArguments().get(0).getSimpleName());
assertEquals("Object", s.getType().getActualTypeArguments().get(0).getTypeDeclaration().getSimpleName());
assertEquals(Object.class, s.getType().getActualTypeArguments().get(0).getTypeDeclaration().getActualClass());

// some additional tests
CtLocalVariable<?> s2 = new Launcher().getFactory().Code().createCodeSnippetStatement("java.util.List<String> l = null").compile();
assertEquals("String", s2.getType().getActualTypeArguments().get(0).getSimpleName());
assertEquals(String.class, s2.getType().getActualTypeArguments().get(0).getTypeDeclaration().getActualClass());
}
}

0 comments on commit 85e29ad

Please sign in to comment.