]> source.dussan.org Git - vaadin-framework.git/commitdiff
Throw real exceptions from helpers in ReflectTools
authorArtur Signell <artur@vaadin.com>
Tue, 20 Dec 2011 13:54:19 +0000 (15:54 +0200)
committerArtur Signell <artur@vaadin.com>
Tue, 20 Dec 2011 13:54:19 +0000 (15:54 +0200)
src/com/vaadin/data/fieldbinder/FormBuilder.java
src/com/vaadin/tools/ReflectTools.java

index 3e78fa85e6310cc56bd7fefe8a2e02af9b7d5d40..9a4703061143b7d9418496aef8c3a523cc724643 100644 (file)
@@ -4,6 +4,7 @@
 package com.vaadin.data.fieldbinder;\r
 \r
 import java.io.Serializable;\r
+import java.lang.reflect.InvocationTargetException;\r
 import java.util.logging.Logger;\r
 \r
 import org.apache.tools.ant.BuildException;\r
@@ -232,7 +233,21 @@ public class FormBuilder implements Serializable {
                 builtField = build(caption, propertyType, fieldType);\r
 \r
                 // Store it in the field\r
-                ReflectTools.setJavaFieldValue(object, f, builtField);\r
+                try {\r
+                    ReflectTools.setJavaFieldValue(object, f, builtField);\r
+                } catch (IllegalArgumentException e) {\r
+                    throw new BuildException(\r
+                            "Could not assign value to field '" + f.getName()\r
+                                    + "'", e);\r
+                } catch (IllegalAccessException e) {\r
+                    throw new BuildException(\r
+                            "Could not assign value to field '" + f.getName()\r
+                                    + "'", e);\r
+                } catch (InvocationTargetException e) {\r
+                    throw new BuildException(\r
+                            "Could not assign value to field '" + f.getName()\r
+                                    + "'", e);\r
+                }\r
             }\r
 \r
             // Bind it to the property id\r
index 03c5b8be92a7b0e6d867072f92380bb796c67830..240adccb73a8fa94e72505e4758f153774d59e15 100644 (file)
@@ -56,9 +56,14 @@ public class ReflectTools {
      * @return The value of the field in the object
      * @throws FormBuilderException
      *             If the field value cannot be determined
+     * @throws InvocationTargetException
+     * @throws IllegalAccessException
+     * @throws IllegalArgumentException
      */
     public static Object getJavaFieldValue(Object object,
-            java.lang.reflect.Field field) throws FormBuilderException {
+            java.lang.reflect.Field field) throws FormBuilderException,
+            IllegalArgumentException, IllegalAccessException,
+            InvocationTargetException {
         PropertyDescriptor pd;
         try {
             pd = new PropertyDescriptor(field.getName(), object.getClass());
@@ -66,26 +71,16 @@ public class ReflectTools {
             if (getter != null) {
                 return getter.invoke(object, (Object[]) null);
             }
-        } catch (Exception e) {
-            // Ignore all problems with getter and try to get the value directly
-            // from the field
+        } catch (IntrospectionException e1) {
+            // Ignore this and try to get directly using the field
         }
 
-        try {
-            if (!field.isAccessible()) {
-                // Try to gain access even if field is private
-                field.setAccessible(true);
-            }
-            return field.get(object);
-        } catch (IllegalArgumentException e) {
-            throw new FormBuilderException("Could not get value for field '"
-                    + field.getName() + "'", e.getCause());
-        } catch (IllegalAccessException e) {
-            throw new FormBuilderException(
-                    "Access denied while assigning built component to field '"
-                            + field.getName() + "' in "
-                            + object.getClass().getName(), e);
+        // Try to get the value or throw an exception
+        if (!field.isAccessible()) {
+            // Try to gain access even if field is private
+            field.setAccessible(true);
         }
+        return field.get(object);
     }
 
     /**
@@ -105,47 +100,25 @@ public class ReflectTools {
      */
     public static void setJavaFieldValue(Object object,
             java.lang.reflect.Field field, Object value)
-            throws FormBuilderException {
+            throws IllegalAccessException, IllegalArgumentException,
+            InvocationTargetException {
         PropertyDescriptor pd;
         try {
             pd = new PropertyDescriptor(field.getName(), object.getClass());
             Method setter = pd.getWriteMethod();
             if (setter != null) {
-                try {
-                    setter.invoke(object, value);
-                } catch (IllegalArgumentException e) {
-                    throw new FormBuilderException(
-                            "Could not assign value to field '"
-                                    + field.getName() + "'", e);
-                } catch (IllegalAccessException e) {
-                    throw new FormBuilderException(
-                            "Access denied while assigning value to field using "
-                                    + setter.getName() + " in "
-                                    + object.getClass().getName(), e);
-                } catch (InvocationTargetException e) {
-                    throw new FormBuilderException(
-                            "Could not assign value to field '"
-                                    + field.getName() + "'", e.getCause());
-                }
+                // Exceptions are thrown forward if this fails
+                setter.invoke(object, value);
             }
         } catch (IntrospectionException e1) {
             // Ignore this and try to set directly using the field
         }
 
-        try {
-            if (!field.isAccessible()) {
-                // Try to gain access even if field is private
-                field.setAccessible(true);
-            }
-            field.set(object, value);
-        } catch (IllegalArgumentException e) {
-            throw new FormBuilderException("Could not assign value to field '"
-                    + field.getName() + "'", e.getCause());
-        } catch (IllegalAccessException e) {
-            throw new FormBuilderException(
-                    "Access denied while assigning value to field '"
-                            + field.getName() + "' in "
-                            + object.getClass().getName(), e);
+        // Try to set the value directly to the field or throw an exception
+        if (!field.isAccessible()) {
+            // Try to gain access even if field is private
+            field.setAccessible(true);
         }
+        field.set(object, value);
     }
 }