Bug 547706 - Annotation on TypeParameterElement incorrectly reported on type use
Summary: Annotation on TypeParameterElement incorrectly reported on type use
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: APT (show other bugs)
Version: 4.11   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Generic inbox for the JDT-APT component CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-27 19:42 EDT by Éamonn McManus CLA
Modified: 2023-06-01 12:40 EDT (History)
0 users

See Also:


Attachments
Maven project reproducing the problem (3.11 KB, application/zip)
2019-05-27 19:42 EDT, Éamonn McManus CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Éamonn McManus CLA 2019-05-27 19:42:37 EDT
Created attachment 278758 [details]
Maven project reproducing the problem

The compiler is incorrectly reporting to annotation processors that an annotation on a type parameter also appears on uses of that type parameter. For example, consider this class:

package bug;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.ImmutableTypeParameter;

@AutoValue
abstract class Bug<@ImmutableTypeParameter T> {
  abstract T bug();

  static <@ImmutableTypeParameter T> Bug<T> of(T t) {
    return new AutoValue_Bug<>(t);
  }
}

@ImmutableTypeParameter has @Target({TYPE_PARAMETER}), so it is only applicable to type parameters. But when an annotation processor looks at the T that is the return type of the bug() method, it gets back a TypeVariable with the @ImmutableTypeParameter annotation. This leads AutoValue to generate this code:

package bug;

import com.google.errorprone.annotations.ImmutableTypeParameter;
import javax.annotation.Generated;

@Generated("com.google.auto.value.processor.AutoValueProcessor")
final class AutoValue_Bug<@ImmutableTypeParameter T> extends Bug<T> {

  private final @ImmutableTypeParameter T bug;

  AutoValue_Bug(
      @ImmutableTypeParameter T bug) {
    if (bug == null) {
      throw new NullPointerException("Null bug");
    }
    this.bug = bug;
  }

  @Override
  @ImmutableTypeParameter T bug() {
    return bug;
  }
  ...
}

The <@ImmutableTypeParameter T> in the class declaration is correctly copied, because it was returned from TypeParameterElement.getAnnotationMirrors(). However, the other three occurrences of @ImmutableTypeParameter are incorrect, and come about because TypeVariable.getAnnotationMirrors() reported that the type variable T has that annotation. This code then fails to compile, because the compiler correctly rejects @ImmutableTypeParameter applied to a type variable. It can only be applied to a type parameter.

This code compiles fine with javac.
Comment 1 Eclipse Genie CLA 2021-05-17 05:01:44 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 2 Eclipse Genie CLA 2023-06-01 12:40:14 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.