/** * Perform the given callback operation on all matching methods of the given * class and superclasses. * <p>The same named method occurring on subclass and superclass will appear * twice, unless excluded by a {@link MethodFilter}. * @param clazz the class to introspect * @param mc the callback to invoke for each method * @throws IllegalStateException if introspection fails * @see #doWithMethods(Class, MethodCallback, MethodFilter) */ public static void doWithMethods(Class<?> clazz, MethodCallback mc) { doWithMethods(clazz, mc, null); }
/** * Get all declared methods on the leaf class and all superclasses. * Leaf class methods are included first. * @param leafClass the class to introspect * @throws IllegalStateException if introspection fails */ public static Method[] getAllDeclaredMethods(Class<?> leafClass) { final List<Method> methods = new ArrayList<>(32); doWithMethods(leafClass, methods::add); return methods.toArray(new Method[0]); }
/** * Perform the given callback operation on all matching methods of the given * class and superclasses. * <p>The same named method occurring on subclass and superclass will appear * twice, unless excluded by a {@link MethodFilter}. * @param clazz the class to introspect * @param mc the callback to invoke for each method * @throws IllegalStateException if introspection fails * @see #doWithMethods(Class, MethodCallback, MethodFilter) */ public static void doWithMethods(Class<?> clazz, MethodCallback mc) { doWithMethods(clazz, mc, null); }
private List<Method> findAllMethod(Class clazz) { final List<Method> res = new LinkedList<>(); ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { res.add(method); } }); return res; } }
private List<Method> getAdvisorMethods(Class<?> aspectClass) { final List<Method> methods = new ArrayList<>(); ReflectionUtils.doWithMethods(aspectClass, method -> { // Exclude pointcuts if (AnnotationUtils.getAnnotation(method, Pointcut.class) == null) { methods.add(method); } }); methods.sort(METHOD_COMPARATOR); return methods; }
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
/** * Get all declared methods on the leaf class and all superclasses. * Leaf class methods are included first. * @param leafClass the class to introspect * @throws IllegalStateException if introspection fails */ public static Method[] getAllDeclaredMethods(Class<?> leafClass) { final List<Method> methods = new ArrayList<>(32); doWithMethods(leafClass, methods::add); return methods.toArray(new Method[0]); }
doWithMethods(leafClass, method -> { boolean knownSignature = false; Method methodBeingOverriddenWithCovariantReturnType = null;
doWithMethods(clazz.getSuperclass(), mc, mf); doWithMethods(superIfc, mc, mf);
private boolean hasQueryHandlerMethod(Class<?> beanClass) { AtomicBoolean result = new AtomicBoolean(false); ReflectionUtils.doWithMethods(beanClass, new HasQueryHandlerAnnotationMethodCallback(result)); return result.get(); }
private boolean hasCommandHandlerMethod(Class<?> beanClass) { final AtomicBoolean result = new AtomicBoolean(false); ReflectionUtils.doWithMethods(beanClass, new HasCommandHandlerAnnotationMethodCallback(result)); return result.get(); }
ReflectionUtils.doWithMethods(fbClass, method -> { if (method.getName().equals(factoryMethodName) && FactoryBean.class.isAssignableFrom(method.getReturnType())) {
final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType); ReflectionUtils.doWithMethods(currentHandlerType, method -> { Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass); T result = metadataLookup.inspect(specificMethod);
private void testSupportsStandardClasses() throws Exception { final StandardClasses standardClasses = new StandardClasses(); ReflectionUtils.doWithMethods(StandardClasses.class, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(standardClasses); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method method) { return method.getName().startsWith("standardClass"); } }); }
private void testSupportsPrimitives() { final Primitives primitives = new Primitives(); ReflectionUtils.doWithMethods(Primitives.class, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Type returnType = method.getGenericReturnType(); assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">", marshaller.supports(returnType)); try { // make sure the marshalling does not result in errors Object returnValue = method.invoke(primitives); marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream())); } catch (InvocationTargetException e) { fail(e.getMessage()); } } }, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method method) { return method.getName().startsWith("primitive"); } }); }
@Test public void duplicatesFound() { ListSavingMethodCallback mc = new ListSavingMethodCallback(); ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc); int absquatulateCount = 0; for (String name : mc.getMethodNames()) { if (name.equals("absquatulate")) { ++absquatulateCount; } } assertEquals("Found 2 absquatulates", 2, absquatulateCount); }
@Test public void doWithProtectedMethods() { ListSavingMethodCallback mc = new ListSavingMethodCallback(); ReflectionUtils.doWithMethods(TestObject.class, mc, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method m) { return Modifier.isProtected(m.getModifiers()); } }); assertFalse(mc.getMethodNames().isEmpty()); assertTrue("Must find protected method on Object", mc.getMethodNames().contains("clone")); assertTrue("Must find protected method on Object", mc.getMethodNames().contains("finalize")); assertFalse("Public, not protected", mc.getMethodNames().contains("hashCode")); assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate")); }