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

Conversation

avenger2597
Copy link
Contributor

@avenger2597 avenger2597 commented Oct 9, 2024

This PR helps insert annotations at the correct position for array declarations.

For array declarations we want the annotations to be inserted just before the array type brackets. For example :
String @Nullable [] f2;

Currently, the behaviour is that the annotations are inserted before the simpleName type. Like this :
@Nullable String[] f2;

@nimakarimipour nimakarimipour added the bug Something isn't working label Oct 10, 2024
@nimakarimipour nimakarimipour self-requested a review October 10, 2024 16:40
@nimakarimipour
Copy link
Member

nimakarimipour commented Oct 10, 2024

@avenger2597 Please add a description to the PR explaining what it does, the bug it resolves, and how the changes address the issue. Also, update the title to accurately reflect the purpose of the PR.

@avenger2597 avenger2597 changed the title Added changes to handle arrays corectly Insert annotations at the correct position for arrays Oct 10, 2024
Copy link
Member

@nimakarimipour nimakarimipour left a comment

Choose a reason for hiding this comment

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

Couple of comments.


String annotation = annotationExpr.toString();
// Check node for array
int index = node.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 a right way to determine the type. You can use Helper.getTypeFromNode(node) and then call isArray() on that.

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

int index = node.toString().indexOf('[');
// Node has array
if (index != -1) {
int begin = range.begin.column + index;
Copy link
Member

@nimakarimipour nimakarimipour Oct 10, 2024

Choose a reason for hiding this comment

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

This is an unused variable. Should be removed ?

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 this

"import javax.annotation.Nullable;",
"public class Foo {",
" Object[] h = new Object[4];",
" public void test(@Nullable Object[] f) {",
Copy link
Member

@nimakarimipour nimakarimipour Oct 10, 2024

Choose a reason for hiding this comment

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

If we are not doing any operation on this method, remove the method from the test input. Test inputs should be as minimal as possible, containing only the information necessary to demonstrate improvements or bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this test case entirely. Currently using TypeUseAnnotationTest.additionOnArrayTest() to test my chages. Can add more if needed later.

@@ -244,9 +244,9 @@ public void onArrayType() {
"import edu.ucr.UnTainted;",
"public class Foo<T> {",
" public void foo() {",
" @UnTainted Map<String, T[]>[] f0;",
Copy link
Member

@nimakarimipour nimakarimipour Oct 10, 2024

Choose a reason for hiding this comment

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

This failure is due to a bug in your changes, not an issue with the expected output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted to original

" }",
"}")
.addChanges(
new AddMarkerAnnotation(
Copy link
Member

Choose a reason for hiding this comment

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

This test should fail with only AddTypeUseMarkerAnnotation instance not with AddMarkerAnnotation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this test case.

@@ -62,7 +62,17 @@ Modification computeTextModificationOn(T node) {
if (annotAlreadyExists) {
return null;
}
return new Insertion(annotationExpr.toString(), range.begin);

String annotation = annotationExpr.toString();
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a right place to add your changes. Your changes should alter AddTypeUseMarkerAnnotation behavior.

Hint: all you changes should be in here where we are returning array component range.

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

@@ -62,6 +62,35 @@ public void fieldNullableSimple() {
.start();
}

@Test
public void fieldNullableArray() {
Copy link
Member

@nimakarimipour nimakarimipour Oct 10, 2024

Choose a reason for hiding this comment

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

This isn't a suitable name for this test. Unit test names should clearly reflect the specific feature or functionality being tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this test case

@nimakarimipour
Copy link
Member

@avenger2597 Make sure you merge the latest changes on master branch before resolving comments.

@nimakarimipour nimakarimipour self-requested a review October 13, 2024 22:41
@@ -68,6 +69,13 @@ public Modification computeTextModificationOnType(Type type, AnnotationExpr anno
if (range == null) {
return null;
}

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.


if (type instanceof ArrayType) {
ArrayType.Origin origin = ((ArrayType) type).getOrigin();
if (origin == ArrayType.Origin.TYPE) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's not worry about this yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

Copy link
Member

@nimakarimipour nimakarimipour left a comment

Choose a reason for hiding this comment

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

One comment.

Also please write a single and simple unit test for this.

@@ -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.

@nimakarimipour nimakarimipour self-requested a review October 29, 2024 20:25
@nimakarimipour nimakarimipour merged commit ac804c0 into ucr-riple:master Oct 29, 2024
7 checks passed
nimakarimipour added a commit that referenced this pull request Oct 30, 2024
This PR splits all utility methods in `Helper` class into two different classes. (`TypeUtils` and `ASTUtils`). This can wait for #251.

This PR only moves methods among classes, there is not a single change on any method declaration or implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants