FileDocCategorySizeDatePackage
DefaultAnnotationProcessor.javaAPI DocApache Tomcat 6.0.1410057Fri Jul 20 04:20:30 BST 2007org.apache.catalina.util

DefaultAnnotationProcessor

public class DefaultAnnotationProcessor extends Object implements org.apache.AnnotationProcessor
Verify the annotation and Process it.
author
Fabien Carrion
author
Remy Maucherat
version
$Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
protected Context
context
Constructors Summary
public DefaultAnnotationProcessor(Context context)

    
       
        this.context = context;
    
Methods Summary
protected static voidlookupFieldResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Field field, java.lang.String name)
Inject resources in specified field.

    
        Object lookedupResource = null;
        boolean accessibility = false;
        
        if ((name != null) &&
                (name.length() > 0)) {
            lookedupResource = context.lookup(name);
        } else {
            lookedupResource = context.lookup(instance.getClass().getName() + "/" + field.getName());
        }
        
        accessibility = field.isAccessible();
        field.setAccessible(true);
        field.set(instance, lookedupResource);
        field.setAccessible(accessibility);
    
protected static voidlookupMethodResource(javax.naming.Context context, java.lang.Object instance, java.lang.reflect.Method method, java.lang.String name)
Inject resources in specified method.

        
        if (!method.getName().startsWith("set") 
                || method.getParameterTypes().length != 1
                || !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);
        } else {
            lookedupResource = 
                context.lookup(instance.getClass().getName() + "/" + method.getName().substring(3));
        }
        
        accessibility = method.isAccessible();
        method.setAccessible(true);
        method.invoke(instance, lookedupResource);
        method.setAccessible(accessibility);
    
public voidpostConstruct(java.lang.Object instance)
Call postConstruct method on the specified instance.

        
        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");
                }
                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);
        }
        
    
public voidpreDestroy(java.lang.Object instance)
Call preDestroy method on the specified instance.

        
        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];
            }
        }

        // 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);
        }
        
    
public voidprocessAnnotations(java.lang.Object instance)
Inject resources in specified instance.

        
        if (context == null) {
            // No resource injection
            return;
        }
        
        // Initialize fields annotations
        Field[] fields = instance.getClass().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());
            }
            if (fields[i].isAnnotationPresent(EJB.class)) {
                EJB annotation = (EJB) fields[i].getAnnotation(EJB.class);
                lookupFieldResource(context, instance, fields[i], annotation.name());
            }
            if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
                WebServiceRef annotation = 
                    (WebServiceRef) fields[i].getAnnotation(WebServiceRef.class);
                lookupFieldResource(context, instance, fields[i], annotation.name());
            }
            if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
                PersistenceContext annotation = 
                    (PersistenceContext) fields[i].getAnnotation(PersistenceContext.class);
                lookupFieldResource(context, instance, fields[i], annotation.name());
            }
            if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
                PersistenceUnit annotation = 
                    (PersistenceUnit) fields[i].getAnnotation(PersistenceUnit.class);
                lookupFieldResource(context, instance, fields[i], annotation.name());
            }
        }
        
        // Initialize methods annotations
        Method[] methods = instance.getClass().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());
            }
            if (methods[i].isAnnotationPresent(EJB.class)) {
                EJB annotation = (EJB) methods[i].getAnnotation(EJB.class);
                lookupMethodResource(context, instance, methods[i], annotation.name());
            }
            if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
                WebServiceRef annotation = 
                    (WebServiceRef) methods[i].getAnnotation(WebServiceRef.class);
                lookupMethodResource(context, instance, methods[i], annotation.name());
            }
            if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
                PersistenceContext annotation = 
                    (PersistenceContext) methods[i].getAnnotation(PersistenceContext.class);
                lookupMethodResource(context, instance, methods[i], annotation.name());
            }
            if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
                PersistenceUnit annotation = 
                    (PersistenceUnit) methods[i].getAnnotation(PersistenceUnit.class);
                lookupMethodResource(context, instance, methods[i], annotation.name());
            }
        }