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 Duplicate @Nullable Annotations by fixing Equality Check #263

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading