Conversation
| AnnotatedTypeMirror defaultType = getDefaultTypeFor(tree); | ||
| defaultTypes.put(tree.getIdentifier(), defaultType); | ||
| ExpressionTree type = tree.getIdentifier(); | ||
| if ((type instanceof ParameterizedTypeTree) && !((ParameterizedTypeTree) type).getTypeArguments().isEmpty()) |
There was a problem hiding this comment.
I think you need to loop through the type arguments and put the corresponding types into defaultTypes. You can take visitParameterizedType as a reference.
There was a problem hiding this comment.
I have a small example like new ArrayList<String>(), and I find super.VisitNewClass() already recursively call the visitParameterizedType() method for tree ArrayList<String>. So, I don't think I need to call that method again.
Besides, what are the type arguments of a new class tree?
As said in the javadoc in NewClassTree.java
A tree node to declare a new instance of a class. For example:
new typeArguments identifier ( arguments )
classBody
The typearguments is in front of the identifier. And when I played around with this example new ArrayList<String>(), using tree.getTypeArguments() returns an empty list.
There was a problem hiding this comment.
You're right, I forgot the typeArguments are not arguments to the class' type parameters. Those are arguments to type parameters on the constructor.
| if ((type instanceof ParameterizedTypeTree) && !((ParameterizedTypeTree) type).getTypeArguments().isEmpty()) | ||
| return super.visitNewClass(tree, unused); |
There was a problem hiding this comment.
We probably don't need the logic here. We should always save defaultType to the cache before visiting child nodes.
There was a problem hiding this comment.
Without the if branch, CI failed on the test case AnonymousProblem.java. It raised the assertion error from the visitParameterizedType
The reason is because: (take new ArrayList<String>() as an example). the NewClassTree does not have String as the type argument, which has 0 type argument, but the ParameterizedTypeTree has 1 type argument, and thus cause the assertion failure.
Previously, for new class tree like
new @Slot1 ArrayList<String>();the default type for slot1 was derived by visiting the identifier tree, not the new class tree. By overriding this method, the default type for slot1 now is derived by visiting the new class tree, which is a more precise context for annotating.And I don't think we have to do anything else, like getting the default type for the type argument
Stringin the above example, as the new method will recursively callvisitParameterizedTypeand do corresponding things there.