Bug 328575 - Inheritance of annotation fails with generic classes
Summary: Inheritance of annotation fails with generic classes
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Target Milestone: 3.7 RC1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-25 05:04 EDT by Christian CLA
Modified: 2011-05-12 03:42 EDT (History)
4 users (show)

See Also:
eclipse: review+


Attachments
Project that reproduces the problem (16.48 KB, application/zip)
2010-11-18 10:59 EST, Christian CLA
no flags Details
Proposed patch (951 bytes, patch)
2010-11-30 03:01 EST, Walter Harley CLA
no flags Details | Diff
Proposed fix + regression test (18.67 KB, patch)
2011-05-05 16:38 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix + regression test (18.93 KB, patch)
2011-05-05 17:01 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix + regression test (10.65 KB, patch)
2011-05-09 10:50 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christian CLA 2010-10-25 05:04:45 EDT
Build Identifier: M20100909-0800

With generic classes a child class that should inherit the an annotation is not fed to the correesponding processor during compilation.

This works with javac / or with non generic classes.

Reproducible: Always

Steps to Reproduce:
1. Create some annotation with @Inherited 
2. Create a processor for that annotation that puts out what classes it checks
3. Jar up everything for use and add it to some eclipse project as annotation processor

4. Create a generic class that is annotated with the created annotation.
5. Extends the generic class.
6. Go to ErrorLog view and check which classes were checked by the processor.
Comment 1 Christian CLA 2010-10-25 05:07:18 EDT
Because the reproduction steps seem rather top down and cost a lot of time I will supply a Java project readily setup to show this bug if anyone requests it.
Comment 2 Walter Harley CLA 2010-11-13 18:57:43 EST
Hi, Christian.  Yes, a project that demonstrates this would be helpful.

Also, can you take a look at Bug 270754 and make sure that's not part of the issue?  You say that your bug happens with generic classes and not with raw classes, so it is probably unrelated; but 270754 can cause confusion while investigating behavior with @Inherited, so I just thought I'd confirm.
Comment 3 Christian CLA 2010-11-18 10:59:33 EST
Created attachment 183392 [details]
Project that reproduces the problem
Comment 4 Christian CLA 2010-11-18 11:00:25 EST
I checked and verified that this is indeed unrelated to https://bugs.eclipse.org/bugs/show_bug.cgi?id=270754  

I did use "clean" for checking.
Comment 5 Walter Harley CLA 2010-11-30 02:57:30 EST
We determine whether an element has an inherited annotation with RoundEnvImpl.inheritsAnno(ReferenceBinding element, ReferenceBinding anno), which looks like this:

  do {
    AnnotationBinding[] annos = element.getAnnotations();
    for (AnnotationBinding annoBinding : annos) {
      if (annoBinding.getAnnotationType() == anno) {
        // element is annotated with anno
        return true;
      }
    }
  } while (null != (element = element.superclass()));
  return false;

The superclass of TestGenericChild in the sample project is the ParameterizedTypeBinding to TestGeneric<TestPoint>; that is, it is a binding to the parameterized type, not to the generic declaration.  For that binding, getAnnotations() returns an empty list.  

I think the proper behavior here is that the ParameterizedTypeBinding should override getAnnotations() and return genericType().getAnnotations().  With this change, the project behaves properly (errors are reported on both child types).
Comment 6 Walter Harley CLA 2010-11-30 03:01:22 EST
Created attachment 184094 [details]
Proposed patch

Proposed patch to jdt.core.  With this patch, the problematic project compiles correctly (that is, with errors on both TestGenericChild and TestNormalChild.  I will work on an automated test.
Comment 7 Olivier Thomann CLA 2011-05-05 14:44:49 EDT
Walter, do you have a regression test for this one ?
This looks like something that should have made it for 3.7. Might be too late now.
Comment 8 Olivier Thomann CLA 2011-05-05 16:38:47 EDT
Created attachment 194881 [details]
Proposed fix + regression test

Same patch with a regression test based on the attached project.
Walter, please verify.
Comment 9 Olivier Thomann CLA 2011-05-05 17:01:39 EDT
Created attachment 194885 [details]
Proposed fix + regression test

Same patch. Check the error output from the compiler.
Comment 10 Olivier Thomann CLA 2011-05-06 12:35:48 EDT
Walter, if we release, I'd like to do it for next I-build (tonight at 8:00pm Ottawa time).
Comment 11 Olivier Thomann CLA 2011-05-09 10:50:17 EDT
Created attachment 195076 [details]
Proposed fix + regression test

This is another way to reach the same goal but touching only apt and not jdt/core.
Inside the DOM AST package, we do have the same trick to retrieve the generic type in order to return the annotations of a parameterized type.

At this stage I would go with this solution.
Walter, I need your input by the end of today in order to make it into an I-build this week.
Comment 12 Walter Harley CLA 2011-05-09 12:27:40 EDT
Hi, Olivier. Sorry I've not responded - I am traveling for work and I am not allowed to do Eclipse development on any of the computers I have access to. I looked at your latest proposed patch and it looks good to me, but I can't run it or examine it in context. I am fine with this approach so will give my +1.
Comment 13 Olivier Thomann CLA 2011-05-09 13:17:27 EDT
Released for 3.7RC1.
Comment 14 Jay Arthanareeswaran CLA 2011-05-12 03:42:17 EDT
Verified 3.7RC1 with build I20110511-0800.