Example usage for org.springframework.util ReflectionUtils doWithMethods

List of usage examples for org.springframework.util ReflectionUtils doWithMethods

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils doWithMethods.

Prototype

public static void doWithMethods(Class<?> clazz, MethodCallback mc) 

Source Link

Document

Perform the given callback operation on all matching methods of the given class and superclasses.

Usage

From source file:tnt.common.monitoring.GuardedByAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    final List<Method> guardedMethods = new ArrayList<>();
    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {
        @Override/*from   w w w .  j av  a  2  s  .  c  om*/
        public void doWith(Method method) {
            if (method.isAnnotationPresent(GuardedBy.class)) {
                guardedMethods.add(method);
            }
        }
    });
    return guardedMethods.isEmpty() ? bean : createProxy(bean, guardedMethods);
}

From source file:org.jdal.annotation.AnnotatedElementAccessor.java

/**
 * Find annotated elements on types/*from  w ww . j  a v  a  2s. c  o  m*/
 * @param ann annotation to search
 * @param clazz class to search on.
 * @return List with annotated elements
 */
public static List<AnnotatedElement> findAnnotatedElements(final Class<? extends Annotation> annotationType,
        Class<?> clazz) {
    final ArrayList<AnnotatedElement> elements = new ArrayList<AnnotatedElement>();
    // Lookup fields
    ReflectionUtils.doWithFields(clazz, new FieldCallback() {

        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (field.getAnnotation(annotationType) != null) {
                elements.add(field);
            }

        }
    });
    // Lookup methods
    ReflectionUtils.doWithMethods(clazz, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {

            if (org.springframework.core.annotation.AnnotationUtils.getAnnotation(method,
                    annotationType) != null)
                elements.add(method);
        }
    });

    return elements;
}

From source file:com.googlecode.ehcache.annotations.key.SimpleReflectionHelper.java

public boolean implementsEquals(Object element) {
    final MutableBoolean found = new MutableBoolean();

    ReflectionUtils.doWithMethods(element.getClass(), new MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (found.value || method.getDeclaringClass() == Object.class) {
                return;
            }/*from   ww  w.  j a  v  a 2  s. c om*/

            if (ReflectionUtils.isEqualsMethod(method)) {
                found.value = true;
            }
        }
    });

    return found.value;
}

From source file:org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBHashAndRangeKeyMethodExtractorImpl.java

/**
 * Creates a new {@link DynamoDBHashAndRangeKeyMethodExtractor} for the given domain type.
 *
 * @param idType//from w  ww. j a va 2s. c om
 *            must not be {@literal null}.
 */
public DynamoDBHashAndRangeKeyMethodExtractorImpl(final Class<T> idType) {

    Assert.notNull(idType, "Id type must not be null!");
    this.idType = idType;
    ReflectionUtils.doWithMethods(idType, new MethodCallback() {
        @Override
        public void doWith(Method method) {
            if (method.getAnnotation(DynamoDBHashKey.class) != null) {
                Assert.isNull(hashKeyMethod,
                        "Multiple methods annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
                ReflectionUtils.makeAccessible(method);
                hashKeyMethod = method;
            }
        }
    });
    ReflectionUtils.doWithFields(idType, new FieldCallback() {
        @Override
        public void doWith(Field field) {
            if (field.getAnnotation(DynamoDBHashKey.class) != null) {
                Assert.isNull(hashKeyField,
                        "Multiple fields annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
                ReflectionUtils.makeAccessible(field);

                hashKeyField = field;
            }
        }
    });
    ReflectionUtils.doWithMethods(idType, new MethodCallback() {
        @Override
        public void doWith(Method method) {
            if (method.getAnnotation(DynamoDBRangeKey.class) != null) {
                Assert.isNull(rangeKeyMethod, "Multiple methods annotated by @DynamoDBRangeKey within type "
                        + idType.getName() + "!");
                ReflectionUtils.makeAccessible(method);
                rangeKeyMethod = method;
            }
        }
    });
    ReflectionUtils.doWithFields(idType, new FieldCallback() {
        @Override
        public void doWith(Field field) {
            if (field.getAnnotation(DynamoDBRangeKey.class) != null) {
                Assert.isNull(rangeKeyField,
                        "Multiple fields annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
                ReflectionUtils.makeAccessible(field);
                rangeKeyField = field;
            }
        }
    });
    if (hashKeyMethod == null && hashKeyField == null) {
        throw new IllegalArgumentException(
                "No method or field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
    }
    if (rangeKeyMethod == null && rangeKeyField == null) {
        throw new IllegalArgumentException(
                "No method or field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
    }
    if (hashKeyMethod != null && hashKeyField != null) {
        throw new IllegalArgumentException(
                "Both method and field annotated by @DynamoDBHashKey within type " + idType.getName() + "!");
    }
    if (rangeKeyMethod != null && rangeKeyField != null) {
        throw new IllegalArgumentException(
                "Both method and field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!");
    }
}

From source file:org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java

public DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl(final Class<T> domainType) {
    super(domainType);
    this.hashAndRangeKeyMethodExtractor = new DynamoDBHashAndRangeKeyMethodExtractorImpl<T>(getJavaType());
    ReflectionUtils.doWithMethods(domainType, new MethodCallback() {
        public void doWith(Method method) {
            if (method.getAnnotation(DynamoDBHashKey.class) != null) {
                String setterMethodName = toSetterMethodNameFromAccessorMethod(method);
                if (setterMethodName != null) {
                    hashKeySetterMethod = ReflectionUtils.findMethod(domainType, setterMethodName,
                            method.getReturnType());
                }/*from w  w  w . j  a  v  a 2s .  c  om*/
            }
        }
    });
    ReflectionUtils.doWithFields(domainType, new FieldCallback() {
        public void doWith(Field field) {
            if (field.getAnnotation(DynamoDBHashKey.class) != null) {

                hashKeyField = ReflectionUtils.findField(domainType, field.getName());

            }
        }
    });
    Assert.isTrue(hashKeySetterMethod != null || hashKeyField != null,
            "Unable to find hash key field or setter method on " + domainType + "!");
    Assert.isTrue(hashKeySetterMethod == null || hashKeyField == null,
            "Found both hash key field and setter method on " + domainType + "!");

}

From source file:demo.lifecycle.SmartLifecycleRegistry.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override//from w w w  .jav  a2s .c  om
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Start start = AnnotationUtils.getAnnotation(method, Start.class);
            if (start != null) {
                processStart(beanName, start, method, bean);
            }
        }

    });
    return bean;
}

From source file:com.googlecode.ehcache.annotations.key.SimpleReflectionHelper.java

public boolean implementsHashCode(Object element) {
    final MutableBoolean found = new MutableBoolean();

    ReflectionUtils.doWithMethods(element.getClass(), new MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (found.value || method.getDeclaringClass() == Object.class) {
                return;
            }/*  w  ww.  ja  va 2  s .c om*/

            if (ReflectionUtils.isHashCodeMethod(method)) {
                found.value = true;
            }
        }
    });

    return found.value;
}

From source file:com.excilys.spring.mom.annotation.MOMAnnotationProcessing.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    final Class<?> clazz = bean.getClass();
    MOMController classAnnotation = clazz.getAnnotation(MOMController.class);

    // If the bean is annotated with @MOMController
    if (classAnnotation != null) {
        LOGGER.debug("Found @MOMController annotated class : {}", clazz);

        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                MOMMapping methodAnnotation = method.getAnnotation(MOMMapping.class);

                // If the method is annotated with @MOMMapping
                if (methodAnnotation != null) {
                    String topic = resolveProperty(methodAnnotation.topic());
                    MOMMappingConsum consum = methodAnnotation.consumes();

                    LOGGER.debug("Configuring @MOMMapping({}) method {}", consum, method);

                    try {
                        momClient.subscribe(topic, new MOMMethodHandler(method, bean, consum));
                    } catch (NotConnectedException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    } catch (SocketException e) {
                        LOGGER.error("Can't subscribe to topic {}", topic, e);
                    }//from ww w. java2s . com
                }
            }
        });
    }

    return bean;
}

From source file:cn.wanghaomiao.seimi.struct.CrawlerModel.java

private void init() {
    Crawler c = clazz.getAnnotation(Crawler.class);
    Assert.notNull(c, StrFormatUtil.info("crawler {} lost annotation @cn.wanghaomiao.seimi.annotation.Crawler!",
            clazz.getName()));// w w  w  . j a v  a 2  s  .  co m
    this.queueClass = c.queue();
    this.queueInstance = context.getBean(queueClass);
    Assert.notNull(queueInstance,
            StrFormatUtil.info("can not get {} instance,please check scan path", queueClass));
    instance.setQueue(queueInstance);
    memberMethods = new HashMap<>();
    ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            memberMethods.put(method.getName(), method);
        }
    });
    this.crawlerName = StringUtils.isNoneBlank(c.name()) ? c.name() : clazz.getSimpleName();
    instance.setCrawlerName(this.crawlerName);
    resolveProxy(c.proxy());
    this.useCookie = c.useCookie();
    this.delay = c.delay();
    this.useUnrepeated = c.useUnrepeated();
    logger.info("Crawler[{}] init complete.", crawlerName);
}

From source file:com.googlecode.ehcache.annotations.key.SimpleReflectionHelper.java

public boolean implementsToString(Object element) {
    final MutableBoolean found = new MutableBoolean();

    ReflectionUtils.doWithMethods(element.getClass(), new MethodCallback() {
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (found.value || method.getDeclaringClass() == Object.class) {
                return;
            }/*from   w ww. j  a  v a2  s . c  om*/

            if (ReflectionUtils.isToStringMethod(method)) {
                found.value = true;
            }
        }
    });

    return found.value;
}