--- java/org/apache/catalina/util/DefaultAnnotationProcessor.java (revision 704548) +++ java/org/apache/catalina/util/DefaultAnnotationProcessor.java (working copy) @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -42,9 +42,9 @@ * @version $Revision$, $Date$ */ public class DefaultAnnotationProcessor implements AnnotationProcessor { - + protected javax.naming.Context context = null; - + public DefaultAnnotationProcessor(javax.naming.Context context) { this.context = context; } @@ -55,34 +55,46 @@ */ public void postConstruct(Object instance) throws IllegalAccessException, InvocationTargetException { - - Method[] methods = instance.getClass().getDeclaredMethods(); - Method postConstruct = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].isAnnotationPresent(PostConstruct.class)) { - if ((postConstruct != null) - || (methods[i].getParameterTypes().length != 0) - || (Modifier.isStatic(methods[i].getModifiers())) - || (methods[i].getExceptionTypes().length > 0) - || (!methods[i].getReturnType().getName().equals("void"))) { - throw new IllegalArgumentException("Invalid PostConstruct annotation"); + + Class clazz = instance.getClass(); + + // Analyze each super class until there is none + while (clazz != null) { + Method[] methods = clazz.getDeclaredMethods(); + Method postConstruct = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].isAnnotationPresent(PostConstruct.class)) { + if ((postConstruct != null) + || (methods[i].getParameterTypes().length != 0) + || (Modifier.isStatic(methods[i].getModifiers())) + || (methods[i].getExceptionTypes().length > 0) + || (!methods[i].getReturnType().getName().equals( + "void"))) { + throw new IllegalArgumentException( + "Invalid PostConstruct annotation"); + } + postConstruct = methods[i]; } - postConstruct = methods[i]; + } + + // At the end the postconstruct annotated + // method is invoked + if (postConstruct != null) { + boolean accessibility = postConstruct.isAccessible(); + postConstruct.setAccessible(true); + postConstruct.invoke(instance); + postConstruct.setAccessible(accessibility); } + + // will analyze the super class + clazz = clazz.getSuperclass(); + } - // At the end the postconstruct annotated - // method is invoked - if (postConstruct != null) { - boolean accessibility = postConstruct.isAccessible(); - postConstruct.setAccessible(true); - postConstruct.invoke(instance); - postConstruct.setAccessible(accessibility); - } - + } - - + + /** * Call preDestroy method on the specified instance. */ @@ -88,34 +100,46 @@ */ public void preDestroy(Object instance) throws IllegalAccessException, InvocationTargetException { - - Method[] methods = instance.getClass().getDeclaredMethods(); - Method preDestroy = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].isAnnotationPresent(PreDestroy.class)) { - if ((preDestroy != null) - || (methods[i].getParameterTypes().length != 0) - || (Modifier.isStatic(methods[i].getModifiers())) - || (methods[i].getExceptionTypes().length > 0) - || (!methods[i].getReturnType().getName().equals("void"))) { - throw new IllegalArgumentException("Invalid PreDestroy annotation"); + + Class clazz = instance.getClass(); + + // Analyze each super class until there is none + while (clazz != null) { + + Method[] methods = instance.getClass().getDeclaredMethods(); + Method preDestroy = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].isAnnotationPresent(PreDestroy.class)) { + if ((preDestroy != null) + || (methods[i].getParameterTypes().length != 0) + || (Modifier.isStatic(methods[i].getModifiers())) + || (methods[i].getExceptionTypes().length > 0) + || (!methods[i].getReturnType().getName().equals( + "void"))) { + throw new IllegalArgumentException( + "Invalid PreDestroy annotation"); + } + preDestroy = methods[i]; } - preDestroy = methods[i]; } - } + + // At the end the postconstruct annotated + // method is invoked + if (preDestroy != null) { + boolean accessibility = preDestroy.isAccessible(); + preDestroy.setAccessible(true); + preDestroy.invoke(instance); + preDestroy.setAccessible(accessibility); + } + + // will analyze the super class + clazz = clazz.getSuperclass(); - // At the end the postconstruct annotated - // method is invoked - if (preDestroy != null) { - boolean accessibility = preDestroy.isAccessible(); - preDestroy.setAccessible(true); - preDestroy.invoke(instance); - preDestroy.setAccessible(accessibility); } - + } - - + + /** * Inject resources in specified instance. */ @@ -121,7 +145,7 @@ */ public void processAnnotations(Object instance) throws IllegalAccessException, InvocationTargetException, NamingException { - + if (context == null) { // No resource injection return; @@ -126,81 +150,81 @@ // No resource injection return; } - + Class clazz = instance.getClass(); - + while (clazz != null) { // Initialize fields annotations - Field[] fields = instance.getClass().getDeclaredFields(); + Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (fields[i].isAnnotationPresent(Resource.class)) { Resource annotation = (Resource) fields[i].getAnnotation(Resource.class); - lookupFieldResource(context, instance, fields[i], annotation.name()); + lookupFieldResource(context, instance, fields[i], annotation.name(), clazz); } if (fields[i].isAnnotationPresent(EJB.class)) { EJB annotation = (EJB) fields[i].getAnnotation(EJB.class); - lookupFieldResource(context, instance, fields[i], annotation.name()); + lookupFieldResource(context, instance, fields[i], annotation.name(), clazz); } if (fields[i].isAnnotationPresent(WebServiceRef.class)) { - WebServiceRef annotation = + WebServiceRef annotation = (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class); - lookupFieldResource(context, instance, fields[i], annotation.name()); + lookupFieldResource(context, instance, fields[i], annotation.name(), clazz); } if (fields[i].isAnnotationPresent(PersistenceContext.class)) { - PersistenceContext annotation = + PersistenceContext annotation = (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class); - lookupFieldResource(context, instance, fields[i], annotation.name()); + lookupFieldResource(context, instance, fields[i], annotation.name(), clazz); } if (fields[i].isAnnotationPresent(PersistenceUnit.class)) { - PersistenceUnit annotation = + PersistenceUnit annotation = (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class); - lookupFieldResource(context, instance, fields[i], annotation.name()); + lookupFieldResource(context, instance, fields[i], annotation.name(), clazz); } } - + // Initialize methods annotations - Method[] methods = instance.getClass().getDeclaredMethods(); + Method[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].isAnnotationPresent(Resource.class)) { Resource annotation = (Resource) methods[i].getAnnotation(Resource.class); - lookupMethodResource(context, instance, methods[i], annotation.name()); + lookupMethodResource(context, instance, methods[i], annotation.name(), clazz); } if (methods[i].isAnnotationPresent(EJB.class)) { EJB annotation = (EJB) methods[i].getAnnotation(EJB.class); - lookupMethodResource(context, instance, methods[i], annotation.name()); + lookupMethodResource(context, instance, methods[i], annotation.name(), clazz); } if (methods[i].isAnnotationPresent(WebServiceRef.class)) { - WebServiceRef annotation = + WebServiceRef annotation = (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class); - lookupMethodResource(context, instance, methods[i], annotation.name()); + lookupMethodResource(context, instance, methods[i], annotation.name(), clazz); } if (methods[i].isAnnotationPresent(PersistenceContext.class)) { - PersistenceContext annotation = + PersistenceContext annotation = (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class); - lookupMethodResource(context, instance, methods[i], annotation.name()); + lookupMethodResource(context, instance, methods[i], annotation.name(), clazz); } if (methods[i].isAnnotationPresent(PersistenceUnit.class)) { - PersistenceUnit annotation = + PersistenceUnit annotation = (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class); - lookupMethodResource(context, instance, methods[i], annotation.name()); + lookupMethodResource(context, instance, methods[i], annotation.name(), clazz); } } - + clazz = clazz.getSuperclass(); } } - - + + /** * Inject resources in specified field. */ - protected static void lookupFieldResource(javax.naming.Context context, - Object instance, Field field, String name) + protected static void lookupFieldResource(javax.naming.Context context, + Object instance, Field field, String name, Class clazz) throws NamingException, IllegalAccessException { - + Object lookedupResource = null; boolean accessibility = false; - + if ((name != null) && (name.length() > 0)) { lookedupResource = context.lookup(name); @@ -205,9 +229,9 @@ (name.length() > 0)) { lookedupResource = context.lookup(name); } else { - lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName()); + lookedupResource = context.lookup(clazz.getName() + "/" + field.getName()); } - + accessibility = field.isAccessible(); field.setAccessible(true); field.set(instance, lookedupResource); @@ -218,11 +242,11 @@ /** * Inject resources in specified method. */ - protected static void lookupMethodResource(javax.naming.Context context, - Object instance, Method method, String name) + protected static void lookupMethodResource(javax.naming.Context context, + Object instance, Method method, String name, Class clazz) throws NamingException, IllegalAccessException, InvocationTargetException { - - if (!method.getName().startsWith("set") + + if (!method.getName().startsWith("set") || method.getParameterTypes().length != 1 || !method.getReturnType().getName().equals("void")) { throw new IllegalArgumentException("Invalid method resource injection annotation"); @@ -227,10 +251,10 @@ || !method.getReturnType().getName().equals("void")) { throw new IllegalArgumentException("Invalid method resource injection annotation"); } - + Object lookedupResource = null; boolean accessibility = false; - + if ((name != null) && (name.length() > 0)) { lookedupResource = context.lookup(name); @@ -235,10 +259,10 @@ (name.length() > 0)) { lookedupResource = context.lookup(name); } else { - lookedupResource = - context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3)); + lookedupResource = + context.lookup(clazz.getName() + "/" + method.getName().substring(3)); } - + accessibility = method.isAccessible(); method.setAccessible(true); method.invoke(instance, lookedupResource); @@ -244,6 +268,6 @@ method.invoke(instance, lookedupResource); method.setAccessible(accessibility); } - + }