diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java index 9312922d319907256f42c9e97d32db1eb3f3839e..df1facee4ed00dbd3d3d19eac186b9a0c0caea77 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java @@ -266,9 +266,10 @@ public final class ClassUtils * This is useful when you want to set a <code>private</code> field on which you do not have access. Note that * this method should only be used in very special cases. You should consider it as a hack. * </p> + * + * @return a <code>true</code> if <code>fieldName</code> has been modified. */ - public final static void setFieldValue(final Object object, final String fieldName, final Object newValue) - throws IllegalArgumentException + public final static boolean setFieldValue(final Object object, final String fieldName, final Object newValue) { assert object != null : "Unspecified object."; final Class<?> clazz = object.getClass(); @@ -279,7 +280,7 @@ public final class ClassUtils { field.setAccessible(true); field.set(object, newValue); - return; + return true; } } catch (final SecurityException ex) { @@ -288,9 +289,7 @@ public final class ClassUtils } catch (final IllegalAccessException ex) { } - throw new IllegalArgumentException(String.format( - "Cannot set field '%s' of class '%s' to given new value '%s'.", fieldName, clazz.getSimpleName(), - newValue)); + return false; } /** diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java index a04f840e21d2e89391df71ede0ff6746f1d023f8..44ca0f90ef57526b47f874c3eca35114515b6c76 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.common.utilities; import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; @@ -146,13 +147,7 @@ public final class ClassUtilsTest } catch (final AssertionError error) { } - try - { - ClassUtils.setFieldValue(myClass, "doesNotExist", null); - fail("Field name 'doesNotExist' not found."); - } catch (final IllegalArgumentException ex) - { - } + assertFalse(ClassUtils.setFieldValue(myClass, "doesNotExist", null)); } @Test