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

Insert annotations at the correct position for arrays #251

Merged
merged 10 commits into from
Oct 29, 2024
13 changes: 12 additions & 1 deletion injector/src/main/java/edu/ucr/cs/riple/injector/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

package edu.ucr.cs.riple.injector;

import com.github.javaparser.JavaToken;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.Range;
import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
Expand Down Expand Up @@ -608,7 +610,16 @@ public static Range findSimpleNameRangeInTypeName(Type type) {
return ((ClassOrInterfaceType) type).getName().getRange().get();
}
if (type instanceof ArrayType) {
return findSimpleNameRangeInTypeName(((ArrayType) type).getComponentType());
Optional<TokenRange> tokenRange = type.getTokenRange();
if (tokenRange.isEmpty()) {
return null;
}
for (JavaToken javaToken : tokenRange.get()) {
if (javaToken.asString().equals("[")) {
return javaToken.getRange().orElse(null);
}
}
return null;
}
if (type instanceof PrimitiveType) {
if (type.getRange().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public RemoveAnnotation getReverse() {
@Nullable
public AddTypeUseMarkerAnnotation toDeclaration() {
// check if the annotation is on array declaration
// TODO check this for array.
if (isOnLocalVariableArray() && typeIndex.contains(ImmutableList.of(0))) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public Set<Modification> visit(ClassOrInterfaceType type, TypeUseAnnotationChang

@Override
public Set<Modification> visit(ArrayType type, TypeUseAnnotationChange change) {
return type.getComponentType().accept(this, change);
if (index.size() == 1 && index.getFirst() == 0) {
Modification onType = change.computeTextModificationOnType(type, annotationExpr);
if (onType != null) {
return Set.of(onType);
}
}
// if current index is 1, the process component type
if (!index.isEmpty()) {
boolean onComponentType = index.pollFirst() == 1;
if (onComponentType) {
return type.getComponentType().accept(this, change);
}
}
return Collections.emptySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void additionOnFullyQualifiedTypeNamesTest() {
"import edu.ucr.custom.Nullable;",
"public class Foo {",
" java.lang.@Nullable Object bar;",
" java.util.@Nullable Map<java.lang.@Nullable String, @Nullable String[]> f0;",
" java.util.@Nullable Map<java.lang.@Nullable String, String@Nullable []> f0;",
" java.lang.@Nullable Object baz(java.lang.@Nullable Object param) {;",
" java.lang.@Nullable Object localVar;",
" return new Object();",
Expand Down Expand Up @@ -175,7 +175,7 @@ public void deletionOnFullyQualifiedTypeNamesTest() {
"import edu.ucr.custom.Nullable;",
"public class Foo {",
" java.lang.@Nullable Object bar;",
" java.util.@Nullable Map<java.lang.@Nullable String, @Nullable String[]> f0;",
" java.util.@Nullable Map<java.lang.@Nullable String, String @Nullable []> f0;",
" java.lang.@Nullable Object baz(java.lang.@Nullable Object param) {;",
" java.lang.@Nullable Object localVar;",
" return new Object();",
Expand All @@ -186,7 +186,7 @@ public void deletionOnFullyQualifiedTypeNamesTest() {
"import edu.ucr.custom.Nullable;",
"public class Foo {",
" java.lang.Object bar;",
" java.util.Map<java.lang.String, String[]> f0;",
" java.util.Map<java.lang.String, String []> f0;",
" java.lang.Object baz(java.lang.Object param) {;",
" java.lang.Object localVar;",
" return new Object();",
Expand Down Expand Up @@ -222,17 +222,17 @@ public void additionOnArrayTest() {
" public void foo() {",
" java.util.Map<java.lang.String, String[]> f0;",
" Map<T, T>[] f1;",
" String[] f2;",
" int[] f2;",
" }",
"}")
.expectOutput(
"package test;",
"import edu.ucr.custom.Nullable;",
"public class Foo<T> {",
" public void foo() {",
" java.util.@Nullable Map<java.lang.@Nullable String, @Nullable String[]> f0;",
" @Nullable Map<@Nullable T, @Nullable T>[] f1;",
" @Nullable String[] f2;",
" java.util.@Nullable Map<java.lang.@Nullable String, String@Nullable []> f0;",
" Map<@Nullable T, @Nullable T>@Nullable [] f1;",
" int@Nullable [] f2;",
" }",
"}")
.addChanges(
Expand All @@ -245,7 +245,7 @@ public void additionOnArrayTest() {
new OnLocalVariable("Foo.java", "test.Foo", "foo()", "f1"),
"edu.ucr.custom.Nullable",
ImmutableList.of(
ImmutableList.of(0), ImmutableList.of(1, 0), ImmutableList.of(2, 0))),
ImmutableList.of(0), ImmutableList.of(1, 1, 0), ImmutableList.of(1, 2, 0))),
new AddTypeUseMarkerAnnotation(
new OnLocalVariable("Foo.java", "test.Foo", "foo()", "f2"),
"edu.ucr.custom.Nullable"))
Expand All @@ -261,14 +261,14 @@ public void deletionOnArrayTest() {
"import edu.ucr.custom.Nullable;",
"public class Foo {",
" java.util.Map<String, String[]> f0;",
" java.util.@Nullable Map<@Nullable String, @Nullable String[]> f1;",
" @Nullable Map<java.util.@Nullable Map, @Nullable String>[] f2;",
" java.util.@Nullable Map<@Nullable String, String@Nullable []> f1;",
" Map<java.util.@Nullable Map, @Nullable String>@Nullable [] f2;",
"}")
.expectOutput(
"package test;",
"import edu.ucr.custom.Nullable;",
"public class Foo {",
" java.util.@Nullable Map<@Nullable String, @Nullable String[]> f0;",
" java.util.@Nullable Map<@Nullable String, String@Nullable []> f0;",
" java.util.Map<String, String[]> f1;",
" Map<java.util.Map, String>[] f2;",
"}")
Expand All @@ -287,7 +287,7 @@ public void deletionOnArrayTest() {
new OnField("Foo.java", "test.Foo", Set.of("f2")),
"edu.ucr.custom.Nullable",
ImmutableList.of(
ImmutableList.of(0), ImmutableList.of(1, 0), ImmutableList.of(2, 0))))
ImmutableList.of(0), ImmutableList.of(1, 1, 0), ImmutableList.of(1, 2, 0))))
.start();
}

Expand Down Expand Up @@ -409,7 +409,7 @@ public void deletionOnInitializerTest() {
" @Nullable int f0 = 0;",
" @Nullable Bar<@Nullable String, @Nullable Integer, @Nullable Baz<@Nullable String, @Nullable Integer>> f1 = new Bar<@Nullable String, @Nullable Integer, @Nullable Baz<@Nullable String, @Nullable Integer>>();",
" @Nullable String f2 = \"FOO\";",
" @Nullable Bar<@Nullable String, @Nullable Integer[], @Nullable Baz<@Nullable String, @Nullable Integer>> f3 = new Bar<@Nullable String, @Nullable Integer[], @Nullable Baz<@Nullable String, @Nullable Integer>>();",
" @Nullable Bar<@Nullable String, Integer@Nullable [], @Nullable Baz<@Nullable String, @Nullable Integer>> f3 = new Bar<@Nullable String, Integer@Nullable [], @Nullable Baz<@Nullable String, @Nullable Integer>>();",
" }",
"}")
.expectOutput(
Expand Down Expand Up @@ -775,4 +775,50 @@ public void multipleInlineLocalVariableTest() {
ImmutableList.of(ImmutableList.of(1, 0))))
.start();
}

@Test
public void nullableArrayAdditionOnReference() {
injectorTestHelper
.addInput(
"Foo.java",
"package test;",
"import javax.annotation.Nullable;",
"public class Foo {",
" Object[] h = new Object[4];",
"}")
.expectOutput(
"package test;",
"import javax.annotation.Nullable;",
"public class Foo {",
" Object@Nullable [] h = new Object[4];",
"}")
.addChanges(
new AddTypeUseMarkerAnnotation(
new OnField("Foo.java", "test.Foo", Collections.singleton("h")),
"javax.annotation.Nullable"))
.start();
}

@Test
public void nullableArrayDeletionOnReference() {
injectorTestHelper
.addInput(
"Foo.java",
"package test;",
"import javax.annotation.Nullable;",
"public class Foo {",
" Object@Nullable [] h = new Object[4];",
"}")
.expectOutput(
"package test;",
"import javax.annotation.Nullable;",
"public class Foo {",
" Object[] h = new Object[4];",
"}")
.addChanges(
new RemoveTypeUseMarkerAnnotation(
new OnField("Foo.java", "test.Foo", Collections.singleton("h")),
"javax.annotation.Nullable"))
.start();
}
}
Loading