[weld-commits] Weld SVN: r5497 - in core/trunk/impl/src/main/java/org/jboss/weld: bean/interceptor and 3 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Mon Jan 18 18:09:49 EST 2010


Author: pete.muir at jboss.org
Date: 2010-01-18 18:09:48 -0500 (Mon, 18 Jan 2010)
New Revision: 5497

Modified:
   core/trunk/impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java
   core/trunk/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
   core/trunk/impl/src/main/java/org/jboss/weld/bean/interceptor/ClassInterceptionHandlerFactory.java
   core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldConstructorImpl.java
   core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldFieldImpl.java
   core/trunk/impl/src/main/java/org/jboss/weld/jsf/JsfApiAbstraction.java
   core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflectionAccess.java
   core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflections.java
Log:
Use some of these new-fangled ideas like generics

Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/DisposalMethod.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -213,7 +213,7 @@
             if (type instanceof Class<?>)
             {
                Class<?> clazz = (Class<?>) type;
-               if (SecureReflections.methodExists(clazz, disposalMethodInjectionPoint.getName(), disposalMethodInjectionPoint.getParameterTypesAsArray()))
+               if (SecureReflections.isMethodExists(clazz, disposalMethodInjectionPoint.getName(), disposalMethodInjectionPoint.getParameterTypesAsArray()))
                {
                   methodDeclaredOnTypes = true;
                   continue;

Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -162,7 +162,7 @@
          {
             if (type instanceof Class<?>)
             {
-               if (SecureReflections.methodExists((Class<?>) type, getWeldAnnotated().getName(), getWeldAnnotated().getParameterTypesAsArray()))
+               if (SecureReflections.isMethodExists((Class<?>) type, getWeldAnnotated().getName(), getWeldAnnotated().getParameterTypesAsArray()))
                {
                   methodDeclaredOnTypes = true;
                   continue;

Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/interceptor/ClassInterceptionHandlerFactory.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/interceptor/ClassInterceptionHandlerFactory.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/interceptor/ClassInterceptionHandlerFactory.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -48,7 +48,7 @@
       {
          // this is not a managed instance - assume no-argument constructor exists
          Constructor<T> constructor = (Constructor<T>) SecureReflections.getDeclaredConstructor(clazz);
-         T interceptorInstance = SecureReflections.ensureConstructorAccessible(constructor).newInstance();
+         T interceptorInstance = SecureReflections.ensureAccessible(constructor).newInstance();
          // inject
          manager.createInjectionTarget(manager.createAnnotatedType(clazz)).inject(interceptorInstance, creationalContext);
          return new DirectClassInterceptionHandler<T>(interceptorInstance, clazz);

Modified: core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldConstructorImpl.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldConstructorImpl.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldConstructorImpl.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -239,7 +239,7 @@
     */
    public T newInstance(Object... parameters) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
    {
-      return SecureReflections.ensureConstructorAccessible(getDelegate()).newInstance(parameters);
+      return SecureReflections.ensureAccessible(getDelegate()).newInstance(parameters);
    }
 
    /**

Modified: core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldFieldImpl.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldFieldImpl.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/introspector/jlr/WeldFieldImpl.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -91,7 +91,7 @@
 
    public void set(Object instance, Object value) throws IllegalArgumentException, IllegalAccessException
    {
-      SecureReflections.ensureFieldAccessible(field).set(instance, value);
+      SecureReflections.ensureAccessible(field).set(instance, value);
    }
 
    public void setOnInstance(Object instance, Object value) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException
@@ -104,7 +104,7 @@
    {
       try
       {
-         return (T) SecureReflections.ensureFieldAccessible(getDelegate()).get(instance);
+         return (T) SecureReflections.ensureAccessible(getDelegate()).get(instance);
       }
       catch (Exception e)
       {

Modified: core/trunk/impl/src/main/java/org/jboss/weld/jsf/JsfApiAbstraction.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/jsf/JsfApiAbstraction.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/jsf/JsfApiAbstraction.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -46,7 +46,7 @@
       double version = 2.0;
       if (this.FACES_CONTEXT != null)
       {
-         version = SecureReflections.methodExists(FACES_CONTEXT, "isPostback") ? 2.0 : 1.2;
+         version = SecureReflections.isMethodExists(FACES_CONTEXT, "isPostback") ? 2.0 : 1.2;
       }
       MINIMUM_API_VERSION = version;
    }

Modified: core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflectionAccess.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflectionAccess.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflectionAccess.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -23,7 +23,7 @@
  * Helper class for doing work in a privileged context under the
  * "weld.reflection" permission
  */
-abstract class SecureReflectionAccess
+abstract class SecureReflectionAccess<T>
 {
    /**
     * Performs the work and returns the result
@@ -31,7 +31,7 @@
     * @return The value of the operation
     * @throws Exception If the operation failed
     */
-   public Object run() throws Exception
+   public T run() throws Exception
    {
 //      SecurityManager securityManager = System.getSecurityManager();
 //      if (securityManager != null)
@@ -60,7 +60,7 @@
     * 
     * @return The result of the work
     */
-   public Object runAndWrap()
+   public T runAndWrap()
    {
       try
       {
@@ -80,7 +80,7 @@
     * @throws NoSuchFieldException If a field with the specified name is not
     *            found.
     */
-   public Object runAsFieldAccess() throws NoSuchFieldException
+   public T runAsFieldAccess() throws NoSuchFieldException
    {
       try
       {
@@ -112,7 +112,7 @@
     * @throws NoSuchMethodException If a method with the specified name is not
     *            found.
     */
-   public Object runAsMethodAccess() throws NoSuchMethodException
+   public T runAsMethodAccess() throws NoSuchMethodException
    {
       try
       {
@@ -156,7 +156,7 @@
     * @throws InvocationTargetException I the underlying method throws an
     *            exception.
     */
-   public Object runAsInvocation() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
+   public T runAsInvocation() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
    {
       try
       {
@@ -180,15 +180,15 @@
       }
       catch (IllegalAccessException e)
       {
-         throw (IllegalAccessException) e;
+         throw e;
       }
       catch (IllegalArgumentException e)
       {
-         throw (IllegalArgumentException) e;
+         throw e;
       }
       catch (InvocationTargetException e)
       {
-         throw (InvocationTargetException) e;
+         throw e;
       }
       catch (Exception e)
       {
@@ -209,7 +209,7 @@
     *            the class has no nullary constructor; or if the instantiation
     *            fails for some other reason.
     */
-   public Object runAsInstantiation() throws InstantiationException, IllegalAccessException
+   public T runAsInstantiation() throws InstantiationException, IllegalAccessException
    {
       try
       {
@@ -229,11 +229,11 @@
       }
       catch (InstantiationException e)
       {
-         throw (InstantiationException) e;
+         throw e;
       }
       catch (IllegalAccessException e)
       {
-         throw (IllegalAccessException) e;
+         throw e;
       }
       catch (Exception e)
       {
@@ -241,6 +241,6 @@
       }
    }
 
-   protected abstract Object work() throws Exception;
+   protected abstract T work() throws Exception;
 
 }

Modified: core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflections.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflections.java	2010-01-18 22:37:16 UTC (rev 5496)
+++ core/trunk/impl/src/main/java/org/jboss/weld/util/reflection/SecureReflections.java	2010-01-18 23:09:48 UTC (rev 5497)
@@ -48,10 +48,10 @@
     */
    public static Field getField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException
    {
-      return (Field) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Field>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Field work() throws Exception
          {
             return clazz.getField(fieldName);
          }
@@ -69,10 +69,10 @@
     */
    public static Field getDeclaredField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException
    {
-      return (Field) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Field>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Field work() throws Exception
          {
             return clazz.getDeclaredField(fieldName);
          }
@@ -88,10 +88,10 @@
     */
    public static Field[] getFields(final Class<?> clazz)
    {
-      return (Field[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Field[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Field[] work() throws Exception
          {
             return clazz.getFields();
          }
@@ -107,10 +107,10 @@
     */
    public static Field[] getDeclaredFields(final Class<?> clazz)
    {
-      return (Field[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Field[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Field[] work() throws Exception
          {
             return clazz.getDeclaredFields();
          }
@@ -129,10 +129,10 @@
     */
    public static Method getMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException
    {
-      return (Method) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Method>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Method work() throws Exception
          {
             return clazz.getMethod(methodName, parameterTypes);
          }
@@ -151,10 +151,10 @@
     */
    public static Method getDeclaredMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException
    {
-      return (Method) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Method>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Method work() throws Exception
          {
             return clazz.getDeclaredMethod(methodName, parameterTypes);
          }
@@ -170,10 +170,10 @@
     */
    public static Method[] getMethods(final Class<?> clazz)
    {
-      return (Method[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Method[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Method[] work() throws Exception
          {
             return clazz.getMethods();
          }
@@ -189,10 +189,10 @@
     */
    public static Method[] getDeclaredMethods(final Class<?> clazz)
    {
-      return (Method[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Method[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Method[] work() throws Exception
          {
             return clazz.getDeclaredMethods();
          }
@@ -210,10 +210,10 @@
     */
    public static Constructor<?> getConstructor(final Class<?> clazz, final Class<?>... parameterTypes) throws NoSuchMethodException
    {
-      return (Constructor<?>) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Constructor<?>>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Constructor<?> work() throws Exception
          {
             return clazz.getConstructor(parameterTypes);
          }
@@ -231,10 +231,10 @@
     */
    public static Constructor<?> getDeclaredConstructor(final Class<?> clazz, final Class<?>... parameterTypes) throws NoSuchMethodException
    {
-      return (Constructor<?>) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Constructor<?>>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Constructor<?> work() throws Exception
          {
             return clazz.getDeclaredConstructor(parameterTypes);
          }
@@ -250,10 +250,10 @@
     */
    public static Constructor<?>[] getConstructors(final Class<?> clazz)
    {
-      return (Constructor<?>[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Constructor<?>[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Constructor<?>[] work() throws Exception
          {
             return clazz.getConstructors();
          }
@@ -269,10 +269,10 @@
     */
    public static Constructor<?>[] getDeclaredConstructors(final Class<?> clazz)
    {
-      return (Constructor<?>[]) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Constructor<?>[]>()
       {
          @Override
-         protected Object work() throws Exception
+         protected Constructor<?>[] work() throws Exception
          {
             return clazz.getDeclaredConstructors();
 
@@ -293,49 +293,23 @@
     *            method
     * @see java.lang.reflect.Method#invoke(Object, Object...)
     */
-   public static Object invoke(final Object instance, final Method method, final Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
+   public static <T> T invoke(final Object instance, final Method method, final Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
    {
-      return new SecureReflectionAccess()
+      return new SecureReflectionAccess<T>()
       {
+
          @Override
-         protected Object work() throws Exception
+         protected T work() throws Exception
          {
-            return ensureMethodAccessible(method).invoke(instance, parameters);
-         }
-      }.runAsInvocation();
-   }
+            Object result = ensureAccessible(method).invoke(instance, parameters);
 
-   /**
-    * Makes a method accessible
-    * 
-    * @param method The method to make accessible
-    * @return The accessible method
-    */
-   public static Method ensureMethodAccessible(final Method method)
-   {
-      return (Method) ensureAccessible(method);
-   }
+            @SuppressWarnings("unchecked")
+            T t = (T) result;
 
-   /**
-    * Makes a field accessible
-    * 
-    * @param field The field to make accessible
-    * @return The accessible field
-    */
-   public static Field ensureFieldAccessible(final Field field)
-   {
-      return (Field) ensureAccessible(field);
-   }
+            return t;
+         }
 
-   /**
-    * Makes a constructor accessible
-    * 
-    * @param constructor The constructor to make accessible
-    * @return The accessible constructor
-    */
-   public static <T> Constructor<T> ensureConstructorAccessible(final Constructor<T> constructor)
-   {
-      return (Constructor<T>) ensureAccessible(constructor);
+      }.runAsInvocation();
    }
 
    /**
@@ -344,12 +318,13 @@
     * @param accessibleObjects The object to manipulate
     * @return The accessible object
     */
-   private static AccessibleObject ensureAccessible(final AccessibleObject accessibleObject)
+   public static <T extends AccessibleObject> T ensureAccessible(final T accessibleObject)
    {
-      return (AccessibleObject) new SecureReflectionAccess()
+      return new SecureReflectionAccess<T>()
       {
+
          @Override
-         protected Object work() throws Exception
+         protected T work() throws Exception
          {
             if (!accessibleObject.isAccessible())
             {
@@ -357,6 +332,7 @@
             }
             return accessibleObject;
          }
+
       }.runAndWrap();
    }
 
@@ -373,12 +349,12 @@
     *            method
     * @see java.lang.reflect.Method#invoke(Object, Object...)
     */
-   public static Object invoke(final Object instance, final String methodName, final Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
+   public static <T> T invoke(final Object instance, final String methodName, final Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
    {
-      return new SecureReflectionAccess()
+      return new SecureReflectionAccess<T>()
       {
          @Override
-         protected Object work() throws Exception
+         protected T work() throws Exception
          {
             Class<?>[] parameterTypes = new Class<?>[parameters.length];
             for (int i = 0; i < parameters.length; i++)
@@ -386,7 +362,14 @@
                parameterTypes[i] = parameters[i].getClass();
             }
             Method method = getMethod(instance.getClass(), methodName, parameterTypes);
-            return ensureMethodAccessible(method).invoke(instance, parameters);
+
+            Object result = ensureAccessible(method).invoke(instance, parameters);
+
+            @SuppressWarnings("unchecked")
+            T t = (T) result;
+
+            return t;
+
          }
       }.runAsInvocation();
    }
@@ -439,7 +422,7 @@
     */
    public static Method lookupMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException
    {
-      return (Method) new SecureReflectionAccess()
+      return new SecureReflectionAccess<Method>()
       {
 
          private Method lookupMethod(final Class<?> currentClass) throws NoSuchMethodException
@@ -470,7 +453,7 @@
          }
 
          @Override
-         protected Object work() throws Exception
+         protected Method work() throws Exception
          {
             return lookupMethod(clazz);
          }
@@ -487,7 +470,7 @@
    {
       try
       {
-         Class<?>[] valueClasses = (Class<?>[]) invoke(annotation, "value");
+         Class<?>[] valueClasses = invoke(annotation, "value");
          return valueClasses;
       }
       catch (Exception e)
@@ -504,7 +487,7 @@
     * @param parameterTypes The parameter types of the method
     * @return true if method is present, false otherwise
     */
-   public static boolean methodExists(Class<?> clazz, String methodName, Class<?>... parameterTypes)
+   public static boolean isMethodExists(Class<?> clazz, String methodName, Class<?>... parameterTypes)
    {
       try
       {



More information about the weld-commits mailing list