Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(array): NPE when build the type of a CtNewArray. #561

Merged
merged 1 commit into from
Mar 21, 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 @@ -773,7 +773,7 @@ public DefaultJavaPrettyPrinter writeImplementsClause(CtType<?> t) {

public <T> void visitCtClass(CtClass<T> ctClass) {
SortedList<CtElement> lst = new SortedList<CtElement>(new CtLineElementComparator());
if (ctClass.getSimpleName() != null && !ctClass.isAnonymous()) {
if (ctClass.getSimpleName() != null && !CtType.NAME_UNKNOWN.equals(ctClass.getSimpleName()) && !ctClass.isAnonymous()) {
visitCtType(ctClass);
if (ctClass.isLocalType()) {
write("class " + ctClass.getSimpleName().replaceAll("^[0-9]*", ""));
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,7 @@ public boolean visit(Argument argument, BlockScope scope) {
}
context.isLambdaParameterImplicitlyTyped = true;
} else if (argument.type != null) {
p.setType(references.getTypeReference(argument.type.resolvedType));
p.setType(references.getTypeReference(argument.type));
}

final TypeBinding receiverType = argument.type != null ? argument.type.resolvedType : null;
Expand Down Expand Up @@ -2332,7 +2332,13 @@ public boolean visit(ArrayAllocationExpression arrayAllocationExpression, BlockS
CtNewArray<Object> array = factory.Core().createNewArray();
context.enter(array, arrayAllocationExpression);

final CtArrayTypeReference<Object> arrayType = (CtArrayTypeReference<Object>) references.getTypeReference(arrayAllocationExpression.resolvedType);
CtTypeReference<?> typeReference;
if (arrayAllocationExpression.resolvedType != null) {
typeReference = references.getTypeReference(arrayAllocationExpression.resolvedType.leafComponentType(), arrayAllocationExpression.type);
} else {
typeReference = references.getTypeReference(arrayAllocationExpression.type);
}
final CtArrayTypeReference arrayType = factory.Type().createArrayReference(typeReference, arrayAllocationExpression.dimensions.length);
arrayType.getArrayType().setAnnotations(buildTypeReference(arrayAllocationExpression.type, scope).getAnnotations());
array.setType(arrayType);

Expand Down
33 changes: 33 additions & 0 deletions src/test/java/spoon/test/arrays/ArraysTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package spoon.test.arrays;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -29,4 +36,30 @@ public void testArrayReferences() throws Exception {
assertTrue(((CtArrayTypeReference<?>) x.getType()).getComponentType().getActualClass().equals(int.class));
}

@Test
public void testInitializeWithNewArray() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/Foo.java");
launcher.setSourceOutputDirectory("./target/trash");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();

CtType<Object> aType = launcher.getFactory().Type().get("com.example.Foo");

final List<CtNewArray> elements = aType.getElements(new TypeFilter<>(CtNewArray.class));
assertEquals(2, elements.size());

final CtNewArray attribute = elements.get(0);
assertEquals(1, attribute.getDimensionExpressions().size());
assertEquals(0, ((CtLiteral) attribute.getDimensionExpressions().get(0)).getValue());
assertTrue(attribute.getType() instanceof CtArrayTypeReference);
assertEquals("new java.lang.String[0]", attribute.toString());

final CtNewArray local = elements.get(1);
assertEquals(1, local.getDimensionExpressions().size());
assertTrue(local.getDimensionExpressions().get(0) instanceof CtInvocation);
assertTrue(local.getType() instanceof CtArrayTypeReference);
assertEquals("new Type[list.size()]", local.toString());
}

}
20 changes: 20 additions & 0 deletions src/test/resources/noclasspath/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example;

import de.rcenvironment.core.component.api.BatchedConsoleRowsProcessor;
import com.example.Type;
import com.example.Kuu;
import com.example.Bar;

public class Foo implements Kuu {
private String[] commandLineArgs = new String[0];

public Foo() {
Bar.Inner<Type> variable = new Bar.Inner<Type>() {
@Override
public void method(List<Type> list) {
Type[] array = list.toArray(new Type[list.size()]);
}
};
}

}