ReflectionUtils { public static void setProperty( Object object, String name, String value ) { Object[] arguments = new Object[]{ value }; Class<?>[] parameterTypes = new Class<?>[]{ String.class }; if ( name.length() > 0 ) { name = "set" + name.substring( 0, 1 ).toUpperCase() + name.substring( 1, name.length() ); try { Method concatMethod = object.getClass().getMethod( name, parameterTypes ); concatMethod.invoke( object, arguments ); } catch ( Exception ex ) { throw new UnsupportedOperationException( "Failed to set property", ex ); } } }  static long getId( Object object ); static String getProperty( Object object, String property ); static void setProperty( Object object, String name, String value ); static void setProperty( Object object, String namePrefix, String name, String value ); static String getClassName( Object object ); static boolean isCollection( Object value ); static boolean isCollection( String fieldName, Object object, Class<?> type ); static boolean isCollection( String fieldName, Object object, Class<?> type, Class<? extends Annotation> annotation ); static Method findGetterMethod( String fieldName, Object target ); static Method findGetterMethod( String fieldName, Class<?> clazz ); static T invokeGetterMethod( String fieldName, Object target ); static Method findSetterMethod( String fieldName, Object target ); static T invokeSetterMethod( String fieldName, Object target, Object... args ); static boolean isType( Field field, Class<?> clazz ); static List<Field> getAllFields( Class<?> clazz ); static List<String> getAllFieldNames( Class<?> klass ); @SuppressWarnings( "unchecked" ) static T invokeMethod( Object target, Method method, Object... args ); static Collection<Field> collectFields( Class<?> clazz, Predicate<Field> predicate ); static Collection<E> newCollectionInstance( Class<?> clazz ); static Class<?> getRealClass( Class<?> klass ); static List<Method> getMethods( Class<?> klass ); static Multimap<String, Method> getMethodsMultimap( Class<?> klass ); static boolean isSharingProperty( Property property ); static boolean isTranslationProperty( Property property ); static final List<String> SHARING_PROPS; }
@Test public void testSetProperty() { setProperty( dataElementA, "shortName", "ShortNameA" ); assertEquals( "ShortNameA", dataElementA.getShortName() ); }
@Test( expected = UnsupportedOperationException.class ) public void testSetPropertyException() { setProperty( dataElementA, "color", "Blue" ); }