-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
Changes from 12 commits
0a684b2
af50c5b
e9aa2e9
718443e
153f1e5
855c505
19a26ba
d49f763
28871ad
74a9411
b353b9f
92eb3f0
87115d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.declaration.CtTypeMember; | ||
import spoon.reflect.declaration.CtTypeParameter; | ||
import spoon.reflect.declaration.CtTypedElement; | ||
import spoon.reflect.declaration.ModifierKind; | ||
import spoon.reflect.factory.Factory; | ||
import spoon.reflect.reference.CtActualTypeContainer; | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. explain this complex condition with a fat comment? |
||
continue; | ||
} | ||
|
||
prettyPrinter.scan(annotation); | ||
printer.writeln(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,22 +169,37 @@ static boolean hasAnnotationWithType(Annotation a, CtAnnotatedElementType elemen | |
if (a.resolvedType == null) { | ||
return false; | ||
} | ||
|
||
// JLS says: | ||
// "If an annotation of type java.lang.annotation.Target is not present on the declaration of an annotation type T, | ||
// then T is applicable in all declaration contexts except type parameter declarations, and in no type contexts." | ||
boolean shouldTargetAnnotationExists = (elementType == CtAnnotatedElementType.TYPE_USE || elementType == CtAnnotatedElementType.TYPE_PARAMETER); | ||
boolean targetAnnotationExists = false; | ||
|
||
for (AnnotationBinding annotation : a.resolvedType.getAnnotations()) { | ||
if (!"Target".equals(CharOperation.charToString(annotation.getAnnotationType().sourceName()))) { | ||
continue; | ||
} | ||
targetAnnotationExists = true; | ||
Object value = annotation.getElementValuePairs()[0].value; | ||
if (value == null || !value.getClass().isArray()) { | ||
if (value == null) { | ||
continue; | ||
} | ||
Object[] fields = (Object[]) value; | ||
for (Object field : fields) { | ||
if (field instanceof FieldBinding && elementType.name().equals(CharOperation.charToString(((FieldBinding) field).name))) { | ||
return true; | ||
if (value instanceof FieldBinding && elementType.name().equals(CharOperation.charToString(((FieldBinding) value).name))) { | ||
return true; | ||
} | ||
if (value.getClass().isArray()) { | ||
Object[] fields = (Object[]) value; | ||
for (Object field : fields) { | ||
if (field instanceof FieldBinding && elementType.name().equals(CharOperation.charToString(((FieldBinding) field).name))) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. don't have -> have not |
||
return !shouldTargetAnnotationExists && !targetAnnotationExists; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,7 +113,7 @@ public class ParentExiter extends CtInheritanceScanner { | |
|
||
private CtElement child; | ||
private ASTNode childJDT; | ||
private Map<CtTypedElement<?>, List<CtTypeReference<? extends java.lang.annotation.Annotation>>> annotationsMap = new HashMap<>(); | ||
private Map<CtTypedElement<?>, List<CtAnnotation>> annotationsMap = new HashMap<>(); | ||
|
||
/** | ||
* @param jdtTreeBuilder | ||
|
@@ -133,29 +133,43 @@ public void setChild(ASTNode child) { | |
@Override | ||
public void scanCtElement(CtElement e) { | ||
if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) { | ||
e.addAnnotation((CtAnnotation<?>) child); | ||
// we check if the current element can have the annotation attached | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. "can be sure" or "cannot be sure"? |
||
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) { | ||
e.addAnnotation((CtAnnotation<?>) child); | ||
} | ||
|
||
// in this case the annotation should be (also) attached to the type | ||
if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) { | ||
List<CtTypeReference<? extends java.lang.annotation.Annotation>> annotations = new ArrayList<>(); | ||
List<CtAnnotation> annotations = new ArrayList<>(); | ||
if (!annotationsMap.containsKey(e)) { | ||
annotationsMap.put((CtTypedElement<?>) e, annotations); | ||
} else { | ||
annotations = annotationsMap.get(e); | ||
} | ||
annotations.add(((CtAnnotation) child).getType()); | ||
annotations.add((CtAnnotation) child.clone()); | ||
annotationsMap.put((CtTypedElement<?>) e, annotations); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
private void substituteAnnotation(CtTypedElement ele) { | ||
if (annotationsMap.containsKey(ele)) { | ||
List<CtTypeReference<? extends java.lang.annotation.Annotation>> annotations = annotationsMap.get(ele); | ||
for (CtTypeReference<? extends java.lang.annotation.Annotation> annotation : annotations) { | ||
final CtAnnotation<? extends java.lang.annotation.Annotation> targetAnnotation = ele.getAnnotation(annotation); | ||
ele.removeAnnotation(targetAnnotation); | ||
if (!ele.getType().getAnnotations().contains(targetAnnotation)) { | ||
ele.getType().addAnnotation(targetAnnotation); | ||
List<CtAnnotation> annotations = annotationsMap.get(ele); | ||
for (CtAnnotation annotation : annotations) { | ||
|
||
// in case of noclasspath we attached previously the element: | ||
// if we are here, we may have find an element for whom it's a better place | ||
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() && annotation.isParentInitialized()) { | ||
CtElement parent = annotation.getParent(); | ||
parent.removeAnnotation(annotation); | ||
} | ||
|
||
if (!ele.getType().getAnnotations().contains(annotation)) { | ||
ele.getType().addAnnotation(annotation.clone()); | ||
} | ||
} | ||
annotationsMap.remove(ele); | ||
|
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.
Unless you create an inline class