-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
review fix(annotation): bug when using an annotation targeting declaration and types #1774
Conversation
This PR is now ready to be reviewed @pvojtechovsky @monperrus Please note that the PR is potentially breaking. When using a code like: @MyAnnotation
String myField;
@Target({ElementType.TYPE_USE, ElementType.FIELD})
public @interface MyAnnotation {} the model will now contain
So this is not true only for fields, but for all declaration.
So I considered those exceptions for adding the annotation or not. |
@@ -77,6 +78,10 @@ public ElementPrinterHelper(TokenWriter printerTokenWriter, DefaultJavaPrettyPri | |||
*/ | |||
public void writeAnnotations(CtElement element) { | |||
for (CtAnnotation<?> annotation : element.getAnnotations()) { | |||
if (element.isParentInitialized() && element instanceof CtTypeReference && (element.getParent() instanceof CtTypedElement) && element.getParent().getAnnotations().contains(annotation)) { |
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.
explain this complex condition with a fat comment?
@@ -165,4 +167,42 @@ | |||
@Override | |||
@UnsettableProperty | |||
<C extends CtExpression<A>> C setTypeCasts(List<CtTypeReference<?>> types); | |||
|
|||
static CtAnnotatedElementType getAnnotatedElementTypeForCtElement(CtElement element) { |
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.
use an anonymous scanner to avoid multiple instanceof?
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 see how to use easily an anonymous scanner here: I must return a variable and I cannot store it as a local variable if I use the anonymous scanner. And I don't have any method to call to setup the value.
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 agree, it's not straightforward.
I must return a variable and I cannot store it as a local variable if I use the anonymous scanner
Unless you create an inline class
class Result { CtVariable v }
final Result result = new Result();
// then the anonymous class
} | ||
} | ||
} | ||
return false; | ||
|
||
// true here means that the target annotation is not mandatory and we don't have found it |
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.
don't have -> have not
CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e); | ||
annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType; | ||
|
||
// in case of noclasspath, we can be 100% sure, so we guess it must be attached... |
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.
"can be sure" or "cannot be sure"?
merge? |
ok for me |
Fix #1773
In this case the JLS says that: "It is possible for @foo to be both a declaration annotation and a type annotation simultaneously." So we should attach the annotation to the field/method AND to the type in Spoon model.
Then in this case we don't want to print it twice: we print it only for the field/method.