FileDocCategorySizeDatePackage
InjectionUtil.javaAPI DocJBoss 4.2.110389Fri Jul 13 20:53:46 BST 2007org.jboss.injection

InjectionUtil

public class InjectionUtil extends Object
Comment
author
Bill Burke
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
Constructors Summary
Methods Summary
public static voidcollapseXmlMethodInjectors(java.util.Set visitedMethods, java.lang.Class clazz, java.util.Map xmlDefinedInjectors, java.util.Map classInjectors)
This method will take a set of XML loaded injectors and collapse them based on spec inheritance rules It will remove injectors that should not be used in the injection of the base component class.

param
visitedMethods
param
clazz
param
xmlDefinedInjectors
param
classInjectors



                                                  
                
   
      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
      Map<AccessibleObject, Injector> xmlInjectors = xmlDefinedInjectors.get(clazz.getName());
      if (xmlInjectors != null)
      {
         Method[] methods = clazz.getDeclaredMethods();
         for (Method method : methods)
         {
            if (method.getParameterTypes().length != 1) continue;

            if (!Modifier.isPrivate(method.getModifiers()))
            {
               if (visitedMethods.contains(method.getName()))
               {
                  xmlInjectors.remove(method); // if not private then it has been overriden
                  continue;
               }
               visitedMethods.add(method.getName());
            }
         }
         classInjectors.putAll(xmlInjectors);
      }
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      collapseXmlMethodInjectors(visitedMethods, clazz.getSuperclass(), xmlDefinedInjectors, classInjectors);
   
public static java.lang.reflect.AccessibleObjectfindInjectionTarget(java.lang.ClassLoader loader, org.jboss.metamodel.descriptor.InjectionTarget target)

      Class clazz = null;
      try
      {
         clazz = loader.loadClass(target.getTargetClass());
      }
      catch (ClassNotFoundException e)
      {
         throw new RuntimeException("<injection-target> class: " + target.getTargetClass() + " was not found nin deployment");
      }

      for (Field field : clazz.getDeclaredFields())
      {
         if (target.getTargetName().equals(field.getName())) return field;
      }

      for (java.lang.reflect.Method method : clazz.getDeclaredMethods())
      {
         if (method.getName().equals(target.getTargetName())) return method;
      }

      throw new RuntimeException("<injection-target> could not be found: " + target.getTargetClass() + "." + target.getTargetName());

   
public static java.lang.ObjectgetAnnotation(java.lang.Class annotation, org.jboss.ejb3.EJBContainer container, java.lang.Class annotatedClass, boolean isContainer)

      if (isContainer)
      {
         return container.resolveAnnotation(annotation);
      }
      else
      {
         return annotatedClass.getAnnotation(annotation);
      }
   
public static java.lang.ObjectgetAnnotation(java.lang.Class annotation, org.jboss.ejb3.EJBContainer container, java.lang.reflect.Method method, boolean isContainer)

      if (isContainer)
      {
         return container.resolveAnnotation(method, annotation);
      }
      else
      {
         return method.getAnnotation(annotation);
      }
   
public static java.lang.ObjectgetAnnotation(java.lang.Class annotation, org.jboss.ejb3.EJBContainer container, java.lang.reflect.Field field, boolean isContainer)

      if (isContainer)
      {
         return container.resolveAnnotation(field, annotation);
      }
      else
      {
         return field.getAnnotation(annotation);
      }
   
public static java.lang.StringgetEncName(java.lang.Class type)

      return "env/" + type.getName();
   
public static java.lang.StringgetEncName(java.lang.reflect.Method method)

      String encName = method.getName().substring(3);
      if (encName.length() > 1)
      {
         encName = encName.substring(0, 1).toLowerCase() + encName.substring(1);
      }
      else
      {
         encName = encName.toLowerCase();
      }

      encName = getEncName(method.getDeclaringClass()) + "/" + encName;
      return encName;
   
public static java.lang.StringgetEncName(java.lang.reflect.Field field)

      return getEncName(field.getDeclaringClass()) + "/" + field.getName();
   
public static java.lang.ClassinjectionTarget(java.lang.String encName, org.jboss.metamodel.descriptor.Ref ref, InjectionContainer container, java.util.Map classInjectors)

      if (ref.getInjectionTarget() != null)
      {
         Class injectionType;
         // todo, get injection target class
         AccessibleObject ao = findInjectionTarget(container.getClassloader(), ref.getInjectionTarget());
         Map<AccessibleObject, Injector> injectors = classInjectors.get(ref.getInjectionTarget().getTargetClass());
         if (injectors == null)
         {
            injectors = new HashMap<AccessibleObject, Injector>();
            classInjectors.put(ref.getInjectionTarget().getTargetClass().trim(), injectors);
         }
         if (ao instanceof Field)
         {
            injectionType = ((Field) ao).getType();
            injectors.put(ao, new JndiFieldInjector((Field) ao, encName, container.getEnc()));
         }
         else
         {
            injectionType = ((Method) ao).getParameterTypes()[0];
            injectors.put(ao, new JndiMethodInjector((Method) ao, encName, container.getEnc()));
         }
         return injectionType;
      }
      else
      {
         return null;
      }

   
public static java.util.MapprocessAnnotations(InjectionContainer container, java.util.Collection handlers, java.lang.Class clazz)

      Map<AccessibleObject, Injector> classInjectors = new HashMap<AccessibleObject, Injector>();
      HashSet<String> visitedMethods = new HashSet<String>();
      collapseXmlMethodInjectors(visitedMethods, clazz, container.getEncInjections(), classInjectors);

      processClassAnnotations(container, handlers, clazz);
      visitedMethods = new HashSet<String>();
      processMethodAnnotations(container, handlers, visitedMethods, clazz, classInjectors);
      processFieldAnnotations(container, handlers, clazz, classInjectors);
      return classInjectors;
   
public static voidprocessClassAnnotations(InjectionContainer container, java.util.Collection handlers, java.lang.Class clazz)

      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
    
      if (handlers != null)
      {
         for (InjectionHandler handler : handlers)
         {
            handler.handleClassAnnotations(clazz, container);
         }
      }
      
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processClassAnnotations(container, handlers, clazz.getSuperclass());
   
public static voidprocessFieldAnnotations(InjectionContainer container, java.util.Collection handlers, java.lang.Class clazz, java.util.Map classInjectors)

      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
 
      if (handlers != null)
      {
         Field[] fields = clazz.getDeclaredFields();
         for (Field field : fields)
         {
            log.trace("process field annotation for " + field.toGenericString());
            for (InjectionHandler handler : handlers)
            {
               handler.handleFieldAnnotations(field, container, classInjectors);
            }
         }
      }
      
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processFieldAnnotations(container, handlers, clazz.getSuperclass(), classInjectors);
   
public static voidprocessMethodAnnotations(InjectionContainer container, java.util.Collection handlers, java.util.Set visitedMethods, java.lang.Class clazz, java.util.Map classInjectors)

      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
      Method[] methods = clazz.getDeclaredMethods();
      for (Method method : methods)
      {
         if (method.getParameterTypes().length != 1) continue;

         if (!Modifier.isPrivate(method.getModifiers()))
         {
            if (visitedMethods.contains(method.getName()))
            {
               continue;
            }
            visitedMethods.add(method.getName());
         }
        
         if (handlers != null)
         {
            for (InjectionHandler handler : handlers)
            {
               handler.handleMethodAnnotations(method, container, classInjectors);
            }
         }
      }
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processMethodAnnotations(container, handlers, visitedMethods, clazz.getSuperclass(), classInjectors);