@Test public void shouldInvokeFallbackOnMethodChain() { Class<?>[] classes1 = { A.class, A.class }; String[] chain = { "current", "abc" }; Object o = ReflectionUtils.invokeMethodChain( classes1, chain, 5L ); assertThat( o ) .isEqualTo( 5L ); Class<?>[] classes2 = { A.class, B.class, B.class }; String[] longChain = { "current", "createB", "abc" }; o = ReflectionUtils.invokeMethodChain( classes2, longChain, 6L ); assertThat( o ) .isEqualTo( 6L ); }
public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ) { if ( classesChain.length != noArgMethodNames.length ) { throw new IllegalArgumentException( "arrays must have the same length" ); } Object obj = null; try { for ( int i = 0, len = noArgMethodNames.length; i < len; i++ ) { if ( i == 0 ) { obj = invokeStaticMethod( classesChain[i], noArgMethodNames[i], EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY ); } else { Method method = getMethod( classesChain[i], noArgMethodNames[i] ); obj = invokeMethodWithArray( obj, method ); } } return obj; } catch ( RuntimeException e ) { return fallback; } }
ReflectionUtils { public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ) { if ( classesChain.length != noArgMethodNames.length ) { throw new IllegalArgumentException( "arrays must have the same length" ); } Object obj = null; try { for ( int i = 0, len = noArgMethodNames.length; i < len; i++ ) { if ( i == 0 ) { obj = invokeStaticMethod( classesChain[i], noArgMethodNames[i], EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY ); } else { Method method = getMethod( classesChain[i], noArgMethodNames[i] ); obj = invokeMethodWithArray( obj, method ); } } return obj; } catch ( RuntimeException e ) { return fallback; } } }
ReflectionUtils { public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ) { if ( classesChain.length != noArgMethodNames.length ) { throw new IllegalArgumentException( "arrays must have the same length" ); } Object obj = null; try { for ( int i = 0, len = noArgMethodNames.length; i < len; i++ ) { if ( i == 0 ) { obj = invokeStaticMethod( classesChain[i], noArgMethodNames[i], EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY ); } else { Method method = getMethod( classesChain[i], noArgMethodNames[i] ); obj = invokeMethodWithArray( obj, method ); } } return obj; } catch ( RuntimeException e ) { return fallback; } } private  ReflectionUtils(); }
ReflectionUtils { public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ) { if ( classesChain.length != noArgMethodNames.length ) { throw new IllegalArgumentException( "arrays must have the same length" ); } Object obj = null; try { for ( int i = 0, len = noArgMethodNames.length; i < len; i++ ) { if ( i == 0 ) { obj = invokeStaticMethod( classesChain[i], noArgMethodNames[i], EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY ); } else { Method method = getMethod( classesChain[i], noArgMethodNames[i] ); obj = invokeMethodWithArray( obj, method ); } } return obj; } catch ( RuntimeException e ) { return fallback; } } private  ReflectionUtils(); static Method getMethod( Object instance, String methodName, Class<?>... parameters ); static Method getMethod( Class<?> clazz, String methodName, Class<?>... parameters ); static Method tryGetMethod( Class<?> clazz, String methodName, Class<?>... parameters ); static Object invokeGetter( Object instance, String methodName ); static Object invokeGetter( Class<?> instanceType, Object instance, String methodName ); static Constructor<?> getConstructor( Class<?> clazz, Class<?>... arguments ); static Object newInstance( Constructor<?> constructor, Object... params ); static T instantiate( ClassLoader classLoader, String classname, Class<T> returnType ); static Object instantiateOneArg( ClassLoader classLoader, String className, Class<?> param1Class,
                                            Object param1 ); static void invokeSetter( Object o, String name, Class<?> value1clazz, Object value ); static Object invokeSetter( Object target, Method method, Object value ); static Object invokeMethodWithArray( Object target, Method method, Object... args ); static Object invokeMethodWithArray2( Object target, Method method, Object... args ); static Object instantiateObject( String className, Class<?>[] types, Object[] params, ClassLoader cl ); @SuppressWarnings( "checkstyle:emptyblock" ) static Class<?> tryLoadClass( ClassLoader classLoader, String className ); static Class<?> loadClass( ClassLoader classLoader, String className ); static Class<?> reloadClass( ClassLoader classLoader, Object source ); static Object invokeStaticMethod( Class<?> clazz, String methodName,
                                             Class<?>[] parameterTypes, Object[] parameters ); static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ); }
ReflectionUtils { public static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback ) { if ( classesChain.length != noArgMethodNames.length ) { throw new IllegalArgumentException( "arrays must have the same length" ); } Object obj = null; try { for ( int i = 0, len = noArgMethodNames.length; i < len; i++ ) { if ( i == 0 ) { obj = invokeStaticMethod( classesChain[i], noArgMethodNames[i], EMPTY_CLASS_ARRAY, EMPTY_OBJECT_ARRAY ); } else { Method method = getMethod( classesChain[i], noArgMethodNames[i] ); obj = invokeMethodWithArray( obj, method ); } } return obj; } catch ( RuntimeException e ) { return fallback; } } private  ReflectionUtils(); static Method getMethod( Object instance, String methodName, Class<?>... parameters ); static Method getMethod( Class<?> clazz, String methodName, Class<?>... parameters ); static Method tryGetMethod( Class<?> clazz, String methodName, Class<?>... parameters ); static Object invokeGetter( Object instance, String methodName ); static Object invokeGetter( Class<?> instanceType, Object instance, String methodName ); static Constructor<?> getConstructor( Class<?> clazz, Class<?>... arguments ); static Object newInstance( Constructor<?> constructor, Object... params ); static T instantiate( ClassLoader classLoader, String classname, Class<T> returnType ); static Object instantiateOneArg( ClassLoader classLoader, String className, Class<?> param1Class,
                                            Object param1 ); static void invokeSetter( Object o, String name, Class<?> value1clazz, Object value ); static Object invokeSetter( Object target, Method method, Object value ); static Object invokeMethodWithArray( Object target, Method method, Object... args ); static Object invokeMethodWithArray2( Object target, Method method, Object... args ); static Object instantiateObject( String className, Class<?>[] types, Object[] params, ClassLoader cl ); @SuppressWarnings( "checkstyle:emptyblock" ) static Class<?> tryLoadClass( ClassLoader classLoader, String className ); static Class<?> loadClass( ClassLoader classLoader, String className ); static Class<?> reloadClass( ClassLoader classLoader, Object source ); static Object invokeStaticMethod( Class<?> clazz, String methodName,
                                             Class<?>[] parameterTypes, Object[] parameters ); static Object invokeMethodChain( Class<?>[] classesChain, String[] noArgMethodNames, Object fallback );  }