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
10 changes: 9 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 @@ -24,6 +24,7 @@

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 +609,14 @@ 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();
int index = type.toString().indexOf('[');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right way to compute the index. The type.toString() will return string representation of the type, not how it is written in the source code. You should find the index via:

for (JavaToken javaToken : range) {
   // Token T = javaToken.asString();
   // Your logic
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to this logic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avenger2597 Nice, this is much cleaner now.

if (tokenRange.isPresent() && tokenRange.get().toRange().isPresent()) {
Range range = tokenRange.get().toRange().get();
range = range.withBeginColumn(range.begin.column + index);
return range;
}
return null;
}
if (type instanceof PrimitiveType) {
if (type.getRange().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.nodeTypes.NodeWithRange;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.Type;
import com.google.common.collect.ImmutableList;
import edu.ucr.cs.riple.injector.Helper;
Expand Down Expand Up @@ -68,7 +69,12 @@ public Modification computeTextModificationOnType(Type type, AnnotationExpr anno
if (range == null) {
return null;
}
return new Insertion(annotationExpr.toString(), range.begin);
String annotationExpression = annotationExpr.toString();
// Need to add a space between annotation and simpleName if type is array type
if (type instanceof ArrayType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avenger2597 As mentioned previously in this comment your changes should be in findSimpleNameRangeInTypeName method where we are returning the array's component type as the place to insert / remove annotation for arrays. You should just change the existing case for array and use type.getTokenRange() to compute the range right before the first [.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

annotationExpression = " " + annotationExpression;
}
return new Insertion(annotationExpression, range.begin);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public void additionOnArrayTest() {
"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;",
" Map<@Nullable T, @Nullable T> @Nullable [] f1;",
" String @Nullable [] f2;",
" }",
"}")
.addChanges(
Expand Down
Loading