Skip to content

Commit

Permalink
fix: do not replace types when looking up fields from supertypes (#5248)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Jun 5, 2023
1 parent b645b94 commit 85ce702
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
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 @@ -288,7 +288,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;
}
}

0 comments on commit 85ce702

Please sign in to comment.