Skip to content

Commit

Permalink
Fix Duplicate @nullable Annotations by fixing Equality Check (#263)
Browse files Browse the repository at this point in the history
This PR resolves #262 where Annotator adds duplicate `@Nullable` annotations to fields that already have a `@Nullable` annotation with comments.

The issue occurred because the equality check for annotations was based on the entire annotation node, which includes comments, rather than just the annotation's name. This PR updates the equality check logic to compare annotations based solely on their names, ensuring that existing annotations are correctly detected and duplicates are avoided.
  • Loading branch information
nimakarimipour authored Nov 22, 2024
1 parent 26b4df8 commit 47e0f92
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
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;
import com.github.javaparser.ast.nodeTypes.NodeWithRange;
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;

Expand All @@ -53,13 +53,10 @@ Modification computeTextModificationOn(T node) {
return null;
}
Range range = node.getRange().get();
NodeList<AnnotationExpr> 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);
Expand Down
27 changes: 27 additions & 0 deletions injector/src/test/java/edu/ucr/cs/riple/injector/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 47e0f92

Please sign in to comment.