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

fix: ensure sniper pretty-printer prints modifiers and type separated by a space #4296

Merged
merged 11 commits into from
Nov 24, 2021
6 changes: 5 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,11 @@ public boolean visit(QualifiedTypeReference qualifiedTypeReference, BlockScope s
context.enter(helper.createCatchVariable(qualifiedTypeReference, scope), qualifiedTypeReference);
return true;
}
context.enter(factory.Code().createTypeAccessWithoutCloningReference(references.buildTypeReference(qualifiedTypeReference, scope)), qualifiedTypeReference);
CtTypeReference<?> typeReference = references.buildTypeReference(qualifiedTypeReference, scope);
if (typeReference != null) {
typeReference.setPosition(position.buildPositionCtElement(typeReference, qualifiedTypeReference));
}
context.enter(factory.Code().createTypeAccessWithoutCloningReference(typeReference), qualifiedTypeReference);
return true;
}

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,6 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
//build position with appropriate context
return buildPositionCtElement(e, (Argument) pair.node);
} else if (node instanceof TypeReference) {
TypeReference typeReference = (TypeReference) node;
if (typeReference.resolvedType.getTypeAnnotations() != null) {
for (int a = 0; a < typeReference.resolvedType.getTypeAnnotations().length; a++) {
sourceStart = findPrevAnnotations(contents, 0, sourceStart);
}
}
sourceEnd = getSourceEndOfTypeReference(contents, (TypeReference) node, sourceEnd);
} else if (node instanceof AllocationExpression) {
AllocationExpression allocationExpression = (AllocationExpression) node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

public class AnnotationPositionTest {

@Ignore("Unresolved Bug")
@GitHubIssue(issueNumber = 3358)
@Test
public void testUsageOfTypeAnnotationOnParameterInMethod() {
final Launcher launcher = new Launcher();
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/spoon/test/serializable/SourcePositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
Expand Down Expand Up @@ -71,4 +72,18 @@ public void testSourcePosition() throws IOException {
CtField<?> elem2 = typeFromFile.getField("a");
assertTrue(elem1.getPosition().getFile().equals(elem2.getPosition().getFile()));
}

@Test
public void test_sourcePositionOfTypeInFieldExists() {
// contract: the type reference of type of the field should have a source position
final Launcher launcher = new Launcher();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a contract? thanks!

launcher.addInputResource("src/test/resources/spoon/test/sourcePosition/FieldType.java");
launcher.addInputResource("src/test/resources/spoon/test/sourcePosition/SourcePartitionValidator.java");
CtModel model = launcher.buildModel();

CtField<?> field = (CtField<?>) model.getElements(
element -> element instanceof CtField &&
((CtField<?>) element).getSimpleName().equals("pleaseAttachSourcePositionToMyType")).stream().findFirst().get();
assertTrue("Source position unknown for type of field", field.getType().getPosition().isValidPosition());
}
}
5 changes: 5 additions & 0 deletions src/test/resources/spoon/test/sourcePosition/FieldType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sourcePosition;

public class FieldType {
private SourcePartitionValidator.MatchingStrategy pleaseAttachSourcePositionToMyType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sourcePosition;

class SourcePartitionValidator {
public enum MatchingStrategy { }
}