List of usage examples for org.springframework.util ReflectionUtils doWithFields
public static void doWithFields(Class<?> clazz, FieldCallback fc)
From source file:cn.org.once.cstack.cli.processor.LoggerPostProcessor.java
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (field.getAnnotation(InjectLogger.class) != null && field.getType().equals(Logger.class)) { ReflectionUtils.makeAccessible(field); Logger logger = Logger.getLogger(bean.getClass().getName()); field.set(bean, logger); StreamHandler fileHandler; try { FileOutputStream fileOutputStream = new FileOutputStream(new File("errors.log"), true); fileHandler = new StreamHandler(fileOutputStream, new SimpleFormatter()); fileHandler.setLevel(Level.SEVERE); logger.addHandler(fileHandler); } catch (SecurityException | IOException e) { throw new IllegalArgumentException(e); }/* ww w.j a va 2s . co m*/ } } }); return bean; }
From source file:com.github.braully.web.DescriptorHtmlEntity.java
private void parseFieldClass(Class classe1) { ReflectionUtils.doWithFields(classe1, (Field field) -> { addHtmlFormElement(field);//from w ww .ja v a 2s. c o m addHtmlFilterElement(field); addHtmlListElement(field); }); Collections.sort(elementsList, new Comparator<DescriptorHtmlEntity>() { @Override public int compare(DescriptorHtmlEntity t, DescriptorHtmlEntity t1) { try { return t.property.compareToIgnoreCase(t1.property); } catch (Exception e) { } return 0; } }); }
From source file:com.blstream.hooks.springframework.mongodb.event.ListCascadingMongoEventListener.java
public void onBeforeConvert(final Object source) { ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() { @Override//from w ww .j a v a 2s. com public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(DBRefListCascade.class)) { final Object fieldValue = field.get(source); if (fieldValue instanceof Collection<?>) { Collection<?> collectionField = (Collection<?>) fieldValue; for (Object collectionFieldElement : collectionField) { DbRefFieldCallback callback = new DbRefFieldCallback(); ReflectionUtils.doWithFields(collectionFieldElement.getClass(), callback); if (!callback.isIdFound()) { throw new MappingException( "Cannot perform cascade save on child object without id set"); } mongoOperations.save(fieldValue); } } else { throw new MappingException( "Cannot perform cascade save using DBRefListCascade annotation on not collection bean."); } } } }); }
From source file:my.adam.smo.common.LoggerInjector.java
@Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); if (field.getAnnotation(InjectLogger.class) != null) { Logger logger = LoggerFactory.getLogger(bean.getClass()); field.set(bean, logger); }//w w w . j av a2 s. c o m } }); return bean; }
From source file:pl.konczak.mystartupapp.sharedkernel.enversRepository.ReflectionRevisionEntityInformation.java
/** * Creates a new {@link ReflectionRevisionEntityInformation} inspecting the given revision entity class. * /*from w w w .java 2 s . c o m*/ * @param revisionEntityClass must not be {@literal null}. */ public ReflectionRevisionEntityInformation(Class<?> revisionEntityClass) { Assert.notNull(revisionEntityClass); AnnotationDetectionFieldCallback fieldCallback = new AnnotationDetectionFieldCallback(RevisionNumber.class); ReflectionUtils.doWithFields(revisionEntityClass, fieldCallback); this.revisionNumberType = fieldCallback.getType(); this.revisionEntityClass = revisionEntityClass; }
From source file:org.appverse.web.framework.backend.api.helpers.log.AutowiredLoggerBeanPostProcessor.java
@Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { @Override//from ww w.j a va 2 s . c om public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { if (field.isAnnotationPresent(AutowiredLogger.class)) { field.setAccessible(true); field.set(bean, LoggerFactory.getLogger(bean.getClass())); } } }); return bean; }
From source file:dwalldorf.jadecr.converter.PropertyConverter.java
/** * Will search for all fields that exist equally in both, {@code src} and {@code dest}, and set copy the value from * {@code src} to {@code dest}./*from w w w.jav a 2s . c om*/ * * @param src the object to be copied * @param dest the object to copy into * @throws IllegalAccessException */ private void copyValues(final Object src, final Object dest) throws IllegalAccessException { ReflectionUtils.doWithFields(src.getClass(), field -> { Field destField = ReflectionUtils.findField(dest.getClass(), field.getName()); if (destField == null) { return; } ReflectionUtils.makeAccessible(field); ReflectionUtils.makeAccessible(destField); Object value = field.get(src); setValue(value, destField, dest); }); }
From source file:net.openj21.mih.protocol.codec.AnnotationUtils.java
/** * Returns a Collection containing all fields of the given SEQUENCE class * that are annotated with the SEQUENCE_ELEMENT annotation. The collection * is sorted on the order of the sequence elements. * * @param sequenceClass a class// w w w.j a va 2s . c om * @return a Collection containing all fields of the given SEQUENCE class */ public Collection<Field> getSequenceFields(Class<?> sequenceClass) { final SortedMap<Integer, Field> toEncodeFields = new TreeMap<Integer, Field>(); ReflectionUtils.doWithFields(sequenceClass, new ReflectionUtils.FieldCallback() { public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { SEQUENCE_ELEMENT encodedAnnotation = field.getAnnotation(SEQUENCE_ELEMENT.class); if (encodedAnnotation != null) { if (toEncodeFields.put(encodedAnnotation.order(), field) != null) { throw new IllegalStateException( "Duplicate " + SEQUENCE_ELEMENT.class.getSimpleName() + " order: " + field); } } } }); return toEncodeFields.values(); }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBHashAndRangeKeyMethodExtractorImpl.java
/** * Creates a new {@link DynamoDBHashAndRangeKeyMethodExtractor} for the given domain type. * * @param idType//from ww w . j ava 2 s . co m * must not be {@literal null}. */ public DynamoDBHashAndRangeKeyMethodExtractorImpl(final Class<T> idType) { Assert.notNull(idType, "Id type must not be null!"); this.idType = idType; ReflectionUtils.doWithMethods(idType, new MethodCallback() { @Override public void doWith(Method method) { if (method.getAnnotation(DynamoDBHashKey.class) != null) { Assert.isNull(hashKeyMethod, "Multiple methods annotated by @DynamoDBHashKey within type " + idType.getName() + "!"); ReflectionUtils.makeAccessible(method); hashKeyMethod = method; } } }); ReflectionUtils.doWithFields(idType, new FieldCallback() { @Override public void doWith(Field field) { if (field.getAnnotation(DynamoDBHashKey.class) != null) { Assert.isNull(hashKeyField, "Multiple fields annotated by @DynamoDBHashKey within type " + idType.getName() + "!"); ReflectionUtils.makeAccessible(field); hashKeyField = field; } } }); ReflectionUtils.doWithMethods(idType, new MethodCallback() { @Override public void doWith(Method method) { if (method.getAnnotation(DynamoDBRangeKey.class) != null) { Assert.isNull(rangeKeyMethod, "Multiple methods annotated by @DynamoDBRangeKey within type " + idType.getName() + "!"); ReflectionUtils.makeAccessible(method); rangeKeyMethod = method; } } }); ReflectionUtils.doWithFields(idType, new FieldCallback() { @Override public void doWith(Field field) { if (field.getAnnotation(DynamoDBRangeKey.class) != null) { Assert.isNull(rangeKeyField, "Multiple fields annotated by @DynamoDBRangeKey within type " + idType.getName() + "!"); ReflectionUtils.makeAccessible(field); rangeKeyField = field; } } }); if (hashKeyMethod == null && hashKeyField == null) { throw new IllegalArgumentException( "No method or field annotated by @DynamoDBHashKey within type " + idType.getName() + "!"); } if (rangeKeyMethod == null && rangeKeyField == null) { throw new IllegalArgumentException( "No method or field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!"); } if (hashKeyMethod != null && hashKeyField != null) { throw new IllegalArgumentException( "Both method and field annotated by @DynamoDBHashKey within type " + idType.getName() + "!"); } if (rangeKeyMethod != null && rangeKeyField != null) { throw new IllegalArgumentException( "Both method and field annotated by @DynamoDBRangeKey within type " + idType.getName() + "!"); } }
From source file:com.excilys.ebi.utils.spring.log.slf4j.InjectLoggerAnnotationBeanPostProcessor.java
/** * Processes a bean's fields for injection if it has a {@link InjectLogger} * annotation.//from w ww . j a va 2 s .c om */ protected void processLogger(final Object bean) { final Class<?> clazz = bean.getClass(); ReflectionUtils.doWithFields(clazz, new FieldCallback() { public void doWith(Field field) { Annotation annotation = field.getAnnotation(InjectLogger.class); if (annotation != null) { int modifiers = field.getModifiers(); Assert.isTrue(!Modifier.isStatic(modifiers), "InjectLogger annotation is not supported on static fields"); Assert.isTrue(!Modifier.isFinal(modifiers), "InjectLogger annotation is not supported on final fields"); ReflectionUtils.makeAccessible(field); Logger logger = LoggerFactory.getLogger(clazz); ReflectionUtils.setField(field, bean, logger); } } }); }