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
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
* @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());
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);
}
/**
*/
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);
}
}