/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
@Test public void testUnbridgeFoo() throws NoSuchMethodException, IOException, ClassNotFoundException { // given Method bridgeMethod = getBridgeMethod(GenericInterfaceImpl.class, "foo"); assertNotNull(bridgeMethod); // when Method genMethod = MethodProvider.getInstance().unbride(bridgeMethod, GenericInterfaceImpl.class); // then assertNotNull(bridgeMethod); assertReturnType(Child.class, genMethod); assertParamsTypes(genMethod, Child.class); }
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if(method.isBridge()){ method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }