List of usage examples for org.springframework.util ReflectionUtils doWithFields
public static void doWithFields(Class<?> clazz, FieldCallback fc)
From source file:py.una.pol.karaku.dao.entity.interceptors.InterceptorHandler.java
/** * Intercepta un bean especifico.//from w ww . j a v a2 s .c om * * <p> * Intercepta todos los campos de un objeto, buscando aquellos que tengan * algn interceptor definido. * </p> * <p> * Reglas: * <ol> * <li>Si el item es un atributo normal, invocar a su respectivo * interceptor. * </p> * <li>Si es una coleccin, y tiene tiene la anotacin {@link OneToMany}, y * su {@link CascadeType} es {@link CascadeType#ALL}, entonces se propaga la * intercepcin a los miembros de la coleccin. </p> * * @param op * Operacin actual. * @param bean * objeto que esta siendo interceptado. */ public void intercept(@Nonnull final Operation op, final Object bean) { ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() { @Override public void doWith(Field field) throws IllegalAccessException { InterceptorHandler.this.intercept(op, notNull(field), bean); } }); }
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)); }// ww w .j ava 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:org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBEntityMetadataSupport.java
/** * Creates a new {@link DynamoDBEntityMetadataSupport} for the given domain type. * * @param domainType//from ww w . j a v a 2s. co m * 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:com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.java
/** * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} fields * * @param beanClass The {@link Class} of Bean * @return non-null {@link List}/*from w ww .j av a 2s .c o m*/ */ private List<InjectionMetadata.InjectedElement> findFieldReferenceMetadata(final Class<?> beanClass) { final List<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>(); ReflectionUtils.doWithFields(beanClass, new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { Reference reference = getAnnotation(field, Reference.class); if (reference != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation is not supported on static fields: " + field); } return; } elements.add(new ReferenceFieldElement(field, reference)); } } }); return elements; }
From source file:org.web4thejob.orm.MetaReaderServiceImpl.java
@Override public void refreshMetaCache() { ((SessionFactoryImpl) sessionFactory).registerEntityNameResolver(EntityNameResolverImpl.INSTANCE); annoCache.clear();//from ww w . j a v a 2s . c o m for (final ClassMetadata classMetadata : sessionFactory.getAllClassMetadata().values()) { final AnnotationReader reader = new AnnotationReader(classMetadata.getEntityName()); ReflectionUtils.doWithFields(classMetadata.getMappedClass(), reader); if (reader.map.size() > 0) { annoCache.put(classMetadata.getEntityName(), reader.map); } } metaCache.clear(); //first pass for (final ClassMetadata classMetadata : sessionFactory.getAllClassMetadata().values()) { if (!metaCache.containsKey(classMetadata.getEntityName())) { metaCache.put(classMetadata.getEntityName(), new EntityMetadataImpl(classMetadata)); } } //second pass for (EntityMetadata entityMetadata : metaCache.values()) { ((EntityMetadataImpl) entityMetadata).initUniqueKeyConstraints(); } }
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"); }/* w ww . ja va 2s . c o m*/ 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.association.AbstractAssociation.java
public static AbstractAssociation createAssociation(ModelAssociation associationAnnotation, ModelBean model, Class<?> typeOfFieldOrReturnValue, Class<?> declaringClass, String name) { ModelAssociationType type = associationAnnotation.value(); Class<?> associationClass = associationAnnotation.model(); if (associationClass == Object.class) { associationClass = typeOfFieldOrReturnValue; }// w w w .ja va2 s . c om final AbstractAssociation association; if (type == ModelAssociationType.HAS_MANY) { association = new HasManyAssociation(associationClass); } else if (type == ModelAssociationType.BELONGS_TO) { association = new BelongsToAssociation(associationClass); } else { association = new HasOneAssociation(associationClass); } association.setAssociationKey(name); if (StringUtils.hasText(associationAnnotation.foreignKey())) { association.setForeignKey(associationAnnotation.foreignKey()); } else if (type == ModelAssociationType.HAS_MANY) { association.setForeignKey(StringUtils.uncapitalize(declaringClass.getSimpleName()) + "_id"); } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) { association.setForeignKey(name + "_id"); } if (StringUtils.hasText(associationAnnotation.primaryKey())) { association.setPrimaryKey(associationAnnotation.primaryKey()); } else if (type == ModelAssociationType.HAS_MANY && StringUtils.hasText(model.getIdProperty()) && !model.getIdProperty().equals("id")) { association.setPrimaryKey(model.getIdProperty()); } else if (type == ModelAssociationType.BELONGS_TO || type == ModelAssociationType.HAS_ONE) { Model associationModelAnnotation = associationClass.getAnnotation(Model.class); if (associationModelAnnotation != null && StringUtils.hasText(associationModelAnnotation.idProperty()) && !associationModelAnnotation.idProperty().equals("id")) { association.setPrimaryKey(associationModelAnnotation.idProperty()); } ReflectionUtils.doWithFields(associationClass, new FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (field.getAnnotation(ModelId.class) != null && !"id".equals(field.getName())) { association.setPrimaryKey(field.getName()); } } }); } if (type == ModelAssociationType.HAS_MANY) { HasManyAssociation hasManyAssociation = (HasManyAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(declaringClass, name, association.getType(), "setterName")); } if (StringUtils.hasText(associationAnnotation.getterName())) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(declaringClass, name, association.getType(), "getterName")); } if (associationAnnotation.autoLoad()) { hasManyAssociation.setAutoLoad(Boolean.TRUE); } if (StringUtils.hasText(associationAnnotation.name())) { hasManyAssociation.setName(associationAnnotation.name()); } else { hasManyAssociation.setName(name); } } else if (type == ModelAssociationType.BELONGS_TO) { BelongsToAssociation belongsToAssociation = (BelongsToAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { belongsToAssociation.setSetterName(associationAnnotation.setterName()); } else { belongsToAssociation.setSetterName("set" + StringUtils.capitalize(name)); } if (StringUtils.hasText(associationAnnotation.getterName())) { belongsToAssociation.setGetterName(associationAnnotation.getterName()); } else { belongsToAssociation.setGetterName("get" + StringUtils.capitalize(name)); } if (associationAnnotation.autoLoad()) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(declaringClass, name, association.getType(), "autoLoad")); } if (StringUtils.hasText(associationAnnotation.name())) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(declaringClass, name, association.getType(), "name")); } } else { HasOneAssociation hasOneAssociation = (HasOneAssociation) association; if (StringUtils.hasText(associationAnnotation.setterName())) { hasOneAssociation.setSetterName(associationAnnotation.setterName()); } else { hasOneAssociation.setSetterName("set" + StringUtils.capitalize(name)); } if (StringUtils.hasText(associationAnnotation.getterName())) { hasOneAssociation.setGetterName(associationAnnotation.getterName()); } else { hasOneAssociation.setGetterName("get" + StringUtils.capitalize(name)); } if (associationAnnotation.autoLoad()) { LogFactory.getLog(ModelGenerator.class) .warn(getWarningText(declaringClass, name, association.getType(), "autoLoad")); } if (StringUtils.hasText(associationAnnotation.name())) { hasOneAssociation.setName(associationAnnotation.name()); } } if (StringUtils.hasText(associationAnnotation.instanceName())) { association.setInstanceName(associationAnnotation.instanceName()); } return association; }
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(); }// w ww . j a v a 2 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:com.kcs.core.utilities.Utility.java
public static void analyze(final Object obj) { ReflectionUtils.doWithFields(obj.getClass(), new ReflectionUtils.FieldCallback() { @Override// ww w .j a v a 2 s. c o m public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); logger.debug(field.getName() + " : " + field.get(obj)); } }); }