[jboss-cvs] JBossAS SVN: r74391 - projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 11 10:14:02 EDT 2008


Author: alesj
Date: 2008-06-11 10:14:02 -0400 (Wed, 11 Jun 2008)
New Revision: 74391

Modified:
   projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectionUtils.java
Log:
Handle wrong target w/ member's declaring class.

Modified: projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectionUtils.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectionUtils.java	2008-06-11 13:58:46 UTC (rev 74390)
+++ projects/jboss-reflect/trunk/src/main/org/jboss/reflect/plugins/introspection/ReflectionUtils.java	2008-06-11 14:14:02 UTC (rev 74391)
@@ -24,9 +24,11 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.jboss.util.Strings;
 
@@ -34,11 +36,34 @@
  * ReflectionUtils.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision$
  */
 public class ReflectionUtils
 {
    /**
+    * Check if member and target are ok.
+    *
+    * @param target the target
+    * @param member the member
+    * @param memberType member type info
+    * @throws Throwable for any error
+    */
+   protected static void checkMember(Object target, Member member, String memberType) throws Throwable
+   {
+      if (member == null)
+         throw new IllegalArgumentException("Null " + memberType);
+
+      if (target != null)
+      {
+         Class<?> declaringClass = member.getDeclaringClass();
+         if (declaringClass.isInstance(target) == false)
+            throw new IllegalArgumentException("Target (" + Strings.defaultToString(target) + ") doesn't match " + memberType + " 's (" + member + ") declaring class.");
+      }
+
+   }
+
+   /**
     * Invoke on a method
     * 
     * @param method the method
@@ -49,8 +74,8 @@
     */
    public static Object invoke(Method method, Object target, Object[] arguments) throws Throwable
    {
-      if (method == null)
-         throw new IllegalArgumentException("Null method");
+      checkMember(target, method, "method");
+
       try
       {
          return method.invoke(target, arguments);
@@ -72,6 +97,7 @@
    {
       if (clazz == null)
          throw new IllegalArgumentException("Null clazz");
+
       try
       {
          return clazz.newInstance();
@@ -96,6 +122,7 @@
          throw new IllegalArgumentException("Null class name");
       if (cl == null)
          throw new IllegalArgumentException("Null classloader");
+
       Class<?> clazz = Class.forName(className, false, cl);
       try
       {
@@ -132,6 +159,7 @@
    {
       if (constructor == null)
          throw new IllegalArgumentException("Null constructor");
+
       try
       {
          return constructor.newInstance(arguments);
@@ -152,15 +180,15 @@
     */
    public static Object getField(Field field, Object target) throws Throwable
    {
-      if (field == null)
-         throw new IllegalArgumentException("Null field");
+      checkMember(target, field, "field");
+
       try
       {
          return field.get(target);
       }
       catch (Throwable t)
       {
-         throw handleErrors("set", field, target, null, t);
+         throw handleErrors("get", field, target, null, t);
       }
    }
 
@@ -175,8 +203,8 @@
     */
    public static Object setField(Field field, Object target, Object value) throws Throwable
    {
-      if (field == null)
-         throw new IllegalArgumentException("Null field");
+      checkMember(target, field, "field");
+
       try
       {
          field.set(target, value);
@@ -238,6 +266,7 @@
       Method method = findMethod(clazz, name, parameterTypes);
       if (method == null)
          throw new NoSuchMethodException(clazz + "." + name + " - " +  arrayInfo((Object[]) parameterTypes));
+
       return method;
    }
 
@@ -277,6 +306,7 @@
       Field field = findField(clazz, name);
       if (field == null)
          throw new NoSuchFieldException(clazz + "." + name);
+
       return field;
    }
 
@@ -316,6 +346,7 @@
       Constructor<?> constructor = findConstructor(clazz, parameterTypes);
       if (constructor == null)
          throw new NoSuchMethodException(clazz + " - " + arrayInfo((Object[]) parameterTypes));
+
       return constructor;
    }
 
@@ -336,13 +367,14 @@
       {
          if (target == null)
             throw new IllegalArgumentException("Null target for " + context);
-         ArrayList<String> expected = new ArrayList<String>();
+
+         List<String> expected = new ArrayList<String>();
          if (parameters != null)
          {
             for (int i = 0; i < parameters.length; ++i)
                expected.add(parameters[i].getName());
          }
-         ArrayList<String> actual = new ArrayList<String>();
+         List<String> actual = new ArrayList<String>();
          if (arguments != null)
          {
             for (int i = 0; i < arguments.length; ++i)




More information about the jboss-cvs-commits mailing list