/** * Get all methods in the supplied {@link Class class} and its superclasses * which are annotated with the supplied {@code annotationType} but * which are not <em>shadowed</em> by methods overridden in subclasses. * <p>Default methods on interfaces are also detected. * @param clazz the class for which to retrieve the annotated methods * @param annotationType the annotation type for which to search * @return all annotated methods in the supplied class and its superclasses * as well as annotated interface default methods */ private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) { return Arrays.stream(ReflectionUtils.getUniqueDeclaredMethods(clazz)) .filter(method -> AnnotatedElementUtils.hasAnnotation(method, annotationType)) .collect(Collectors.toList()); }
@Test public void getUniqueDeclaredMethods() throws Exception { class Foo { @Override public String toString() { return super.toString(); } } int toStringMethodCount = 0; for (Method method : ReflectionUtils.getUniqueDeclaredMethods(Foo.class)) { if (method.getName().equals("toString")) { toStringMethodCount++; } } assertThat(toStringMethodCount, is(1)); }
@Test public void getUniqueDeclaredMethods_withCovariantReturnType_andCglibRewrittenMethodNames() throws Exception { Class<?> cglibLeaf = new ConfigurationClassEnhancer().enhance(Leaf.class, null); int m1MethodCount = 0; Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(cglibLeaf); for (Method method : methods) { if (method.getName().equals("m1")) { m1MethodCount++; } } assertThat(m1MethodCount, is(1)); for (Method method : methods) { if (method.getName().contains("m1")) { assertEquals(method.getReturnType(), Integer.class); } } }
@Test public void getUniqueDeclaredMethods_withCovariantReturnType() throws Exception { class Parent { @SuppressWarnings("unused") public Number m1() { return Integer.valueOf(42); } } class Leaf extends Parent { @Override public Integer m1() { return Integer.valueOf(42); } } int m1MethodCount = 0; Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(Leaf.class); for (Method method : methods) { if (method.getName().equals("m1")) { m1MethodCount++; } } assertThat(m1MethodCount, is(1)); assertTrue(ObjectUtils.containsElement(methods, Leaf.class.getMethod("m1"))); assertFalse(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1"))); }
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(C.class); sw.stop(); long totalMs = sw.getTotalTimeMillis();
Method[] candidates = ReflectionUtils.getUniqueDeclaredMethods(factoryClass); Set<Class> returnTypes = new HashSet<Class>(1); for (Method factoryMethod : candidates) {
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(proxyInterface); for (Method method : methods) { MethodInvocationGateway gateway = this.createGatewayForMethod(method);
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { Class<?> beanType = ProxyUtils.getUserClass(bean); RepositoryEventHandler typeAnno = AnnotationUtils.findAnnotation(beanType, RepositoryEventHandler.class); if (typeAnno == null) { return bean; } for (Method method : ReflectionUtils.getUniqueDeclaredMethods(beanType)) { inspect(bean, method, HandleBeforeCreate.class, BeforeCreateEvent.class); inspect(bean, method, HandleAfterCreate.class, AfterCreateEvent.class); inspect(bean, method, HandleBeforeSave.class, BeforeSaveEvent.class); inspect(bean, method, HandleAfterSave.class, AfterSaveEvent.class); inspect(bean, method, HandleBeforeLinkSave.class, BeforeLinkSaveEvent.class); inspect(bean, method, HandleAfterLinkSave.class, AfterLinkSaveEvent.class); inspect(bean, method, HandleBeforeDelete.class, BeforeDeleteEvent.class); inspect(bean, method, HandleAfterDelete.class, AfterDeleteEvent.class); inspect(bean, method, HandleBeforeLinkDelete.class, BeforeLinkDeleteEvent.class); inspect(bean, method, HandleAfterLinkDelete.class, AfterLinkDeleteEvent.class); } return bean; }
/** * Get all uniquely declared methods on a given Class, if methods are overriden only the topmost method is returned. * * @param klass Class * @return List of uniquely declared methods */ public static List<Method> getMethods( Class<?> klass ) { return Arrays.asList( org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods( klass ) ); }
public Collection<Method> detectHandlerMethods( Object value ) { List<Method> methods = new LinkedList<>(); if ( value != null ) { Method[] declaredMethods = ReflectionUtils.getUniqueDeclaredMethods( value.getClass() ); for ( Method method : declaredMethods ) { if ( AnnotationUtils.findAnnotation( method, Event.class ) != null ) { methods.add( method ); } } } return methods; }
/** * Get all methods in the supplied {@link Class class} and its superclasses * which are annotated with the supplied {@code annotationType} but * which are not <em>shadowed</em> by methods overridden in subclasses. * <p>Default methods on interfaces are also detected. * @param clazz the class for which to retrieve the annotated methods * @param annotationType the annotation type for which to search * @return all annotated methods in the supplied class and its superclasses * as well as annotated interface default methods */ private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) { return Arrays.stream(ReflectionUtils.getUniqueDeclaredMethods(clazz)) .filter(method -> AnnotatedElementUtils.hasAnnotation(method, annotationType)) .collect(Collectors.toList()); }
/** * Returns the list of all methods annotated with {@link com.foreach.across.core.annotations.InstallerMethod} * without method parameters. Methods can be ordered using {@link org.springframework.core.annotation.Order}. * * @return list of installer methods */ public Method[] getInstallerMethods() { List<Method> methods = new ArrayList<>(); for ( Method method : ReflectionUtils.getUniqueDeclaredMethods( installerClass ) ) { if ( AnnotationUtils.findAnnotation( method, InstallerMethod.class ) != null ) { methods.add( method ); } } AnnotationAwareOrderComparator.sort( methods ); return methods.toArray( new Method[0] ); }
/** * Get all methods in the supplied {@link Class class} and its superclasses * which are annotated with the supplied {@code annotationType} but * which are not <em>shadowed</em> by methods overridden in subclasses. * <p>Default methods on interfaces are also detected. * @param clazz the class for which to retrieve the annotated methods * @param annotationType the annotation type for which to search * @return all annotated methods in the supplied class and its superclasses * as well as annotated interface default methods */ private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) { List<Method> methods = new ArrayList<Method>(4); for (Method method : ReflectionUtils.getUniqueDeclaredMethods(clazz)) { if (AnnotationUtils.getAnnotation(method, annotationType) != null) { methods.add(method); } } return methods; }
@SuppressWarnings("unchecked") @EventListener public void dashboardEventListener(DashboardEvent dashboardEvent) throws InvocationTargetException, IllegalAccessException { refreshWidgets(dashboardEvent); if (dashboardViewAssistant == null) { return; } Class eventClass = dashboardEvent.getClass(); Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(dashboardViewAssistant.getClass()); List<Method> eventListenerMethods = Arrays.stream(methods) .filter(m -> m.getAnnotation(EventListener.class) != null) .filter(m -> m.getParameterCount() == 1) .collect(Collectors.toList()); for (Method method : eventListenerMethods) { java.lang.reflect.Parameter[] parameters = method.getParameters(); java.lang.reflect.Parameter parameter = parameters[0]; Class methodEventTypeArg = parameter.getType(); if (methodEventTypeArg.isAssignableFrom(eventClass)) { method.invoke(dashboardViewAssistant, dashboardEvent); } } }
@VisibleForTesting void refresh() { final Map<String, Object> handlerBeanMap = applicationContext.getBeansWithAnnotation(LineMessageHandler.class); final List<HandlerMethod> collect = handlerBeanMap .values().stream() .flatMap((Object bean) -> { final Method[] uniqueDeclaredMethods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass()); return Arrays.stream(uniqueDeclaredMethods) .map(method -> getMethodHandlerMethodFunction(bean, method)) .filter(Objects::nonNull); }) .sorted(HANDLER_METHOD_PRIORITY_COMPARATOR) .collect(Collectors.toList()); log.info("Registered LINE Messaging API event handler: count = {}", collect.size()); collect.forEach(item -> log.info("Mapped \"{}\" onto {}", item.getSupportType(), item.getHandler().toGenericString())); eventConsumerList = collect; }
@Override public final Object postProcessAfterInitialization(Object bean, final String beanName) throws BeansException { Class<?> targetClass = AopUtils.isAopProxy(bean) ? AopUtils.getTargetClass(bean) : bean.getClass(); Method[] uniqueDeclaredMethods = ReflectionUtils.getUniqueDeclaredMethods(targetClass); for (Method method : uniqueDeclaredMethods) { StreamListener streamListener = AnnotatedElementUtils.findMergedAnnotation(method, StreamListener.class); if (streamListener != null && !method.isBridge()) { streamListenerCallbacks.add(() -> { Assert.isTrue(method.getAnnotation(Input.class) == null, StreamListenerErrorMessages.INPUT_AT_STREAM_LISTENER); this.doPostProcess(streamListener, method, bean); }); } } return bean; }
@VisibleForTesting void refresh() { final Map<String, Object> handlerBeanMap = applicationContext.getBeansWithAnnotation(LineMessageHandler.class); final List<HandlerMethod> collect = handlerBeanMap .values().stream() .flatMap((Object bean) -> { final Method[] uniqueDeclaredMethods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass()); return Arrays.stream(uniqueDeclaredMethods) .map(method -> getMethodHandlerMethodFunction(bean, method)) .filter(Objects::nonNull); }) .sorted(HANDLER_METHOD_PRIORITY_COMPARATOR) .collect(Collectors.toList()); log.info("Registered LINE Messaging API event handler: count = {}", collect.size()); collect.forEach(item -> log.info("Mapped \"{}\" onto {}", item.getSupportType(), item.getHandler().toGenericString())); eventConsumerList = collect; }
protected TargetIntrospectionData getTargetIntrospectionDataNotCached(Class<?> concreteClass) { Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(concreteClass); Map<Class, MethodHandle> addListenerMethods = getAddListenerMethodsNotCached(concreteClass, methods); Map<String, MethodHandle> installTargetMethods = getInstallTargetMethodsNotCached(concreteClass, methods); return new TargetIntrospectionData(addListenerMethods, installTargetMethods); }
protected ScreenIntrospectionData getScreenIntrospectionDataNotCached(Class<?> concreteClass) { Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(concreteClass); List<InjectElement> injectElements = getAnnotatedInjectElementsNotCached(concreteClass); List<AnnotatedMethod<Subscribe>> subscribeMethods = getAnnotatedSubscribeMethodsNotCached(concreteClass, methods); List<AnnotatedMethod<Install>> installMethods = getAnnotatedInstallMethodsNotCached(concreteClass, methods); List<Method> eventListenerMethods = getAnnotatedListenerMethodsNotCached(concreteClass, methods); return new ScreenIntrospectionData(injectElements, eventListenerMethods, subscribeMethods, installMethods); }
Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(proxyInterface); for (Method method : methods) { MethodInvocationGateway gateway = this.createGatewayForMethod(method);