eclipse-jdt / eclipse-jdt/eclipse.jdt.core
Compiler should allow method annotations between type parameters and return type
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
public class MethodAnnotationTest {
@Target(ElementType.METHOD)
@interface X {
}
/*ERROR:*/ @X Object betweenTypeParamsAndReturnType() {
return null;
}
public @X Object betweenModifiersAndReturnType() {
return null;
}
@X Object noModifiersOrTypeParams() {
return null;
}
}
```
Eclipse compiler reports an error on`@X` in declaration of `betweenTypeParamsAndReturnType()`:
> The annotation @MethodAnnotationTest.X is disallowed for this location
But javac doesn't report any errors given the same source code.
[Language Spec §9.7.4](https://docs.oracle.com/javase/specs/jls/se18/html/jls-9.html#jls-9.7.4):
> It is possible for an annotation to appear at a syntactic location in a program where it could plausibly apply to a declaration, or a type, or both. This can happen in any of the six declaration contexts where modifiers immediately precede the type of the declared entity:
>
> - Method declarations (including elements of annotation interfaces)
>
> - ...
>
> The grammar of the Java programming language unambiguously treats annotations at these locations as modifiers for a declaration ([§8.3](https://docs.oracle.com/javase/specs/jls/se18/html/jls-8.html#jls-8.3)), but that is purely a syntactic matter. Whether an annotation applies to the declaration or to the type of the declared entity - and thus, whether the annotation is a _declaration annotation_ or a _type annotation_ - depends on the applicability of the annotation's interface:
>
> - ...
>
> - If the annotation's interface is applicable in the declaration context corresponding to the declaration _and_ in type contexts, then the annotation is deemed to apply to both the declaration and the type which is closest to the annotation.
In my interpretation, an annotation after the type parameters "could plausibly" apply to the method declaration.
I can't find any other part of the Language Spec that is more clear on interpretation of this specific syntax.
Contributor guide
Assessment
This issue has not been assessed yet.