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

review: fix: Correctly adapt type parameters inherited from enclosing classes #5228

Merged
merged 7 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 4 additions & 6 deletions src/main/java/spoon/support/adaption/DeclarationNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ public Optional<CtTypeReference<?>> resolveTypeParameter(CtTypeParameterReferenc

// We try to find a glue node below us to delegate to. Glue nodes do the mapping so we can just
// pass it on unchanged.
Optional<GlueNode> glueNode = children.stream()
.filter(it -> it.isInducedBy(this.inducedBy))
.findFirst();

if (glueNode.isPresent()) {
return glueNode.get().resolveTypeParameter(reference);
if (!children.isEmpty()) {
// We pick a random child. Well-typed programs will converge to the same solution, no matter
// which path we pick.
return children.iterator().next().resolveTypeParameter(reference);
}

// If we have no glue node, we need to actually resolve the type parameter as we reached the
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/spoon/support/adaption/TypeAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,17 @@ private Optional<CtExecutable<?>> getDeclaringMethodOrConstructor(CtTypeReferenc
return Optional.of((CtExecutable<?>) parent);
}

@SuppressWarnings("AssignmentToMethodParameter")
private DeclarationNode buildHierarchyFrom(CtTypeReference<?> startReference, CtType<?> startType,
CtTypeReference<?> end) {
CtType<?> endType = findDeclaringType(end);
Map<CtTypeReference<?>, DeclarationNode> declarationNodes = new HashMap<>();

if (needToMoveStartTypeToEnclosingClass(end, endType)) {
startType = moveStartTypeToEnclosingClass(hierarchyStart, endType.getReference());
startReference = startType.getReference();
}

DeclarationNode root = buildDeclarationHierarchyFrom(
startType.getReference(),
endType,
Expand All @@ -583,6 +589,38 @@ private DeclarationNode buildHierarchyFrom(CtTypeReference<?> startReference, Ct
.orElse(null);
}

private boolean needToMoveStartTypeToEnclosingClass(CtTypeReference<?> end, CtType<?> endType) {
if (!(end instanceof CtTypeParameterReference)) {
return false;
}
// Declaring type is not the same as the inner type (i.e. the type parameter was declared on an
// enclosing type)
CtType<?> parentType = end.getParent(CtType.class);
if (parentType instanceof CtTypeParameter) {
CtFormalTypeDeclarer declarer = ((CtTypeParameter) parentType).getTypeParameterDeclarer();
if (declarer instanceof CtType) {
parentType = (CtType<?>) declarer;
} else {
parentType = declarer.getDeclaringType();
}
}

return !parentType.getQualifiedName().equals(endType.getQualifiedName());
}

private CtType<?> moveStartTypeToEnclosingClass(CtType<?> start, CtTypeReference<?> endRef) {
CtType<?> current = start;
while (current != null) {
if (isSubtype(current, endRef)) {
return current;
}
current = current.getDeclaringType();
}
throw new SpoonException(
"Did not find a suitable enclosing type to start parameter type adaption from"
);
}

/**
* This method attempts to find a suitable end type for building our hierarchy.
* <br>
Expand All @@ -598,12 +636,18 @@ private DeclarationNode buildHierarchyFrom(CtTypeReference<?> startReference, Ct
*/
private CtType<?> findDeclaringType(CtTypeReference<?> reference) {
CtType<?> type = null;
if (reference.isParentInitialized()) {
// Prefer declaration to parent. This will be different if the type parameter is declared on an
// enclosing class.
if (reference instanceof CtTypeParameterReference) {
type = reference.getTypeDeclaration();
}
if (type == null && reference.isParentInitialized()) {
type = reference.getParent(CtType.class);
}
if (type == null) {
type = reference.getTypeDeclaration();
}

if (type instanceof CtTypeParameter) {
CtFormalTypeDeclarer declarer = ((CtTypeParameter) type).getTypeParameterDeclarer();
if (declarer instanceof CtType) {
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/spoon/support/TypeAdaptorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package spoon.support;

import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -12,7 +13,9 @@
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.adaption.TypeAdaptor;
import spoon.test.GitHubIssue;
import spoon.testing.utils.ModelTest;

import java.util.List;
Expand Down Expand Up @@ -440,4 +443,53 @@ public <T extends CharSequence> void overloaded(T t) {}

public <T extends String> void overriden(T t) {}
}

@GitHubIssue(issueNumber = 5226, fixed = true)
void testAdaptingTypeFromEnclosingClass() {
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(11);
launcher.addInputResource("src/test/java/spoon/support/TypeAdaptorTest.java");
CtType<?> type = launcher.getFactory()
.Type()
.get(UseGenericFromEnclosingType.class);
@SuppressWarnings("rawtypes")
List<CtMethod> methods = type.getElements(new TypeFilter<>(CtMethod.class))
.stream()
.filter(it -> it.getSimpleName().equals("someMethod"))
.collect(Collectors.toList());
CtMethod<?> test1Method = methods.stream()
.filter(it -> !it.getDeclaringType().getSimpleName().startsWith("Extends"))
.findAny()
.orElseThrow();
CtMethod<?> test2Method = methods.stream()
.filter(it -> it.getDeclaringType().getSimpleName().startsWith("Extends"))
.findAny()
.orElseThrow();

assertTrue(test2Method.isOverriding(test1Method));
assertFalse(test1Method.isOverriding(test2Method));
}

public static class UseGenericFromEnclosingType {

public static class Enclosing<T> {

public class Enclosed<S> {

void someMethod(S s, T t) {
}
}
}

public static class ExtendsEnclosing extends Enclosing<String> {

public class ExtendsEnclosed extends Enclosed<Integer> {

@Override
void someMethod(Integer s, String t) {
throw new UnsupportedOperationException();
}
}
}
}
}