-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
@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. |
There was a problem hiding this 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('['); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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) {", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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;", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this test case
@avenger2597 Make sure you merge the latest changes on master branch before resolving comments. |
@@ -68,6 +69,13 @@ public Modification computeTextModificationOnType(Type type, AnnotationExpr anno | |||
if (range == null) { | |||
return null; | |||
} | |||
|
|||
if (type instanceof ArrayType) { |
There was a problem hiding this comment.
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 [
.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
There was a problem hiding this 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('['); |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to this logic
There was a problem hiding this comment.
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.
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.
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;