typetools / typetools/checker-framework
Remove `METHOD` from `@Target` meta-annotation of `LengthOf`
@kelloggm is already working on this.
Since Jun 20, 2019.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
It is surprising that the type annotation LengthOf is meta-annotated as
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER, ElementType.METHOD})
Here is an excerpt from commit 71379bb7de:
-@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
+// Has target of METHOD so that it is stored as a declaration annotation and SameLen Checker can read it.
+@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER, ElementType.METHOD})
public @interface LengthOf {
We have a FAQ that says not to make an annotation applicable to both type uses and declarations.
The METHOD target has surprising results.
For example, it means that when a source has only one occurrence of @LengthOf, its corresponding .class file has two occurrences.
A recent bug report says that this behavior is confusing.
I propose that we clean up the code (and help Shubham, who reported the issue I mentioned above) by doing 2 things:
- Remove
METHODfromLengthOf's@Targetmeta-annotation. - Remove the special-case handling of
String.lengthinisLengthOfMethodInvocation.
Both items require changes to isLengthOfMethodInvocation.
Regarding item 1:
In
isLengthOfMethodInvocation
this line should look for a type, not a declaration, annotation:
AnnotationMirror len = factory.getDeclAnnotation(ele, LengthOf.class);
Regarding item 2:
Remove the first clause from the implementation of isLengthOfMethodInvocation:
public boolean isLengthOfMethodInvocation(ExecutableElement ele) {
if (stringLength.equals(ele)) {
// TODO: Why not just annotate String.length with @LengthOf and thus eliminate the
// special case in this method's implementation?
return true;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.