Skip to content

Commit

Permalink
Use type of field declarer as owner
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen committed Oct 2, 2023
1 parent 18f8d58 commit f8df670
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
17 changes: 4 additions & 13 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ <T> CtFieldAccess<T> createFieldAccess(SingleNameReference singleNameReference)
if (ref.isStatic() && !ref.getDeclaringType().isAnonymous()) {
va.setTarget(jdtTreeBuilder.getFactory().Code().createTypeAccess(ref.getDeclaringType(), true));
} else if (!ref.isStatic()) {
CtTypeReference<?> owningType = jdtTreeBuilder.getReferencesBuilder().getTypeReference(singleNameReference.actualReceiverType);
if (enclosingClassHasFieldWithName(singleNameReference)) {
TypeBinding fieldDeclarerType = singleNameReference.fieldBinding().declaringClass;
TypeBinding actualReceiverType = singleNameReference.actualReceiverType;
CtTypeReference<?> owningType = jdtTreeBuilder.getReferencesBuilder().getTypeReference(fieldDeclarerType);
if (Arrays.equals(fieldDeclarerType.qualifiedSourceName(), actualReceiverType.qualifiedSourceName())) {
va.setTarget(jdtTreeBuilder.getFactory().Code().createThisAccess(owningType, true));
} else {
va.setTarget(
Expand All @@ -382,17 +384,6 @@ <T> CtFieldAccess<T> createFieldAccess(SingleNameReference singleNameReference)
return va;
}

private boolean enclosingClassHasFieldWithName(SingleNameReference singleNameReference) {
for (ASTPair pair : jdtTreeBuilder.getContextBuilder().getAllContexts()) {
if (pair.node instanceof TypeDeclaration) {
FieldDeclaration[] fields = ((TypeDeclaration) pair.node).fields;
return fields != null
&& Arrays.stream(fields).anyMatch(it -> Arrays.equals(it.name, singleNameReference.token));
}
}
return false;
}

/**
* In no classpath mode, when we build a field access, we have a binding typed by ProblemBinding.
* This binding doesn't contain all information but we can get some of them.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/spoon/reflect/visitor/CtScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ public void exit(CtElement o) {
// this is a coarse-grain check to see if the scanner changes
// no more exec ref in paramref
// also takes into account the comments
assertEquals(3629, counter.nElement + countOfCommentsInCompilationUnits);
assertEquals(2411, counter.nEnter + countOfCommentsInCompilationUnits);
assertEquals(2411, counter.nExit + countOfCommentsInCompilationUnits);
assertEquals(3667, counter.nElement + countOfCommentsInCompilationUnits);
assertEquals(2449, counter.nEnter + countOfCommentsInCompilationUnits);
assertEquals(2449, counter.nExit + countOfCommentsInCompilationUnits);

// contract: all AST nodes which are part of Collection or Map are visited first by method "scan(Collection|Map)" and then by method "scan(CtElement)"
Counter counter2 = new Counter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ void testUnqualifiedSuperFieldAccess() {
CtFieldRead<?> read = ctModel.getElements(new TypeFilter<>(CtFieldRead.class)).get(0);
assertTrue(read.getTarget() instanceof CtSuperAccess<?>, "Target was no super access");
assertTrue(read.getTarget().isImplicit(), "super target was not implicit");
assertEquals("A", read.getTarget().getType().getQualifiedName());
assertEquals(read.toString(), "a", "super target was still printed");
}

Expand Down

0 comments on commit f8df670

Please sign in to comment.