diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/changes/AddMarkerAnnotation.java b/injector/src/main/java/edu/ucr/cs/riple/injector/changes/AddMarkerAnnotation.java index b5e3cb72c..8005c907d 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/changes/AddMarkerAnnotation.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/changes/AddMarkerAnnotation.java @@ -23,7 +23,6 @@ package edu.ucr.cs.riple.injector.changes; import com.github.javaparser.Range; -import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.expr.MarkerAnnotationExpr; import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; @@ -31,6 +30,7 @@ import edu.ucr.cs.riple.injector.location.Location; import edu.ucr.cs.riple.injector.modifications.Insertion; import edu.ucr.cs.riple.injector.modifications.Modification; +import edu.ucr.cs.riple.injector.util.TypeUtils; import java.util.Objects; import javax.annotation.Nullable; @@ -53,13 +53,10 @@ Modification computeTextModificationOn(T node) { return null; } Range range = node.getRange().get(); - NodeList annotations = node.getAnnotations(); AnnotationExpr annotationExpr = new MarkerAnnotationExpr(annotationName.simpleName); // Check if annot already exists. - boolean annotAlreadyExists = - annotations.stream().anyMatch(annot -> annot.equals(annotationExpr)); - if (annotAlreadyExists) { + if (TypeUtils.isAnnotatedWith(node, annotationExpr)) { return null; } return new Insertion(annotationExpr.toString(), range.begin); diff --git a/injector/src/test/java/edu/ucr/cs/riple/injector/BasicTest.java b/injector/src/test/java/edu/ucr/cs/riple/injector/BasicTest.java index 8d004b3e5..1bc6dce54 100644 --- a/injector/src/test/java/edu/ucr/cs/riple/injector/BasicTest.java +++ b/injector/src/test/java/edu/ucr/cs/riple/injector/BasicTest.java @@ -26,7 +26,9 @@ import edu.ucr.cs.riple.injector.changes.ASTChange; import edu.ucr.cs.riple.injector.changes.AddMarkerAnnotation; +import edu.ucr.cs.riple.injector.location.OnField; import edu.ucr.cs.riple.injector.location.OnMethod; +import java.util.Collections; import org.junit.Test; public class BasicTest extends BaseInjectorTest { @@ -89,4 +91,29 @@ public void skipExistingAnnotations() { "javax.annotation.Nullable")) .start(); } + + @Test + public void ignoreCommentsOnAnnotationEqualCheckTest() { + injectorTestHelper + .addInput( + "Foo.java", + "package edu.ucr;", + "import javax.annotation.Nullable;", + "public class Test {", + " @Nullable @GuardedBy(\"this\") // Either a RuntimeException, non-fatal Error, or IOException.", + " private Throwable creationFailure;", + "}") + .expectOutput( + "package edu.ucr;", + "import javax.annotation.Nullable;", + "public class Test {", + " @Nullable @GuardedBy(\"this\") // Either a RuntimeException, non-fatal Error, or IOException.", + " private Throwable creationFailure;", + "}") + .addChanges( + new AddMarkerAnnotation( + new OnField("Foo.java", "edu.ucr.Test", Collections.singleton("creationFailure")), + "javax.annotation.Nullable")) + .start(); + } }