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

alternative to #1762: fix: a NPE in CtTypeImpl#isSameParameter in noclasspath #1767

Merged
merged 6 commits into from
Dec 1, 2017
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
11 changes: 7 additions & 4 deletions src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,28 +760,31 @@ private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParamet
}
}
if (expectedType instanceof CtTypeParameterReference && ctParameterType instanceof CtTypeParameterReference) {
// Check if Object or extended.
// both types are generic
if (!ctParameterType.equals(expectedType)) {
return false;
}
} else if (expectedType instanceof CtTypeParameterReference) {
if (!ctParameterType.isSubtypeOf(factory.Type().createReference(expectedType.getActualClass()))) {
// expectedType type is generic, ctParameterType is real type
if (!expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName())) {
return false;
}
} else if (ctParameterType instanceof CtTypeParameterReference) {
// ctParameterType is generic, expectedType type is real type
CtTypeParameter declaration = (CtTypeParameter) ctParameterType.getDeclaration();
if (declaration.getSuperclass() instanceof CtIntersectionTypeReference) {
if (declaration != null && declaration.getSuperclass() instanceof CtIntersectionTypeReference) {
for (CtTypeReference<?> ctTypeReference : declaration.getSuperclass().asCtIntersectionTypeReference().getBounds()) {
if (ctTypeReference.equals(expectedType)) {
return true;
}
}
} else if (declaration.getSuperclass() != null) {
} else if (declaration != null && declaration.getSuperclass() != null) {
return declaration.getSuperclass().equals(expectedType);
} else {
return getFactory().Type().objectType().equals(expectedType);
}
} else if (!expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName())) {
// both are real types
return false;
}
return true;
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/spoon/test/invocations/InvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.SpoonAPI;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.invocations.testclasses.Bar;
import spoon.test.invocations.testclasses.Foo;

import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.build;
Expand Down Expand Up @@ -58,4 +63,21 @@ public void testTargetNullForStaticMethod() throws Exception {
fail();
}
}

@Test
public void testIssue1753() {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/noclasspath/elasticsearch1753");

final CtModel model = launcher.buildModel();
final List<CtExecutable> executables =
model.getElements(new TypeFilter<>(CtExecutable.class))
.stream()
.filter(i -> i.getPosition().getLine() == 190)
.collect(Collectors.toList());
assertEquals(1, executables.size());
final CtExecutable exe = executables.get(0);
assertNotNull(exe.getReference().getDeclaration());
}
}
Loading