[webbeans-commits] Webbeans SVN: r548 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/bootstrap/spi and 4 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Thu Dec 18 10:07:57 EST 2008


Author: pete.muir at jboss.org
Date: 2008-12-18 10:07:57 -0500 (Thu, 18 Dec 2008)
New Revision: 548

Added:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/MethodDescriptor.java
Modified:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/EjbDescriptor.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedClass.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedMethod.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java
   ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockEjbDescriptor.java
Log:
Wire remove methods through to discovery SPI

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -41,8 +41,6 @@
 import org.jboss.webbeans.bindings.CurrentBinding;
 import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
 import org.jboss.webbeans.introspector.AnnotatedItem;
-import org.jboss.webbeans.introspector.AnnotatedMethod;
-import org.jboss.webbeans.introspector.AnnotatedParameter;
 import org.jboss.webbeans.introspector.jlr.AbstractAnnotatedItem.AnnotationMap;
 import org.jboss.webbeans.log.LogProvider;
 import org.jboss.webbeans.log.Logging;
@@ -101,8 +99,6 @@
    protected Class<? extends Annotation> deploymentType;
    // The type
    protected Class<T> type;
-   // The remove method
-   protected AnnotatedMethod<Object> removeMethod;
    // The API types
    protected Set<Class<?>> apiTypes;
    // The injection points
@@ -217,14 +213,7 @@
     */
    protected void initInjectionPoints()
    {
-      injectionPoints = new HashSet<AnnotatedItem<?, ?>>();
-      if (removeMethod != null)
-      {
-         for (AnnotatedParameter<?> injectable : removeMethod.getParameters())
-         {
-            injectionPoints.add(injectable);
-         }
-      }
+      injectionPoints = new HashSet<AnnotatedItem<?,?>>();
    }
 
    /**
@@ -435,16 +424,6 @@
    }
 
    /**
-    * Gets the remove method of the bean
-    * 
-    * @return The remove method
-    */
-   public AnnotatedMethod<?> getRemoveMethod()
-   {
-      return removeMethod;
-   }
-
-   /**
     * Gets the scope type of the bean
     * 
     * @return The scope type
@@ -559,7 +538,6 @@
       buffer.append("Deployment type: " + deploymentType.toString() + "\n");
       buffer.append("Primitive : " + primitive + "\n");
       buffer.append("Declared bean type: " + (declaredBeanType == null ? "null" : declaredBeanType.toString()) + "\n");
-      buffer.append("Remove method: " + (removeMethod == null ? "null" : removeMethod.toString()) + "\n");
       buffer.append(Strings.collectionToString("Binding types: ", getBindingTypes()));
       buffer.append(Strings.collectionToString("API types: ", getTypes()));
       buffer.append(Strings.collectionToString("Injection points: ", getInjectionPoints()));

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -55,7 +55,7 @@
    // Logger
    private static final LogProvider log = Logging.getLogProvider(AbstractClassBean.class);
    // The item representation
-   private AnnotatedClass<T> annotatedItem;
+   protected AnnotatedClass<T> annotatedItem;
    // The injectable fields
    private Set<AnnotatedField<Object>> injectableFields;
    // The initializer methods

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -36,6 +36,7 @@
 
 import org.jboss.webbeans.ManagerImpl;
 import org.jboss.webbeans.bootstrap.spi.EjbDescriptor;
+import org.jboss.webbeans.bootstrap.spi.MethodDescriptor;
 import org.jboss.webbeans.contexts.DependentContext;
 import org.jboss.webbeans.ejb.EjbDescriptorCache;
 import org.jboss.webbeans.introspector.AnnotatedField;
@@ -57,6 +58,10 @@
    
    // The EJB descriptor
    private EjbDescriptor<T> ejbDescriptor;
+   
+   // The remove method on the bean class (do not call!)
+   private AnnotatedMethod<?> removeMethod;
+   
 
    /**
     * Constructor
@@ -168,10 +173,10 @@
       }
 
       // <1 (0) @Destructors
-      Set<AnnotatedMethod<Object>> noArgsRemoveMethods = getNoArgsRemoveMethods();
+      Set<MethodDescriptor> noArgsRemoveMethods = getNoArgsRemoveMethods(ejbDescriptor);
       if (noArgsRemoveMethods.size() == 1)
       {
-         super.removeMethod = noArgsRemoveMethods.iterator().next();
+         this.removeMethod = annotatedItem.getMethod(noArgsRemoveMethods.iterator().next());
          return;
       }
 
@@ -182,19 +187,29 @@
 
    }
 
-   private Set<AnnotatedMethod<Object>> getNoArgsRemoveMethods()
+   private static Set<MethodDescriptor> getNoArgsRemoveMethods(EjbDescriptor<?> ejbDescriptor)
    {
-      Set<AnnotatedMethod<Object>> noArgsRemoveMethods = new HashSet<AnnotatedMethod<Object>>();
-      /*for (AnnotatedMethod<Object> removeMethod : getAnnotatedItem().getAnnotatedMethods(EJB.REMOVE_ANNOTATION))
+      Set<MethodDescriptor> noArgsRemoveMethods = new HashSet<MethodDescriptor>();
+      for (MethodDescriptor removeMethod : ejbDescriptor.getRemoveMethods())
       {
-         if (removeMethod.getParameters().isEmpty())
+         if (removeMethod.getMethodParameterTypes().length == 0)
          {
             noArgsRemoveMethods.add(removeMethod);
          }
-      }*/
+      }
       return noArgsRemoveMethods;
    }
 
+   private void checkDestructorMethods()
+   {
+      // TODO Check that any method annotated @Destructor is actually in the remove method list
+      /*
+      if (removeMethod.isAnnotationPresent(Destructor.class) && !removeMethod.isAnnotationPresent(EJB.REMOVE_ANNOTATION))
+      {
+         throw new DefinitionException("Methods marked @Destructor must also be marked @Remove on " + removeMethod.getName());
+      }*/
+   }
+   
    /**
     * Validates the remove method
     */
@@ -205,12 +220,8 @@
          return;
       }
 
-      /*if (removeMethod.isAnnotationPresent(Destructor.class) && !removeMethod.isAnnotationPresent(EJB.REMOVE_ANNOTATION))
+      if (removeMethod.isAnnotationPresent(Initializer.class))
       {
-         throw new DefinitionException("Methods marked @Destructor must also be marked @Remove on " + removeMethod.getName());
-      }*/
-      else if (removeMethod.isAnnotationPresent(Initializer.class))
-      {
          throw new DefinitionException("Remove methods cannot be initializers on " + removeMethod.getName());
       }
       else if (removeMethod.isAnnotationPresent(Produces.class))
@@ -264,7 +275,7 @@
       try
       {
          DependentContext.INSTANCE.setActive(true);
-         getRemoveMethod().invoke(instance);
+         removeMethod.invokeOnInstance(instance, manager);
       }
       catch (Exception e) 
       {
@@ -333,6 +344,11 @@
       }
 
    }
+   
+   public AnnotatedMethod<?> getRemoveMethod()
+   {
+      return removeMethod;
+   }
 
    /**
     * Validates the bean type

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/ProducerMethodBean.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -44,6 +44,8 @@
 {
    // The underlying method
    private AnnotatedMethod<T> method;
+   
+   private AnnotatedMethod<?> disposalMethod;
 
    /**
     * Constructor
@@ -129,9 +131,9 @@
       {
          injectionPoints.add(parameter);
       }
-      if (removeMethod != null)
+      if (disposalMethod != null)
       {
-         for (AnnotatedParameter<?> injectable : removeMethod.getParameters())
+         for (AnnotatedParameter<?> injectable : disposalMethod.getParameters())
          {
             injectionPoints.add(injectable);
          }
@@ -165,7 +167,7 @@
       Set<AnnotatedMethod<Object>> disposalMethods = manager.resolveDisposalMethods(getType(), getBindingTypes().toArray(new Annotation[0]));
       if (disposalMethods.size() == 1)
       {
-         removeMethod = disposalMethods.iterator().next();
+         this.disposalMethod = disposalMethods.iterator().next();
       }
       else if (disposalMethods.size() > 1)
       {
@@ -204,7 +206,7 @@
     */
    public AnnotatedMethod<?> getDisposalMethod()
    {
-      return removeMethod;
+      return disposalMethod;
    }
 
    /**

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/EjbDescriptor.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/EjbDescriptor.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/EjbDescriptor.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -17,7 +17,6 @@
 
 package org.jboss.webbeans.bootstrap.spi;
 
-import java.lang.reflect.Method;
 
 /**
  * EJB metadata from the EJB descriptor
@@ -55,7 +54,7 @@
     * 
     * @return An iterator over the remove methods
     */
-   public Iterable<Method> getRemoveMethods();
+   public Iterable<MethodDescriptor> getRemoveMethods();
 
    /**
     * Indicates if the bean is stateless

Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/MethodDescriptor.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/MethodDescriptor.java	                        (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/MethodDescriptor.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -0,0 +1,10 @@
+package org.jboss.webbeans.bootstrap.spi;
+
+public interface MethodDescriptor
+{
+   
+   public String getMethodName();
+   
+   public Class<?>[] getMethodParameterTypes();
+   
+}


Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/spi/MethodDescriptor.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedClass.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedClass.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedClass.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -21,6 +21,8 @@
 import java.util.List;
 import java.util.Set;
 
+import org.jboss.webbeans.bootstrap.spi.MethodDescriptor;
+
 /**
  * Represents a Class
  * 
@@ -90,6 +92,14 @@
    public Set<AnnotatedMethod<Object>> getAnnotatedMethods(Class<? extends Annotation> annotationType);
    
    /**
+    * Find the annotated method for a given methodDescriptor
+    * 
+    * @param methodDescriptor
+    * @return
+    */
+   public AnnotatedMethod<Object> getMethod(MethodDescriptor methodDescriptor);
+   
+   /**
     * Gets all with parameters annotated with annotationType
     * 
     * @param annotationType The annotation to match

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedMethod.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedMethod.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedMethod.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -61,6 +61,11 @@
     *         there are no matches.
     */
    public List<AnnotatedParameter<Object>> getAnnotatedParameters(Class<? extends Annotation> metaAnnotationType);
+   
+   /**
+    * Get the parameter types as an array
+    */
+   public Class<?>[] getParameterTypesAsArray();
 
    /**
     * Invokes the method
@@ -70,6 +75,16 @@
     * @return A reference to the instance
     */
    public T invoke(Object instance, Manager manager);
+   
+   /**
+    * Invokes the method on the class of the passed instance, not the declaring 
+    * class. Useful with proxies
+    * 
+    * @param instance The instance to invoke
+    * @param manager The Web Beans manager
+    * @return A reference to the instance
+    */
+   public T invokeOnInstance(Object instance, Manager manager);
 
    /**
     * Invokes the observer method

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -31,6 +31,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.jboss.webbeans.bootstrap.spi.MethodDescriptor;
 import org.jboss.webbeans.introspector.AnnotatedClass;
 import org.jboss.webbeans.introspector.AnnotatedConstructor;
 import org.jboss.webbeans.introspector.AnnotatedField;
@@ -209,7 +210,7 @@
          return Strings.mapToString("Annotation type -> constructor by arguments mappings: ", delegate);
       }
    }
-
+   
    // The implementing class
    private final Class<T> clazz;
    // The type arguments
@@ -503,6 +504,19 @@
    {
       return methodsByAnnotatedParameters.get(annotationType);
    }
+   
+   public AnnotatedMethod<Object> getMethod(MethodDescriptor methodDescriptor)
+   {
+      // TODO Cache?
+      for (AnnotatedMethod<Object> annotatedMethod : methods)
+      {
+         if (annotatedMethod.getName().equals(methodDescriptor.getMethodName()) && annotatedMethod.getParameterTypesAsArray().equals(methodDescriptor.getMethodParameterTypes()))
+         {
+            return annotatedMethod;
+         }
+      }
+      return null;
+   }
 
    /**
     * Gets a string representation of the class

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -184,6 +184,11 @@
    {
       return Collections.unmodifiableList(parameters);
    }
+   
+   public Class<?>[] getParameterTypesAsArray()
+   {
+      return method.getParameterTypes();
+   }
 
    /**
     * Gets the parameter abstractions with a given annotation type
@@ -243,6 +248,12 @@
    {
       return (T) Reflections.invokeAndWrap(getDelegate(), instance, getParameterValues(parameters, manager));
    }
+   
+   @SuppressWarnings("unchecked")
+   public T invokeOnInstance(Object instance, Manager manager)
+   {
+      return (T) Reflections.invokeAndWrap(getName(), getParameterTypesAsArray(), instance, getParameterValues(parameters, manager));
+   }
 
    @SuppressWarnings("unchecked")
    public T invokeWithSpecialValue(Object instance, Class<? extends Annotation> specialParam, Object specialVal, Manager manager)

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/Reflections.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -445,8 +445,6 @@
       }
       catch (IllegalArgumentException e)
       {
-         System.out.println(method.getDeclaringClass() + " " + instance.getClass());
-         e.printStackTrace();
          throw new ExecutionException("Error invoking method " + method.getName() + " on " + method.getDeclaringClass(), e);
       }
       catch (IllegalAccessException e)
@@ -458,6 +456,43 @@
          throw new ExecutionException("Error invoking method " + method.getName() + " on " + method.getDeclaringClass(), e);
       }
    }
+   
+   /**
+    * Invokes a method and wraps exceptions
+    * 
+    * @param methodName The method name to find on the instance and invoke
+    * @param parameterTypes The method name to find on the instance and invoke
+    * @param instance The instance to invoke on
+    * @param parameterValues The parameters values
+    * @return The return value
+    */
+   public static Object invokeAndWrap(String methodName, Class<?>[] parameterTypes, Object instance, Object[] parameterValues)
+   {
+      try
+      {
+         return instance.getClass().getMethod(methodName, parameterTypes).invoke(instance, parameterValues);
+      }
+      catch (IllegalArgumentException e)
+      {
+         throw new ExecutionException("Error invoking method " + methodName + " on " + instance.getClass(), e);
+      }
+      catch (IllegalAccessException e)
+      {
+         throw new ExecutionException("Error invoking method " + methodName + " on " + instance.getClass(), e);
+      }
+      catch (InvocationTargetException e)
+      {
+         throw new ExecutionException("Error invoking method " + methodName + " on " + instance.getClass(), e);
+      }
+      catch (SecurityException e)
+      {
+         throw new ExecutionException("Error invoking method " + methodName + " on " + instance.getClass(), e);
+      }
+      catch (NoSuchMethodException e)
+      {
+         throw new ExecutionException("Error invoking method " + methodName + " on " + instance.getClass(), e);
+      }
+   }
 
    /**
     * Sets value of a field and wraps exceptions

Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockEjbDescriptor.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockEjbDescriptor.java	2008-12-17 18:46:34 UTC (rev 547)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockEjbDescriptor.java	2008-12-18 15:07:57 UTC (rev 548)
@@ -12,6 +12,7 @@
 
 import org.jboss.webbeans.bootstrap.spi.BusinessInterfaceDescriptor;
 import org.jboss.webbeans.bootstrap.spi.EjbDescriptor;
+import org.jboss.webbeans.bootstrap.spi.MethodDescriptor;
 
 public class MockEjbDescriptor<T> implements EjbDescriptor<T>
 {
@@ -39,14 +40,32 @@
       return Collections.emptyList();
    }
 
-   public Iterable<Method> getRemoveMethods()
+   public Iterable<MethodDescriptor> getRemoveMethods()
    {
-      Collection<Method> removeMethods = new HashSet<Method>();
-      for (Method method : type.getMethods())
+      Collection<MethodDescriptor> removeMethods = new HashSet<MethodDescriptor>();
+      for (final Method method : type.getMethods())
       {
          if (method.isAnnotationPresent(Remove.class))
          {
-            removeMethods.add(method);
+            removeMethods.add(new MethodDescriptor()
+            {
+               
+               public Class<?> getDeclaringClass()
+               {
+                  return type;
+               }
+               
+               public String getMethodName()
+               {
+                  return method.getName();
+               }
+               
+               public Class<?>[] getMethodParameterTypes()
+               {
+                  return method.getParameterTypes();
+               }
+               
+            });
          }
       }
       return removeMethods;




More information about the weld-commits mailing list