Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 415470

Summary: [1.8][compiler] Type annotations on class declaration go vanishing
Product: [Eclipse Project] JDT Reporter: Srikanth Sankaran <srikanth_sankaran>
Component: CoreAssignee: Andrew Clement <aclement>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.4Flags: srikanth_sankaran: review+
Target Milestone: BETA J8   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Bug Depends on:    
Bug Blocks: 409235    
Attachments:
Description Flags
fix and tests none

Description Srikanth Sankaran CLA 2013-08-20 07:08:27 EDT
BETA_JAVA8:

When the following program is compiled:

// --
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; 

@Target({TYPE_USE}) @interface TypeAnnotation { }

@Target({TYPE}) @interface Annotation { }

@Annotation @TypeAnnotation class X {
}

// --

@TypeAnnotation does not seem to make it to the class file.

JSR308, section 2.3 expressly allows such usage. Excerpt:

"A type annotation (one meta-annotated with @Target(ElementType.TYPE USE)) 
is permitted to be written anywhere @Target(ElementType.TYPE) or 
@Target(ElementType.TYPE PARAMETER) would permit—that is, on a class,
interface, or enum declaration, or on a type parameter declaration. Strictly 
speaking, these are declaration sites, not uses of a type. However, it is 
convenient to write a type annotation at a type declaration, as shorthand
for applying it at all uses. For example, @Interned class MyClass { ... } could 
indicate that all uses of MyClass are interned, even though for other classes 
some instances may be interned and other instances not interned."
Comment 1 Srikanth Sankaran CLA 2013-08-20 07:10:13 EDT
Regression test available via: org.eclipse.jdt.core.tests.compiler.regression.JSR308SpecSnippetTests.test030()
Comment 2 Andrew Clement CLA 2013-08-22 11:06:02 EDT
Created attachment 234666 [details]
fix and tests

Fixes the issue and adds a couple more test variants. The change is in ClassFile where runtime annotations are generated I added a parameter to indicate if type use annotations should be included (like in the 'special case' here where they should be). The code does not verify that TYPE_USE is specified and not TYPE_PARAMETER because that check will have been done sooner - so if we reach the code generation point we know it is valid and simply checking whether it is a type annotation is sufficient.
Comment 3 Andrew Clement CLA 2013-08-22 11:07:04 EDT
may want to apply after the patch on bug 415399 to reduce chance of merge conflicts.
Comment 4 Andrew Clement CLA 2013-08-22 12:03:10 EDT
This contribution complies with http://www.eclipse.org/legal/CoO.php
Comment 5 Srikanth Sankaran CLA 2013-08-23 01:10:16 EDT
Fix looks good. Fix and tests released here: http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=e002812d8385bfcab83bbcee92fbb80489aadeb5
Comment 6 Stephan Herrmann CLA 2013-08-25 17:16:25 EDT
(In reply to comment #5)
> Fix looks good. Fix and tests released here:
> http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/
> ?h=BETA_JAVA8&id=e002812d8385bfcab83bbcee92fbb80489aadeb5

Starting with this commit I see regressions in NegativeTypeAnnotationTest#test392119b ff.

Is it just me seeing this (perhaps because I'm still on b92)?
Should these tests simply be adjusted?
Comment 7 Srikanth Sankaran CLA 2013-08-25 23:11:42 EDT
(In reply to comment #6)
> (In reply to comment #5)
> > Fix looks good. Fix and tests released here:
> > http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/
> > ?h=BETA_JAVA8&id=e002812d8385bfcab83bbcee92fbb80489aadeb5
> 
> Starting with this commit I see regressions in
> NegativeTypeAnnotationTest#test392119b ff.
> 
> Is it just me seeing this (perhaps because I'm still on b92)?
> Should these tests simply be adjusted?

Fixed via http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=BETA_JAVA8&id=f5abb716de8ce6d7aaee3cf06a3285e91953dad7. The behavior
was correct, the tests hadn't been updated to account for new behavior.

Andy, please make sure RunAllJava8Tests is green before posting patches.
Comment 8 Andrew Clement CLA 2013-08-26 14:27:29 EDT
Sorry, I had got into the lazy habit of running the JSR308 snippet tests and my type annotation tests, won't happen again.