Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 383950 - [1.8][compiler] Type annotations must have target type meta annotation TYPE_USE
Summary: [1.8][compiler] Type annotations must have target type meta annotation TYPE_USE
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: BETA J8   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 287648
  Show dependency tree
 
Reported: 2012-06-29 19:54 EDT by Srikanth Sankaran CLA
Modified: 2020-01-30 13:22 EST (History)
0 users

See Also:


Attachments
Proposed fix (7.21 KB, patch)
2012-07-05 06:19 EDT, Jay Arthanareeswaran CLA
no flags Details | Diff
Updated patch (12.62 KB, patch)
2012-07-09 10:26 EDT, Jay Arthanareeswaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Srikanth Sankaran CLA 2012-06-29 19:54:55 EDT
BETA_JAVA8:

From the spec:

"If an annotation is not meta-annotated with @Target 
(which would be poor style!), then the compiler treats the 
annotation as if it is meta-annotated with all of the 
ElementType enum constants that appear in Java 7"

This would mean that the following snippet should not
compile: but it does at the moment.

// ----------

@interface Marker {
}
public class X  extends @Marker Object {
}
Comment 1 Jay Arthanareeswaran CLA 2012-07-02 10:31:18 EDT
(In reply to comment #0)
> From the spec:
> 
> "If an annotation is not meta-annotated with @Target 
> (which would be poor style!), then the compiler treats the 
> annotation as if it is meta-annotated with all of the 
> ElementType enum constants that appear in Java 7"

What happens to the scenarios where we have enhanced the existing enum constants, for e.g, though ElementType.METHOD was present earlier, annotations on return types are support only now.
Comment 2 Jay Arthanareeswaran CLA 2012-07-02 11:14:01 EDT
(In reply to comment #1)
> What happens to the scenarios where we have enhanced the existing enum
> constants, for e.g, though ElementType.METHOD was present earlier, annotations
> on return types are support only now.

In this code:

@interface Marker {}

@Marker	// 1: Don't complain ?
public class X<@Marker T> {		// 2: Complain ?
	public @Marker Object foo(@Marker Object obj) { // Don't complain both ? 
		return null; 
	}
}
Should we complain only about '2' ?
Comment 3 Srikanth Sankaran CLA 2012-07-02 19:43:18 EDT
(In reply to comment #1)
> (In reply to comment #0)
> > From the spec:
> > 
> > "If an annotation is not meta-annotated with @Target 
> > (which would be poor style!), then the compiler treats the 
> > annotation as if it is meta-annotated with all of the 
> > ElementType enum constants that appear in Java 7"
> 
> What happens to the scenarios where we have enhanced the existing enum
> constants, for e.g, though ElementType.METHOD was present earlier, annotations
> on return types are support only now.

This is easily resolved - The compiler in the course of compiling a project
works with a certain version of JRE - we go by the Target meta annotations
discovered during the compilation. There is no occasion/opportunity to 
discover the state as it used to be some time ago.

(In reply to comment #2)
> (In reply to comment #1)
> > What happens to the scenarios where we have enhanced the existing enum
> > constants, for e.g, though ElementType.METHOD was present earlier, annotations
> > on return types are support only now.
> 
> In this code:
> 
> @interface Marker {}
> 
> @Marker    // 1: Don't complain ?
> public class X<@Marker T> {        // 2: Complain ?
>     public @Marker Object foo(@Marker Object obj) { // Don't complain both ? 
>         return null; 
>     }
> }
> Should we complain only about '2' ?

Yes, All except #2 are Java SE7 locations for writing annotations and
so qualify as targets for an annotation not meta annotated with Target.
Comment 4 Jay Arthanareeswaran CLA 2012-07-05 06:19:52 EDT
Created attachment 218325 [details]
Proposed fix

Patch with new negative tests added.

org.NegativeTypeAnnotationTest.test036() had to be modified to make it pass with this fix. But the changes can be reverted once we can resolve TYPE_USE and TYPE_PARAMETER in our tests. But it doesn't really matter much, I guess.
Comment 5 Srikanth Sankaran CLA 2012-07-05 10:35:51 EDT
The patch looks to be in the right direction, here are some comments:

    - TYPE_USE not, TYPE_US.
    - In Don't complain on both ?, remove the ?
    - Changes to test036 do not have to reverted, if the combined set
      of messages is still valid - didn't check this yet.
    - I think we should introduce a new problem with a distinct message
      for this scenario: "The annotation $0 is disallowed for this location"
      is not informative enough, for a programmer coming from Java SE 7
      expectation that an annotation type not meta annotated with target
      annotation should be applicable everywhere. May be:
      "Only annotation types that explicitly specify TYPE_USE as a target
      can be applied here", likewise for TYPE_PARAMETER.

In order to make the last change, look at the commit http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=c5fca7ef6c294c6139aba614099e9d98b3db43cc.

There is quite a bit of boiler plate code in introducing new IProblem constants
(IProblem, message.properties, ProblemReporter, CompilerInvocationTests all
need to change) and the above commit could serve to show what is required.
Comment 6 Srikanth Sankaran CLA 2012-07-05 10:38:06 EDT
(In reply to comment #5)
>       "Only annotation types that explicitly specify TYPE_USE as a target
>       can be applied here", likewise for TYPE_PARAMETER.

What do you think about:

"Only annotation types that explicitly specify TYPE_USE as a possible target element type can be applied here"
Comment 7 Jay Arthanareeswaran CLA 2012-07-05 12:06:44 EDT
(In reply to comment #6)
> "Only annotation types that explicitly specify TYPE_USE as a possible target
> element type can be applied here"

Do you really want to make it specific to TYPE_USE, even though that is the case today?
Comment 8 Srikanth Sankaran CLA 2012-07-05 12:19:45 EDT
(In reply to comment #7)
> (In reply to comment #6)
> > "Only annotation types that explicitly specify TYPE_USE as a possible target
> > element type can be applied here"
> 
> Do you really want to make it specific to TYPE_USE, even though that is the
> case today?

I am OK with that being an argument: so the message in the properties file
could be 

"Only annotation types that explicitly specify {0} as a possible target
element type can be applied here"

The substitutions could be retrieved from the properties file itself.

I am also fine with there being two hard coded messages for Java SE 8 cases
Comment 9 Jay Arthanareeswaran CLA 2012-07-05 12:36:11 EDT
(In reply to comment #5)
> There is quite a bit of boiler plate code in introducing new IProblem constants
> (IProblem, message.properties, ProblemReporter, CompilerInvocationTests all
> need to change) and the above commit could serve to show what is required.

Actually, if at all anything should change, it's only the error message. Rest of them, for e.g. IProblem#DisallowedTargetForAnnotation, seem to have been written specifically for this. In fact, the only scenario where this message gets used is this.
Comment 10 Srikanth Sankaran CLA 2012-07-05 19:53:46 EDT
(In reply to comment #9)
> (In reply to comment #5)
> > There is quite a bit of boiler plate code in introducing new IProblem constants
> > (IProblem, message.properties, ProblemReporter, CompilerInvocationTests all
> > need to change) and the above commit could serve to show what is required.
> 
> Actually, if at all anything should change, it's only the error message. Rest
> of them, for e.g. IProblem#DisallowedTargetForAnnotation, seem to have been
> written specifically for this. In fact, the only scenario where this message
> gets used is this.

Under what you propose what would be the error message for this:

// ---
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@interface Annot {
	
}
@Annot
class X {
}

We cannot print a message that reads:

"Only annotation types that explicitly specify TYPE as a possible target
element type can be applied here"
Comment 11 Jay Arthanareeswaran CLA 2012-07-09 10:26:12 EDT
Created attachment 218441 [details]
Updated patch

This patch introduces a new problem message.
Comment 12 Jay Arthanareeswaran CLA 2012-07-10 04:31:03 EDT
Released the fix in BETA_JAVA8 with couple of changes suggested by Srikanth:

http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=446232cf1f35af48915a191f1c4500b94c6d46e0