From: Artur Signell Date: Tue, 20 Dec 2011 13:54:19 +0000 (+0200) Subject: Throw real exceptions from helpers in ReflectTools X-Git-Tag: 7.0.0.alpha1~75 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c83735579800aa69f84aef9f0de0a57df8aa0d33;p=vaadin-framework.git Throw real exceptions from helpers in ReflectTools --- diff --git a/src/com/vaadin/data/fieldbinder/FormBuilder.java b/src/com/vaadin/data/fieldbinder/FormBuilder.java index 3e78fa85e6..9a47030611 100644 --- a/src/com/vaadin/data/fieldbinder/FormBuilder.java +++ b/src/com/vaadin/data/fieldbinder/FormBuilder.java @@ -4,6 +4,7 @@ package com.vaadin.data.fieldbinder; import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; import java.util.logging.Logger; import org.apache.tools.ant.BuildException; @@ -232,7 +233,21 @@ public class FormBuilder implements Serializable { builtField = build(caption, propertyType, fieldType); // Store it in the field - ReflectTools.setJavaFieldValue(object, f, builtField); + try { + ReflectTools.setJavaFieldValue(object, f, builtField); + } catch (IllegalArgumentException e) { + throw new BuildException( + "Could not assign value to field '" + f.getName() + + "'", e); + } catch (IllegalAccessException e) { + throw new BuildException( + "Could not assign value to field '" + f.getName() + + "'", e); + } catch (InvocationTargetException e) { + throw new BuildException( + "Could not assign value to field '" + f.getName() + + "'", e); + } } // Bind it to the property id diff --git a/src/com/vaadin/tools/ReflectTools.java b/src/com/vaadin/tools/ReflectTools.java index 03c5b8be92..240adccb73 100644 --- a/src/com/vaadin/tools/ReflectTools.java +++ b/src/com/vaadin/tools/ReflectTools.java @@ -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); } }