List of usage examples for org.springframework.util ReflectionUtils doWithMethods
public static void doWithMethods(Class<?> clazz, MethodCallback mc)
From source file:org.socialsignin.spring.data.dynamodb.repository.support.FieldAndGetterReflectionEntityInformation.java
/** * Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the * given domain class for a getter carrying the given annotation. * /* w w w . j a va2 s .c o m*/ * @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); } }
From source file:reactor.spring.context.ConsumerBeanPostProcessor.java
@Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { @Override/* w w w .j ava 2 s . c o m*/ public void doWith(final Method method) throws IllegalArgumentException, IllegalAccessException { Annotation anno = AnnotationUtils.findAnnotation(method, On.class); if (null == anno) { return; } StandardEvaluationContext evalCtx = new StandardEvaluationContext(); evalCtx.setRootObject(bean); evalCtx.setBeanResolver(beanResolver); evalCtx.setMethodResolvers(METHOD_RESOLVERS); On onAnno = (On) anno; Object reactorObj = null; if (StringUtils.hasText(onAnno.reactor())) { Expression reactorExpr = expressionParser.parseExpression(onAnno.reactor()); reactorObj = reactorExpr.getValue(evalCtx); } Object selObj; if (StringUtils.hasText(onAnno.selector())) { try { Expression selectorExpr = expressionParser.parseExpression(onAnno.selector()); selObj = selectorExpr.getValue(evalCtx); } catch (EvaluationException e) { selObj = Fn.$(onAnno.selector()); } } else { selObj = Fn.$(method.getName()); } Consumer<Event<Object>> handler = new Consumer<Event<Object>>() { Class<?>[] argTypes = method.getParameterTypes(); @Override public void accept(Event<Object> ev) { if (argTypes.length == 0) { ReflectionUtils.invokeMethod(method, bean); return; } if (!argTypes[0].isAssignableFrom(ev.getClass()) && conversionService.canConvert(ev.getClass(), argTypes[0])) { ReflectionUtils.invokeMethod(method, bean, conversionService.convert(ev, argTypes[0])); } else { ReflectionUtils.invokeMethod(method, bean, ev); return; } if (null == ev.getData() || argTypes[0].isAssignableFrom(ev.getData().getClass())) { ReflectionUtils.invokeMethod(method, bean, ev.getData()); return; } if (conversionService.canConvert(ev.getData().getClass(), argTypes[0])) { ReflectionUtils.invokeMethod(method, bean, conversionService.convert(ev.getData(), argTypes[0])); return; } throw new IllegalArgumentException( "Cannot invoke method " + method + " passing parameter " + ev.getData()); } }; if (!(selObj instanceof Selector)) { throw new IllegalArgumentException(selObj + ", referred to by the expression '" + onAnno.selector() + "', is not a Selector"); } if (null == reactorObj) { throw new IllegalStateException("Cannot register handler with null Reactor"); } else { ((Reactor) reactorObj).on((Selector) selObj, handler); } } }); return bean; }
From source file:com.googlecode.ehcache.annotations.key.CachingReflectionHelper.java
/** * Scans a class to see if it implements the hashCode, toString and equals methods which are commonly * used by key generators/*from w ww .ja va2 s . c o m*/ */ private boolean doesImplement(final Class<?> elementClass, ImplementsMethod method) { final Map<Class<?>, Set<ImplementsMethod>> cache = this.getCache(); Set<ImplementsMethod> methodCache = cache.get(elementClass); if (methodCache == null) { methodCache = EnumSet.noneOf(ImplementsMethod.class); cache.put(elementClass, methodCache); //Create final reference for use by anonymous class final Set<ImplementsMethod> implementsSet = methodCache; ReflectionUtils.doWithMethods(elementClass, new MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (implementsSet.size() == 3 || method.getDeclaringClass() == Object.class) { return; } if (ReflectionUtils.isEqualsMethod(method)) { implementsSet.add(ImplementsMethod.EQUALS); } else if (ReflectionUtils.isHashCodeMethod(method)) { implementsSet.add(ImplementsMethod.HASH_CODE); } else if (ReflectionUtils.isToStringMethod(method)) { implementsSet.add(ImplementsMethod.TO_STRING); } } }); } return methodCache.contains(method); }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport.java
/** * Creates a new {@link DynamoDBEntityMetadataSupport} for the given domain type. * * @param domainType//from ww w . ja va 2 s . c om * must not be {@literal null}. */ public DynamoDBEntityMetadataSupport(final Class<T> domainType) { Assert.notNull(domainType, "Domain type must not be null!"); this.domainType = domainType; DynamoDBTable table = this.domainType.getAnnotation(DynamoDBTable.class); Assert.notNull(table, "Domain type must by annotated with DynamoDBTable!"); this.dynamoDBTableName = table.tableName(); this.globalSecondaryIndexNames = new HashMap<String, String[]>(); this.globalIndexHashKeyPropertyNames = new ArrayList<String>(); this.globalIndexRangeKeyPropertyNames = new ArrayList<String>(); ReflectionUtils.doWithMethods(domainType, new MethodCallback() { @Override public void doWith(Method method) { if (method.getAnnotation(DynamoDBHashKey.class) != null) { hashKeyPropertyName = getPropertyNameForAccessorMethod(method); } if (method.getAnnotation(DynamoDBRangeKey.class) != null) { hasRangeKey = true; } DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = method .getAnnotation(DynamoDBIndexRangeKey.class); DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = method.getAnnotation(DynamoDBIndexHashKey.class); if (dynamoDBRangeKeyAnnotation != null) { addGlobalSecondaryIndexNames(method, dynamoDBRangeKeyAnnotation); } if (dynamoDBHashKeyAnnotation != null) { addGlobalSecondaryIndexNames(method, dynamoDBHashKeyAnnotation); } } }); ReflectionUtils.doWithFields(domainType, new FieldCallback() { @Override public void doWith(Field field) { if (field.getAnnotation(DynamoDBHashKey.class) != null) { hashKeyPropertyName = getPropertyNameForField(field); } if (field.getAnnotation(DynamoDBRangeKey.class) != null) { hasRangeKey = true; } DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = field.getAnnotation(DynamoDBIndexRangeKey.class); DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = field.getAnnotation(DynamoDBIndexHashKey.class); if (dynamoDBRangeKeyAnnotation != null) { addGlobalSecondaryIndexNames(field, dynamoDBRangeKeyAnnotation); } if (dynamoDBHashKeyAnnotation != null) { addGlobalSecondaryIndexNames(field, dynamoDBHashKeyAnnotation); } } }); Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!"); }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.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)); }/* w w w . j av a 2 s . c o m*/ } } }); 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; }
From source file:guru.qas.martini.annotation.StepsAnnotationProcessor.java
private void processStepsBean(String beanName, Class wrapped) { checkState(context.isSingleton(beanName), "Beans annotated @Steps must have singleton scope."); for (ReflectionUtils.MethodCallback callback : callbacks) { ReflectionUtils.doWithMethods(wrapped, callback); }//w ww . j a v a 2 s . c om }
From source file:com.watchrabbit.executor.spring.ExecutorAnnotationBeanPostProcessor.java
private Map<Method, Executor> findAnnotatedMethods(Class<?> targetClass) throws IllegalArgumentException { Map<Method, Executor> annotatedMethods = new HashMap<>(); ReflectionUtils.doWithMethods(targetClass, (Method method) -> { Executor annotation = AnnotationUtils.getAnnotation(method, Executor.class); if (annotation != null) { annotatedMethods.put(method, annotation); }/*from w w w . jav a 2s. c om*/ }); return annotatedMethods; }
From source file:com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.java
/** * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} methods * * @param beanClass The {@link Class} of Bean * @return non-null {@link List}//w w w . j av a 2 s .c om */ private List<InjectionMetadata.InjectedElement> findMethodReferenceMetadata(final Class<?> beanClass) { final List<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>(); ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Method bridgedMethod = findBridgedMethod(method); if (!isVisibilityBridgeMethodPair(method, bridgedMethod)) { return; } Reference reference = findAnnotation(bridgedMethod, Reference.class); if (reference != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { if (Modifier.isStatic(method.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation is not supported on static methods: " + method); } return; } if (method.getParameterTypes().length == 0) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation should only be used on methods with parameters: " + method); } } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, beanClass); elements.add(new ReferenceMethodElement(method, pd, reference)); } } }); return elements; }
From source file:org.okj.commons.annotations.ServiceReferenceInjectionBeanPostProcessor.java
private void injectServices(final Object bean, final String beanName) { ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { public void doWith(Method method) { ServiceReference s = AnnotationUtils.getAnnotation(method, ServiceReference.class); if (s != null && method.getParameterTypes().length == 1) { try { if (logger.isDebugEnabled()) logger.debug("Processing annotation [" + s + "] for [" + bean.getClass().getName() + "." + method.getName() + "()] on bean [" + beanName + "]"); method.invoke(bean, getServiceImporter(s, method, beanName).getObject()); } catch (Exception e) { throw new IllegalArgumentException("Error processing service annotation", e); }/*from ww w . j a v a 2 s .co m*/ } } }); }