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: do not replace types when looking up fields from supertypes #5248

Merged
merged 1 commit into from
Jun 5, 2023
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
6 changes: 5 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ private <T, U extends CtVariable<T>> U getVariableDeclaration(
if (name.equals(new String(fieldBinding.readableName()))) {
final String qualifiedNameOfParent = getNormalQualifiedName(referenceBinding);

final CtType parentOfField = referenceBinding.isClass()
CtType<?> parentOfField = typeFactory.get(qualifiedNameOfParent);
if (parentOfField != null) {
return (U) parentOfField.getField(name);
}
parentOfField = referenceBinding.isClass()
? classFactory.create(qualifiedNameOfParent)
: interfaceFactory.create(qualifiedNameOfParent);

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/type/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
Expand All @@ -56,6 +57,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -417,4 +419,15 @@ public void testBinaryOpStringsType() {
List<CtBinaryOperator> concats = model.getElements(new TypeFilter<>(CtBinaryOperator.class));
concats.forEach(c -> assertEquals("java.lang.String", c.getType().toString()));
}

@ModelTest(
value = {"./src/test/resources/noclasspath/issue5208/"},
noClasspath = true
)
void testClassNotReplacedInNoClasspathMode(Factory factory) {
// contract: ClassT1 is not replaced once present when looking up the ClassT1#classT3 field from ClassT2
CtType<?> type = factory.Type().get("p20.ClassT1");
assertNotNull(type);
assertNotEquals(SourcePosition.NOPOSITION, type.getPosition());
}
}
11 changes: 11 additions & 0 deletions src/test/resources/noclasspath/issue5208/p20/ClassT1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package p20;
public abstract class ClassT1 {
ClassT3 classT3;

public ClassT1(){
this.classT3 = new ClassT3(this);
}

void fun2(ClassT3 classT3){
}
}
10 changes: 10 additions & 0 deletions src/test/resources/noclasspath/issue5208/p20/ClassT2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package p20;
import p8.GlobalExecutor;

public class ClassT2 extends ClassT1 {
public void fun(){
GlobalExecutor.executeByCommon(() -> {
fun2(classT3);
});
}
}
9 changes: 9 additions & 0 deletions src/test/resources/noclasspath/issue5208/p20/ClassT3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package p20;

public class ClassT3 {
final ClassT1 classT1;

public ClassT3(ClassT1 classT1) {
this.classT1 = classT1;
}
}