typetools / typetools/checker-framework

Remove `METHOD` from `@Target` meta-annotation of `LengthOf`

Open
#2,557 4 comments 0 reactions 1 assignee View on GitHub

@kelloggm is already working on this.

Since Jun 20, 2019.

Index
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:

  1. Remove METHOD from LengthOf's @Target meta-annotation.
  2. Remove the special-case handling of String.length in isLengthOfMethodInvocation.

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.