Java 类org.springframework.util.ReflectionUtils.MethodCallback 实例源码

项目:configx    文件:ConfigurationBeanFactoryMetaData.java   
private Method findFactoryMethod(String beanName) {
    if (!this.beans.containsKey(beanName)) {
        return null;
    }
    final AtomicReference<Method> found = new AtomicReference<Method>(null);
    MetaData meta = this.beans.get(beanName);
    final String factory = meta.getMethod();
    Class<?> type = this.beanFactory.getType(meta.getBean());
    ReflectionUtils.doWithMethods(type, new MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals(factory)) {
                found.compareAndSet(null, method);
            }
        }
    });
    return found.get();
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:ConfigurationBeanFactoryMetaData.java   
private Method findFactoryMethod(String beanName) {
    if (!this.beans.containsKey(beanName)) {
        return null;
    }
    final AtomicReference<Method> found = new AtomicReference<Method>(null);
    MetaData meta = this.beans.get(beanName);
    final String factory = meta.getMethod();
    Class<?> type = this.beanFactory.getType(meta.getBean());
    ReflectionUtils.doWithMethods(type, new MethodCallback() {
        @Override
        public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals(factory)) {
                found.compareAndSet(null, method);
            }
        }
    });
    return found.get();
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:OnBeanCondition.java   
private void addDeducedBeanTypeForBeanMethod(ConditionContext context,
        AnnotatedTypeMetadata metadata, final List<String> beanTypes,
        final MethodMetadata methodMetadata) {
    try {
        // We should be safe to load at this point since we are in the
        // REGISTER_BEAN phase
        Class<?> configClass = ClassUtils.forName(
                methodMetadata.getDeclaringClassName(), context.getClassLoader());
        ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
            @Override
            public void doWith(Method method)
                    throws IllegalArgumentException, IllegalAccessException {
                if (methodMetadata.getMethodName().equals(method.getName())) {
                    beanTypes.add(method.getReturnType().getName());
                }
            }
        });
    }
    catch (Throwable ex) {
        throw new BeanTypeDeductionException(
                methodMetadata.getDeclaringClassName(),
                methodMetadata.getMethodName(), ex);
    }
}
项目:spring-boot-concourse    文件:ConfigurationBeanFactoryMetaData.java   
private Method findFactoryMethod(String beanName) {
    if (!this.beans.containsKey(beanName)) {
        return null;
    }
    final AtomicReference<Method> found = new AtomicReference<Method>(null);
    MetaData meta = this.beans.get(beanName);
    final String factory = meta.getMethod();
    Class<?> type = this.beanFactory.getType(meta.getBean());
    ReflectionUtils.doWithMethods(type, new MethodCallback() {
        @Override
        public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals(factory)) {
                found.compareAndSet(null, method);
            }
        }
    });
    return found.get();
}
项目:spring-boot-concourse    文件:OnBeanCondition.java   
private void addDeducedBeanTypeForBeanMethod(ConditionContext context,
        AnnotatedTypeMetadata metadata, final List<String> beanTypes,
        final MethodMetadata methodMetadata) {
    try {
        // We should be safe to load at this point since we are in the
        // REGISTER_BEAN phase
        Class<?> configClass = ClassUtils.forName(
                methodMetadata.getDeclaringClassName(), context.getClassLoader());
        ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
            @Override
            public void doWith(Method method)
                    throws IllegalArgumentException, IllegalAccessException {
                if (methodMetadata.getMethodName().equals(method.getName())) {
                    beanTypes.add(method.getReturnType().getName());
                }
            }
        });
    }
    catch (Throwable ex) {
        throw new BeanTypeDeductionException(
                methodMetadata.getDeclaringClassName(),
                methodMetadata.getMethodName(), ex);
    }
}
项目:contestparser    文件:ConfigurationBeanFactoryMetaData.java   
private Method findFactoryMethod(String beanName) {
    if (!this.beans.containsKey(beanName)) {
        return null;
    }
    final AtomicReference<Method> found = new AtomicReference<Method>(null);
    MetaData meta = this.beans.get(beanName);
    final String factory = meta.getMethod();
    Class<?> type = this.beanFactory.getType(meta.getBean());
    ReflectionUtils.doWithMethods(type, new MethodCallback() {
        @Override
        public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
            if (method.getName().equals(factory)) {
                found.compareAndSet(null, method);
            }
        }
    });
    return found.get();
}
项目:spring-data-dynamodb    文件: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());
                }
            }
        }
    });
    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 + "!");

}
项目:lams    文件:ScheduledAnnotationBeanPostProcessor.java   
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            for (Scheduled scheduled : AnnotationUtils.getRepeatableAnnotation(method, Schedules.class, Scheduled.class)) {
                processScheduled(scheduled, method, bean);
            }
        }
    });
    return bean;
}
项目:my-spring-cache-redis    文件:ScheduledAnnotationBeanPostProcessor.java   
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    ReflectionUtils.doWithMethods(targetClass, new MethodCallback() {
        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            for (Scheduled scheduled : AnnotationUtils.getRepeatableAnnotation(method, Schedules.class, Scheduled.class)) {
                processScheduled(scheduled, method, bean);
            }
        }
    });
    return bean;
}
项目:netty-http-3.x    文件:InterceptorManager.java   
private Map<Integer, Set<Interceptor>> getInterceptors0(final Object bean, final Class<? extends Annotation> annotationClass) {
    final Map<Integer, Set<Interceptor>> interceptorMap = Maps.newTreeMap();

    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {
        private Set<Class<? extends Annotation>> getAnnotations(Class<? extends Annotation>[] annotations) {
            if (ArrayUtils.isEmpty(annotations)) {
                return null;
            }

            Set<Class<? extends Annotation>> set = Sets.newLinkedHashSetWithExpectedSize(annotations.length);
            Collections.addAll(set, annotations);
            return set;
        }

        @Override
        @SuppressWarnings("unchecked")
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Annotation annotation = method.getAnnotation(annotationClass);
            Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotation);

            int priority = MapUtils.getIntValue(annotationAttributes, "priority", 0);
            Set<Class<? extends Annotation>> only = getAnnotations((Class<? extends Annotation>[]) annotationAttributes.get("only"));
            Set<Class<? extends Annotation>> unless = getAnnotations((Class<? extends Annotation>[]) annotationAttributes.get("unless"));

            Set<Interceptor> interceptors = interceptorMap.get(priority);

            if (interceptors == null) {
                interceptors = Sets.newLinkedHashSet();
            }

            interceptors.add(new Interceptor(bean, method, only, unless));
            interceptorMap.put(priority, interceptors);
        }
    }, method -> method.isAnnotationPresent(annotationClass));

    return interceptorMap;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:ImportsContextCustomizerFactory.java   
private void assertHasNoBeanMethods(Class<?> testClass) {
    ReflectionUtils.doWithMethods(testClass, new MethodCallback() {

        @Override
        public void doWith(Method method) {
            Assert.state(!AnnotatedElementUtils.isAnnotated(method, Bean.class),
                    "Test classes cannot include @Bean methods");
        }

    });

}
项目:spring-boot-concourse    文件:ImportsContextCustomizerFactory.java   
private void assertHasNoBeanMethods(Class<?> testClass) {
    ReflectionUtils.doWithMethods(testClass, new MethodCallback() {

        @Override
        public void doWith(Method method) {
            Assert.state(!AnnotatedElementUtils.isAnnotated(method, Bean.class),
                    "Test classes cannot include @Bean methods");
        }

    });

}
项目:contestparser    文件:OnBeanCondition.java   
private void addDeducedBeanTypeForBeanMethod(ConditionContext context,
        AnnotatedTypeMetadata metadata, final List<String> beanTypes,
        final MethodMetadata methodMetadata) {
    try {
        // We should be safe to load at this point since we are in the
        // REGISTER_BEAN phase
        Class<?> configClass = ClassUtils.forName(
                methodMetadata.getDeclaringClassName(), context.getClassLoader());
        ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
            @Override
            public void doWith(Method method)
                    throws IllegalArgumentException, IllegalAccessException {
                if (methodMetadata.getMethodName().equals(method.getName())) {
                    beanTypes.add(method.getReturnType().getName());
                }
            }
        });
    }
    catch (Throwable ex) {
        // swallow exception and continue
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to deduce bean type for "
                    + methodMetadata.getDeclaringClassName() + "."
                    + methodMetadata.getMethodName(), ex);
        }
    }
}
项目:spring-data-dynamodb    文件:DynamoDBHashAndRangeKeyExtractingEntityMetadataImpl.java   
@Override
public Set<String> getIndexRangeKeyPropertyNames() {
    final Set<String> propertyNames = new HashSet<String>();
    ReflectionUtils.doWithMethods(getJavaType(), new MethodCallback() {
        public void doWith(Method method) {
            if (method.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
                if ((method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && method
                        .getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
                        || (method.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && method
                                .getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
                    propertyNames.add(getPropertyNameForAccessorMethod(method));
                }
            }
        }
    });
    ReflectionUtils.doWithFields(getJavaType(), new FieldCallback() {
        public void doWith(Field field) {
            if (field.getAnnotation(DynamoDBIndexRangeKey.class) != null) {
                if ((field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName() != null && field
                        .getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexName().trim().length() > 0)
                        || (field.getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames() != null && field
                                .getAnnotation(DynamoDBIndexRangeKey.class).localSecondaryIndexNames().length > 0)) {
                    propertyNames.add(getPropertyNameForField(field));
                }
            }
        }
    });
    return propertyNames;
}
项目:spring-data-dynamodb    文件:FieldAndGetterReflectionEntityInformation.java   
/**
 * Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the
 * given domain class for a getter carrying the given annotation.
 * 
 * @param domainClass
 *            must not be {@literal null}.
 * @param annotation
 *            must not be {@literal null}.
 */
public FieldAndGetterReflectionEntityInformation(Class<T> domainClass, final Class<? extends Annotation> annotation) {

    super(domainClass);
    Assert.notNull(annotation);

    ReflectionUtils.doWithMethods(domainClass, new MethodCallback() {
        public void doWith(Method method) {
            if (method.getAnnotation(annotation) != null) {
                FieldAndGetterReflectionEntityInformation.this.method = method;
                return;
            }
        }
    });

    if (method == null)
    {
        ReflectionUtils.doWithFields(domainClass, new FieldCallback() {
            public void doWith(Field field) {
                if (field.getAnnotation(annotation) != null) {
                    FieldAndGetterReflectionEntityInformation.this.field = field;
                    return;
                }
            }
        });
    }

    Assert.isTrue(this.method != null || this.field != null, String.format("No field or method annotated with %s found!", annotation.toString()));
    Assert.isTrue(this.method == null || this.field == null, String.format("Both field and method annotated with %s found!", annotation.toString()));

    if (method != null)
    {
        ReflectionUtils.makeAccessible(method);
    }
}
项目:jdal    文件:AnnotatedElementAccessor.java   
/**
 * Find annotated elements on types
 * @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;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:TestRestTemplateTests.java   
@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override
        public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class,
                    method.getName(), method.getParameterTypes());
            try {
                equivalent.invoke(restTemplate,
                        mockArguments(method.getParameterTypes()));
            }
            catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            return mock(type);
        }

    });

}
项目:spring-boot-concourse    文件:TestRestTemplateTests.java   
@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override
        public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class,
                    method.getName(), method.getParameterTypes());
            try {
                equivalent.invoke(restTemplate,
                        mockArguments(method.getParameterTypes()));
            }
            catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            return mock(type);
        }

    });

}