From 26dc266633c55fc7d4e04b7a6cb83d883a390484 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Mon, 25 Feb 2008 21:24:03 +0000 Subject: [PATCH] change: - 'ClassUtils.setFieldValue' no longer throws an exception but prefers to return a boolean. SVN: 4479 --- .../ch/systemsx/cisd/common/utilities/ClassUtils.java | 11 +++++------ .../cisd/common/utilities/ClassUtilsTest.java | 9 ++------- 2 files changed, 7 insertions(+), 13 deletions(-) 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 9312922d319..df1facee4ed 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 a04f840e21d..44ca0f90ef5 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 -- GitLab