From 84e73d6e81f95709ba2ef2fe3fa7d3a9047a59fc Mon Sep 17 00:00:00 2001 From: nimakarimipour Date: Thu, 21 Nov 2024 23:22:44 -0800 Subject: [PATCH 1/2] bug fix --- .../injector/changes/AddMarkerAnnotation.java | 7 ++--- .../edu/ucr/cs/riple/injector/BasicTest.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) 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..400095de4 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(); + } } From ab4fc473da99d5acaab0a07a98b06b8ed2e7889b Mon Sep 17 00:00:00 2001 From: nimakarimipour Date: Thu, 21 Nov 2024 23:32:52 -0800 Subject: [PATCH 2/2] fix test content --- .../test/java/edu/ucr/cs/riple/injector/BasicTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 400095de4..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 @@ -100,15 +100,15 @@ public void ignoreCommentsOnAnnotationEqualCheckTest() { "package edu.ucr;", "import javax.annotation.Nullable;", "public class Test {", - " @Nullable @GuardedBy(\"this\") // Either a RuntimeException, non-fatal Error, or IOException.", - " private Throwable creationFailure;", + " @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;", + " @Nullable @GuardedBy(\"this\") // Either a RuntimeException, non-fatal Error, or IOException.", + " private Throwable creationFailure;", "}") .addChanges( new AddMarkerAnnotation(