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: removed annotation should not appear in sniper mode #4284

Merged
merged 7 commits into from
Nov 16, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public int hashCode() {
@Override
public <E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotations) {
if (annotations == null || annotations.isEmpty()) {
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, CtRole.ANNOTATION, this.annotations, new ArrayList<>(this.annotations));
this.annotations = emptyList();
return (E) this;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtThrow;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtElement;
Expand Down Expand Up @@ -73,6 +74,7 @@
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -811,6 +813,19 @@ void testRoundBracketPrintingInComplexArithmeticExpression() {
testSniper("ArithmeticExpression", noOpModifyFieldAssignment, assertPrintsRoundBracketsCorrectly);
}

@Test
@GitHubIssue(issueNumber = 4218)
void testSniperDoesNotPrintTheDeletedAnnotation() {
Consumer<CtType<?>> deleteAnnotation = type -> {
type.getAnnotations().forEach(CtAnnotation::delete);
};

BiConsumer<CtType<?>, String> assertDoesNotContainAnnotation = (type, result) ->
assertThat(result, not(containsString("@abc.def.xyz")));

testSniper("sniperPrinter.DeleteAnnotation", deleteAnnotation, assertDoesNotContainAnnotation);
}

@Test
@GitHubIssue(issueNumber = 4220)
void testSniperAddsSpaceAfterFinal() {
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/sniperPrinter/DeleteAnnotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sniperPrinter;

@abc.def.xyz
class DeleteAnnotation { }