Skip to content

Commit

Permalink
fix: ensure sniper pretty-printer prints modifiers and type separated…
Browse files Browse the repository at this point in the history
… by a space (#4296)
  • Loading branch information
algomaster99 committed Nov 24, 2021
1 parent a4e39c6 commit 454dada
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
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();
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 { }
}

0 comments on commit 454dada

Please sign in to comment.