Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(array): the qualified name of an array is not java.lang.reflect.Array #995

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
public class CtArrayTypeReferenceImpl<T> extends CtTypeReferenceImpl<T> implements CtArrayTypeReference<T> {
private static final long serialVersionUID = 1L;

public static final String ARRAY_SIMPLE_NAME = Array.class.getSimpleName();
/**
* getCanonicalName() is an _expensive_ operation, result should be cached
*/
public static final String ARRAY_CANONICAL_NAME = Array.class.getCanonicalName();

CtTypeReference<?> componentType;

public CtArrayTypeReferenceImpl() {
Expand All @@ -44,6 +38,10 @@ public void accept(CtVisitor visitor) {

@Override
public CtTypeReference<?> getComponentType() {
if (componentType == null) {
// a sensible default component type to facilitate object creation and testing
componentType = getFactory().Type().get(Object.class).getReference();
}
return componentType;
}

Expand All @@ -67,12 +65,12 @@ public <C extends CtArrayTypeReference<T>> C setComponentType(CtTypeReference<?>

@Override
public String getSimpleName() {
return ARRAY_SIMPLE_NAME;
return getComponentType().getSimpleName() + "[]";
}

@Override
public String getQualifiedName() {
return ARRAY_CANONICAL_NAME;
return getComponentType().getQualifiedName() + "[]";
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package spoon.support.reflect.reference;

import spoon.SpoonException;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
import spoon.reflect.reference.CtReference;
Expand Down Expand Up @@ -46,9 +45,6 @@ public String getSimpleName() {

@Override
public <T extends CtReference> T setSimpleName(String simplename) {
if (simplename.contains("?")) {
throw new SpoonException("A reference can't have a \"?\" in its name.");
}
Factory factory = getFactory();
if (factory instanceof FactoryImpl) {
simplename = ((FactoryImpl) factory).dedup(simplename);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/spoon/test/arrays/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public class ArraysTest {
public void testArrayReferences() throws Exception {
CtType<?> type = build("spoon.test.arrays", "ArrayClass");
assertEquals("ArrayClass", type.getSimpleName());
assertEquals("int[][][]", type.getField("i").getType().toString());
assertEquals("int[][][]", type.getField("i").getType().getSimpleName());
assertEquals(3, ((CtArrayTypeReference<?>) type.getField("i").getType()).getDimensionCount());
final CtArrayTypeReference<?> arrayTypeReference = (CtArrayTypeReference<?>) type.getField("i").getDefaultExpression().getType();
assertEquals(1, arrayTypeReference.getArrayType().getAnnotations().size());
assertEquals("@spoon.test.arrays.ArrayClass.TypeAnnotation(integer = 1)" + System.lineSeparator(), arrayTypeReference.getArrayType().getAnnotations().get(0).toString());

CtField<?> x = type.getField("x");
assertTrue(x.getType() instanceof CtArrayTypeReference);
assertEquals("Array", x.getType().getSimpleName());
assertEquals("java.lang.reflect.Array", x.getType().getQualifiedName());
assertEquals("int[]", x.getType().getSimpleName());
assertEquals("int[]", x.getType().getQualifiedName());
assertEquals("int", ((CtArrayTypeReference<?>) x.getType()).getComponentType().getSimpleName());
assertTrue(((CtArrayTypeReference<?>) x.getType()).getComponentType().getActualClass().equals(int.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testConstructorCallWithGenericArray() throws Exception {
assertTrue(implicitArray.isImplicit());
final CtArrayTypeReference implicitArrayTyped = (CtArrayTypeReference) implicitArray;
assertEquals("", implicitArrayTyped.toString());
assertEquals("Array", implicitArrayTyped.getSimpleName());
assertEquals("AtomicLong[]", implicitArrayTyped.getSimpleName());
assertTrue(implicitArrayTyped.getComponentType().isImplicit());
assertEquals("", implicitArrayTyped.getComponentType().toString());
assertEquals("AtomicLong", implicitArrayTyped.getComponentType().getSimpleName());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/lambda/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void testTypeParameterWithImplicitArrayType() throws Exception {
assertEquals("a", ctParameter.getSimpleName());
assertTrue(ctParameter.getType().isImplicit());
assertEquals("", ctParameter.getType().toString());
assertEquals("Array", ctParameter.getType().getSimpleName());
assertEquals("Object[]", ctParameter.getType().getSimpleName());

final CtArrayTypeReference typeParameter = (CtArrayTypeReference) ctParameter.getType();
assertTrue(typeParameter.getComponentType().isImplicit());
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,6 @@ public void testClearBoundsForTypeParameterReference() throws Exception {
assertNull(reference.getBoundingType());
}

@Test(expected = SpoonException.class)
public void testReferenceName() throws Exception {
final Factory factory = createFactory();
final CtTypeReference<Object> typeReference = factory.Core().createTypeReference();
typeReference.setSimpleName("?");
}

@Test
public void testIgnoreEnclosingClassInActualTypes() throws Exception {
final CtType<Panini> aPanini = buildClass(Panini.class);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/varargs/VarArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void testModelBuildingInitializer() throws Exception {
CtParameter<?> param1 = m.getParameters().get(1);
assertEquals(true, param1.isVarArgs());
assertEquals("java.lang.String[]", param1.getType().toString());
assertEquals("Array", param1.getType().getSimpleName());
assertEquals("String[]", param1.getType().getSimpleName());
assertEquals("java.lang.String[]", param1.getType().getQualifiedName());
assertEquals("java.lang.String", ((CtArrayTypeReference<?>)param1.getType()).getComponentType().toString());
// we can even rewrite the vararg
assertEquals("void foo(int arg0, java.lang.String... args) {"
Expand Down