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, @Nullable MethodFilter mf) 

Source Link

Document

Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

Usage

From source file:com.ryantenney.metrics.spring.AbstractMetricMethodInterceptor.java

AbstractMetricMethodInterceptor(final MetricRegistry metricRegistry, final Class<?> targetClass,
        final Class<A> annotationClass, final MethodFilter methodFilter) {
    this.metricRegistry = metricRegistry;
    this.targetClass = targetClass;
    this.annotationClass = annotationClass;
    this.metrics = new HashMap<MethodKey, AnnotationMetricPair<A, M>>();

    LOG.debug("Creating method interceptor for class {}", targetClass.getCanonicalName());
    LOG.debug("Scanning for @{} annotated methods", annotationClass.getSimpleName());

    ReflectionUtils.doWithMethods(targetClass, this, methodFilter);
}

From source file:com.ryantenney.metrics.spring.MeteredMethodInterceptor.java

public MeteredMethodInterceptor(final MetricRegistry metrics, final Class<?> targetClass) {
    this.metrics = metrics;
    this.targetClass = targetClass;
    this.meters = new HashMap<MethodKey, Meter>();

    LOG.debug("Creating method interceptor for class {}", targetClass.getCanonicalName());
    LOG.debug("Scanning for @Metered annotated methods");

    ReflectionUtils.doWithMethods(targetClass, this, METHOD_FILTER);
}

From source file:com.corundumstudio.socketio.annotation.SpringAnnotationScanner.java

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    final AtomicBoolean add = new AtomicBoolean();
    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {
        @Override//from ww w .j  a  va2  s. c o m
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            add.set(true);
        }
    }, new MethodFilter() {
        @Override
        public boolean matches(Method method) {
            for (Class<? extends Annotation> annotationClass : annotations) {
                if (method.isAnnotationPresent(annotationClass)) {
                    return true;
                }
            }
            return false;
        }
    });

    if (add.get()) {
        originalBeanClass = bean.getClass();
    }
    return bean;
}

From source file:com.ryantenney.metrics.spring.TimedMethodInterceptor.java

public TimedMethodInterceptor(final MetricRegistry metrics, final Class<?> targetClass) {
    this.metrics = metrics;
    this.targetClass = targetClass;
    this.timers = new HashMap<MethodKey, Timer>();

    LOG.debug("Creating method interceptor for class {}", targetClass.getCanonicalName());
    LOG.debug("Scanning for @Timed annotated methods");

    ReflectionUtils.doWithMethods(targetClass, this, METHOD_FILTER);
}

From source file:com.ryantenney.metrics.spring.ExceptionMeteredMethodInterceptor.java

public ExceptionMeteredMethodInterceptor(final MetricRegistry metrics, final Class<?> targetClass) {
    this.metrics = metrics;
    this.targetClass = targetClass;
    this.meters = new HashMap<MethodKey, ExceptionMeter>();

    LOG.debug("Creating method interceptor for class {}", targetClass.getCanonicalName());
    LOG.debug("Scanning for @ExceptionMetered annotated methods");

    ReflectionUtils.doWithMethods(targetClass, this, METHOD_FILTER);
}

From source file:com.asual.summer.core.util.ClassUtils.java

public static Object invokeMethod(Object target, final String methodName, final Object[] parameters) {
    if (target != null) {
        final List<Method> matches = new ArrayList<Method>();
        ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                matches.add(method);//from   www . j av a 2  s.  c  om
            }
        }, new ReflectionUtils.MethodFilter() {
            public boolean matches(Method method) {
                if (method.getName().equals(methodName)) {
                    Class<?>[] types = method.getParameterTypes();
                    if (parameters == null && types.length == 0) {
                        return true;
                    }
                    if (types.length != parameters.length) {
                        return false;
                    }
                    for (int i = 0; i < types.length; i++) {
                        if (!types[i].isInstance(parameters[i])) {
                            return false;
                        }
                    }
                    return true;
                }
                return false;
            }
        });
        if (matches.size() > 0) {
            if (parameters == null) {
                return ReflectionUtils.invokeMethod(matches.get(0), target);
            } else {
                return ReflectionUtils.invokeMethod(matches.get(0), target, parameters);
            }
        }
    }
    return null;
}

From source file:com.ace.erp.common.inject.support.InjectBaseDependencyHelper.java

/**
 * ??//w ww. j a  v  a 2 s .c om
 *
 * @param target
 * @param annotation
 */
private static Set<Object> findDependencies(final Object target, final Class<? extends Annotation> annotation) {

    final Set<Object> candidates = Sets.newHashSet();

    ReflectionUtils.doWithFields(target.getClass(), new ReflectionUtils.FieldCallback() {
        @Override
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(field);
            Object obj = ReflectionUtils.getField(field, target);
            candidates.add(obj);
        }
    }, new ReflectionUtils.FieldFilter() {
        @Override
        public boolean matches(Field field) {
            return field.isAnnotationPresent(annotation);
        }
    });

    ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            ReflectionUtils.makeAccessible(method);
            PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
            candidates.add(ReflectionUtils.invokeMethod(descriptor.getReadMethod(), target));
        }
    }, new ReflectionUtils.MethodFilter() {
        @Override
        public boolean matches(Method method) {
            boolean hasAnnotation = false;
            hasAnnotation = method.isAnnotationPresent(annotation);
            if (!hasAnnotation) {
                return false;
            }

            boolean hasReadMethod = false;
            PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
            hasReadMethod = descriptor != null && descriptor.getReadMethod() != null;

            if (!hasReadMethod) {
                return false;
            }

            return true;
        }
    });

    return candidates;
}

From source file:io.dyn.core.handler.AnnotationHandlerMethodResolver.java

@Override
public boolean supports(final Class<?> aClass) {
    boolean classLevelAnno = (null != AnnotationUtils.findAnnotation(aClass, Handler.class));
    final AtomicBoolean hasHandlerMethod = new AtomicBoolean(false);
    Class<?> clazz = aClass;
    while (null != clazz && clazz != Object.class) {
        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            @Override//from   w  w  w  .ja  v a 2s  .  c  o  m
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                if (!hasHandlerMethod.get()) {
                    hasHandlerMethod.set(true);
                }
            }
        }, Handlers.USER_METHODS);
        clazz = clazz.getSuperclass();
    }

    return (hasHandlerMethod.get() || classLevelAnno);
}

From source file:com.ryantenney.metrics.spring.GaugeAnnotationBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
    final Class<?> targetClass = AopUtils.getTargetClass(bean);

    ReflectionUtils.doWithFields(targetClass, new FieldCallback() {
        @Override/*from   w  w w  .  ja va2 s .  com*/
        public void doWith(final Field field) throws IllegalAccessException {
            ReflectionUtils.makeAccessible(field);

            final Gauge annotation = field.getAnnotation(Gauge.class);
            final String metricName = Util.forGauge(targetClass, field, annotation);

            metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
                @Override
                public Object getValue() {
                    Object value = ReflectionUtils.getField(field, bean);
                    if (value instanceof com.codahale.metrics.Gauge) {
                        value = ((com.codahale.metrics.Gauge<?>) value).getValue();
                    }
                    return value;
                }
            });

            LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(),
                    field.getName());
        }
    }, FILTER);

    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override
        public void doWith(final Method method) throws IllegalAccessException {
            if (method.getParameterTypes().length > 0) {
                throw new IllegalStateException(
                        "Method " + method.getName() + " is annotated with @Gauge but requires parameters.");
            }

            final Gauge annotation = method.getAnnotation(Gauge.class);
            final String metricName = Util.forGauge(targetClass, method, annotation);

            metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() {
                @Override
                public Object getValue() {
                    return ReflectionUtils.invokeMethod(method, bean);
                }
            });

            LOG.debug("Created gauge {} for method {}.{}", metricName, targetClass.getCanonicalName(),
                    method.getName());
        }
    }, FILTER);

    return bean;
}

From source file:com.consol.citrus.admin.converter.endpoint.AbstractEndpointConverter.java

private Method findSetter(Class<S> modelClass, String fieldName) {
    final Method[] setter = { null };

    ReflectionUtils.doWithMethods(modelClass, new ReflectionUtils.MethodCallback() {
        @Override//from  ww w  .  j  av  a 2 s  .c o  m
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals("set" + StringUtils.capitalize(fieldName))) {
                setter[0] = method;
            }
        }
    }, new ReflectionUtils.MethodFilter() {
        @Override
        public boolean matches(Method method) {
            return method.getName().startsWith("set");
        }
    });

    if (setter[0] == null) {
        throw new ApplicationRuntimeException(String.format(
                "Unable to find proper setter for field '%s' on model class '%s'", fieldName, modelClass));
    }

    return setter[0];
}