Bug 45285 - RESOURCES DECLARED IN PARENT CLASSES ARE NOT INJECTED
RESOURCES DECLARED IN PARENT CLASSES ARE NOT INJECTED
Status: RESOLVED FIXED
Product: Tomcat 6
Classification: Unclassified
Component: Servlet & JSP API
unspecified
PC Windows XP
: P3 normal (vote)
: default
Assigned To: Tomcat Developers Mailing List
:
: 45869 (view as bug list)
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2008-06-25 23:58 UTC by divanov
Modified: 2014-02-17 13:54 UTC (History)
2 users (show)



Attachments
postConstruct/preDestroy in super classes (13.29 KB, patch)
2008-10-14 09:56 UTC, Florent BENOIT
Details | Diff
Updated patch for fields/methods (14.85 KB, patch)
2008-10-15 08:48 UTC, Florent BENOIT
Details | Diff
binary of the patch (5.71 KB, patch)
2008-10-15 08:49 UTC, Florent BENOIT
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description divanov 2008-06-25 23:58:28 UTC
According to the section EE.5.2.3 of the Java EE specification:
------------------------
Resource annotations may appear on any of the classes listed above, or on any superclass of any class listed above. A resource annotation on any class in the inheritance hierarchy defines a resource needed by the application component.
------------------------

I use JSF managed beans with @EJB annotated members.
If a resource is annotated in a super class of a class it's not injected.

Example:

public class Base{
@EJB
private SomeService service;
....
}

public class Child implements Base{
....
}

Base class used as managed bean has 'service' injected.
Child class used as managed bean does NOT have 'service' injected.

If I dare suggest the problem source ...

As I can see org.apache.catalina.util.DefaultAnnotationProcessor is used. It has the following lines in its  'public void processAnnotations(Object instance)' method:
....
Field[] fields = instance.getClass().getDeclaredFields();
....
Method[] methods = instance.getClass().getDeclaredMethods();
....


Class.getDeclaredFields method returns 'public, protected, default (package) access, and private fields, but excludes inherited fields'.

The same thing is with Class.getDeclaredMethods.
Comment 1 Mark Thomas 2008-07-09 14:07:06 UTC
A fix has been committed to trunk and proposed for 6.0.x
Comment 2 Mark Thomas 2008-07-13 11:20:51 UTC
This has been fixed in 6.0.x and will be included in 6.0.18 onwards.
Comment 3 Mark Thomas 2008-09-24 05:18:40 UTC
*** Bug 45869 has been marked as a duplicate of this bug. ***
Comment 4 Firstname Lastname 2008-09-26 08:55:24 UTC
I downloaded Tomcat 6.0.18 yesterday and tried again.  With the workaround mentioned in the JSF bug, which is to add the context-param for injectionProvider, the problem is still there

<context-param>
   <param-name>com.sun.faces.injectionProvider</param-name>
   <param-value>com.sun.faces.vendor.WebContainerInjectionProvider</param-value>
</context-param>

Your comments indicated that this bug is fixed in 6.0.18.
Comment 5 Mark Thomas 2008-09-26 11:06:26 UTC
Sorry - my bad. The fixed missed the preDestroy and postContruct methods. I'll get that fixed.
Comment 6 Florent BENOIT 2008-10-14 09:56:40 UTC
Created attachment 22728 [details]
postConstruct/preDestroy in super classes

Here is a patch that is fixing this issue for postConstruct/preDestroy methods in super classes (private)
Comment 7 Firstname Lastname 2008-10-14 11:57:27 UTC
Thanks for the patch.  I don't have the compile environment, so will just close verify the bug in the next build.
Comment 8 Florent BENOIT 2008-10-15 08:48:16 UTC
Created attachment 22738 [details]
Updated patch for fields/methods

I notice that the fix that was done by Mark wasn't right also for attributes/methods.

Because even if we go in the upper classes with a loop, when checking the field/methods we were using instance.getClass() and not the clazz object so fields/methods of superclasses were never obtained.

Also the default name for ENC field/methods should contain the super class name and not the instance name, so I added the clazz parameter to the lookup methods.
Comment 9 Florent BENOIT 2008-10-15 08:49:41 UTC
Created attachment 22739 [details]
binary of the patch

For people without compiling env, here is the class
Comment 10 Mark Thomas 2008-10-20 02:26:06 UTC
Thanks for the review and the new patch. I didn't apply the new patch directly as there were a number of white space changes that made it harder to read. The proposed patch is http://people.apache.org/~markt/patches/2008-10-20-bug45285-v2.patch
Comment 11 Mark Thomas 2008-10-27 06:28:44 UTC
This has been applied to 6.0.x and will be in 6.0.19 onwards.