List of usage examples for org.springframework.util ReflectionUtils doWithMethods
public static void doWithMethods(Class<?> clazz, MethodCallback mc)
From source file:org.okj.commons.web.json.interceptor.JSONOutputterInterceptor.java
/** * /* ww w . j av a2s . com*/ * @param handler * @param annotationClass * @return */ protected Set<Method> findHandlerMethods(Object handler, final Class<? extends Annotation> annotationClass) { final Set<Method> handlerMethods = new HashSet<Method>(); //class final Class<?> handlerClass = ClassUtils.getUserClass(handler); //handlerJsonResulthandlerMethods ReflectionUtils.doWithMethods(handlerClass, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { if (method.isAnnotationPresent(annotationClass)) { Method m = ClassUtils.getMostSpecificMethod(method, handlerClass); handlerMethods.add(m); } } }); return handlerMethods; }
From source file:io.neba.core.selftests.SelftestRegistrar.java
/** * In case no annotation metadata exists, find selftests by checking all * methods for the {@link SelfTest} annotation. *///from w ww . j a v a 2s. com private void findSelftestUsingReflection(final ConfigurableListableBeanFactory factory, final String beanName, final Bundle bundle) { Class<?> beanType = factory.getType(beanName); if (beanType != null) { beanType = unproxy(beanType); ReflectionUtils.doWithMethods(beanType, new MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { SelfTest selfTest = AnnotationUtils.findAnnotation(method, SelfTest.class); if (selfTest != null) { String methodName = method.getName(); selftestReferences .add(new SelftestReference(factory, beanName, selfTest, methodName, bundle)); } } }); } }
From source file:org.apache.james.container.spring.lifecycle.osgi.AbstractOSGIAnnotationBeanPostProcessor.java
private void injectServices(final Object bean, final String beanName) { ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() { public void doWith(Method method) { A s = AnnotationUtils.getAnnotation(method, getAnnotation()); 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 annotation " + s, e); }/* ww w . ja va2 s . co m*/ } } }); }
From source file:com.baidu.jprotobuf.pbrpc.spring.annotation.CommonAnnotationBeanPostProcessor.java
private InjectionMetadata findAnnotationMetadata(final Class clazz, final List<Class<? extends Annotation>> annotion) { // Quick check on the concurrent map first, with minimal locking. InjectionMetadata metadata = this.injectionMetadataCache.get(clazz); if (metadata == null) { synchronized (this.injectionMetadataCache) { metadata = this.injectionMetadataCache.get(clazz); if (metadata == null) { final InjectionMetadata newMetadata = new InjectionMetadata(clazz); ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { public void doWith(Field field) { for (Class<? extends Annotation> anno : annotion) { Annotation annotation = field.getAnnotation(anno); if (annotation != null) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException( "Autowired annotation is not supported on static fields"); }//from ww w. jav a 2s . c om newMetadata.addInjectedField(new AutowiredFieldElement(field, annotation)); } } } }); ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { for (Class<? extends Annotation> anno : annotion) { Annotation annotation = method.getAnnotation(anno); if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (Modifier.isStatic(method.getModifiers())) { throw new IllegalStateException( "Autowired annotation is not supported on static methods"); } if (method.getParameterTypes().length == 0) { throw new IllegalStateException( "Autowired annotation requires at least one argument: " + method); } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); newMetadata .addInjectedMethod(new AutowiredMethodElement(method, annotation, pd)); } } } }); metadata = newMetadata; this.injectionMetadataCache.put(clazz, metadata); } } } return metadata; }
From source file:ch.rasc.extclassgenerator.ModelGenerator.java
public static ModelBean createModel(final Class<?> clazz, final OutputConfig outputConfig) { Assert.notNull(clazz, "clazz must not be null"); Assert.notNull(outputConfig.getIncludeValidation(), "includeValidation must not be null"); ModelCacheKey key = new ModelCacheKey(clazz.getName(), outputConfig); SoftReference<ModelBean> modelReference = modelCache.get(key); if (modelReference != null && modelReference.get() != null) { return modelReference.get(); }//from ww w .jav a2 s . c om Model modelAnnotation = clazz.getAnnotation(Model.class); final ModelBean model = new ModelBean(); if (modelAnnotation != null && StringUtils.hasText(modelAnnotation.value())) { model.setName(modelAnnotation.value()); } else { model.setName(clazz.getName()); } if (modelAnnotation != null) { model.setAutodetectTypes(modelAnnotation.autodetectTypes()); } if (modelAnnotation != null) { model.setExtend(modelAnnotation.extend()); model.setIdProperty(modelAnnotation.idProperty()); model.setVersionProperty(trimToNull(modelAnnotation.versionProperty())); model.setPaging(modelAnnotation.paging()); model.setDisablePagingParameters(modelAnnotation.disablePagingParameters()); model.setCreateMethod(trimToNull(modelAnnotation.createMethod())); model.setReadMethod(trimToNull(modelAnnotation.readMethod())); model.setUpdateMethod(trimToNull(modelAnnotation.updateMethod())); model.setDestroyMethod(trimToNull(modelAnnotation.destroyMethod())); model.setMessageProperty(trimToNull(modelAnnotation.messageProperty())); model.setWriter(trimToNull(modelAnnotation.writer())); model.setReader(trimToNull(modelAnnotation.reader())); model.setSuccessProperty(trimToNull(modelAnnotation.successProperty())); model.setTotalProperty(trimToNull(modelAnnotation.totalProperty())); model.setRootProperty(trimToNull(modelAnnotation.rootProperty())); model.setWriteAllFields(modelAnnotation.writeAllFields()); model.setIdentifier(trimToNull(modelAnnotation.identifier())); String clientIdProperty = trimToNull(modelAnnotation.clientIdProperty()); if (StringUtils.hasText(clientIdProperty)) { model.setClientIdProperty(clientIdProperty); model.setClientIdPropertyAddToWriter(true); } else { model.setClientIdProperty(null); model.setClientIdPropertyAddToWriter(false); } } final Set<String> hasReadMethod = new HashSet<String>(); BeanInfo bi; try { bi = Introspector.getBeanInfo(clazz); } catch (IntrospectionException e) { throw new RuntimeException(e); } for (PropertyDescriptor pd : bi.getPropertyDescriptors()) { if (pd.getReadMethod() != null && pd.getReadMethod().getAnnotation(JsonIgnore.class) == null) { hasReadMethod.add(pd.getName()); } } if (clazz.isInterface()) { final List<Method> methods = new ArrayList<Method>(); ReflectionUtils.doWithMethods(clazz, new MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { methods.add(method); } }); Collections.sort(methods, new Comparator<Method>() { @Override public int compare(Method o1, Method o2) { return o1.getName().compareTo(o2.getName()); } }); for (Method method : methods) { createModelBean(model, method, outputConfig); } } else { final Set<String> fields = new HashSet<String>(); Set<ModelField> modelFieldsOnType = AnnotationUtils.getRepeatableAnnotation(clazz, ModelFields.class, ModelField.class); for (ModelField modelField : modelFieldsOnType) { if (StringUtils.hasText(modelField.value())) { ModelFieldBean modelFieldBean; if (StringUtils.hasText(modelField.customType())) { modelFieldBean = new ModelFieldBean(modelField.value(), modelField.customType()); } else { modelFieldBean = new ModelFieldBean(modelField.value(), modelField.type()); } updateModelFieldBean(modelFieldBean, modelField); model.addField(modelFieldBean); } } Set<ModelAssociation> modelAssociationsOnType = AnnotationUtils.getRepeatableAnnotation(clazz, ModelAssociations.class, ModelAssociation.class); for (ModelAssociation modelAssociationAnnotation : modelAssociationsOnType) { AbstractAssociation modelAssociation = AbstractAssociation .createAssociation(modelAssociationAnnotation); if (modelAssociation != null) { model.addAssociation(modelAssociation); } } Set<ModelValidation> modelValidationsOnType = AnnotationUtils.getRepeatableAnnotation(clazz, ModelValidations.class, ModelValidation.class); for (ModelValidation modelValidationAnnotation : modelValidationsOnType) { AbstractValidation modelValidation = AbstractValidation.createValidation( modelValidationAnnotation.propertyName(), modelValidationAnnotation, outputConfig.getIncludeValidation()); if (modelValidation != null) { model.addValidation(modelValidation); } } ReflectionUtils.doWithFields(clazz, new FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (!fields.contains(field.getName()) && (field.getAnnotation(ModelField.class) != null || field.getAnnotation(ModelAssociation.class) != null || (Modifier.isPublic(field.getModifiers()) || hasReadMethod.contains(field.getName())) && field.getAnnotation(JsonIgnore.class) == null)) { // ignore superclass declarations of fields already // found in a subclass fields.add(field.getName()); createModelBean(model, field, outputConfig); } } }); } modelCache.put(key, new SoftReference<ModelBean>(model)); return model; }
From source file:ductive.console.commands.register.spring.ArgumentParserBeanPostProcessor.java
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() { @Override/*from w w w.j a v a 2 s . co m*/ public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { ArgParser argParser = AnnotationUtils.findAnnotation(method, ArgParser.class); if (argParser == null) return; String qualifier = StringUtils.defaultIfEmpty(argParser.value(), null); if (!void.class.equals(argParser.bind())) { argParserRegistry.register(argParser.bind(), qualifier, bean, method); return; } Type genericReturnType = method.getGenericReturnType(); Validate.isTrue(ParameterizedType.class.isInstance(genericReturnType), String.format( "method %s: return type of parser generator method must be a subclass of %s<?>", method, Parser.class.getCanonicalName())); ParameterizedType pt = ParameterizedType.class.cast(genericReturnType); Validate.isTrue(Parser.class.isAssignableFrom((Class<?>) pt.getRawType()), String.format( "method %s: return type of parser generator method must be a subclass of %s<?>", method, Parser.class.getCanonicalName())); Type[] genericParams = pt.getActualTypeArguments(); Validate.isTrue(genericParams.length == 1); argParserRegistry.register((Class<?>) genericParams[0], qualifier, bean, method); } }); return bean; }
From source file:ductive.stats.spring.StatsPostProcessor.java
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() { @Override/*w w w . j av a 2 s .com*/ public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Stats cmd = AnnotationUtils.findAnnotation(method, Stats.class); if (cmd == null) return; String[] path = ArrayUtils.addAll(prefix, cmd.value()); statsProviderRegistry.register(path, bean, method); } }); return bean; }
From source file:org.apache.dubbo.config.spring.beans.factory.annotation.CompatibleReferenceAnnotationBeanPostProcessor.java
/** * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} methods * * @param beanClass The {@link Class} of Bean * @return non-null {@link List}//from w w w. j a va 2 s.com */ private List<ReferenceMethodElement> findMethodReferenceMetadata(final Class<?> beanClass) { final List<ReferenceMethodElement> elements = new LinkedList<ReferenceMethodElement>(); 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.compass.spring.support.CompassContextBeanPostProcessor.java
private synchronized List<AnnotatedMember> findClassMetadata(Class<? extends Object> clazz) { List<AnnotatedMember> metadata = this.classMetadata.get(clazz); if (metadata == null) { final List<AnnotatedMember> newMetadata = new LinkedList<AnnotatedMember>(); ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { public void doWith(Field f) { addIfPresent(newMetadata, f); }/*www . java2 s .com*/ }); // TODO is it correct to walk up the hierarchy for methods? Otherwise inheritance // is implied? CL to resolve ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method m) { addIfPresent(newMetadata, m); } }); metadata = newMetadata; this.classMetadata.put(clazz, metadata); } return metadata; }
From source file:org.openspaces.core.context.GigaSpaceContextBeanPostProcessor.java
private synchronized List<AnnotatedMember> findClassMetadata(Class<?> clazz) { List<AnnotatedMember> metadata = this.classMetadata.get(clazz); if (metadata == null) { final List<AnnotatedMember> newMetadata = new LinkedList<AnnotatedMember>(); ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { public void doWith(Field f) { addIfPresent(newMetadata, f); }/*from w w w. j ava2 s. co m*/ }); ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method m) { addIfPresent(newMetadata, m); } }); metadata = newMetadata; this.classMetadata.put(clazz, metadata); } return metadata; }