List of usage examples for org.springframework.util ReflectionUtils doWithFields
public static void doWithFields(Class<?> clazz, FieldCallback fc)
From source file:com.vilt.minium.jasmine.MiniumJasmineTestRunner.java
@Override protected Object createTestClassInstance() { try {//from w w w.ja v a2s .c o m final Object testInstance = super.createTestClassInstance(); contextManager.prepareTestInstance(testInstance); ReflectionUtils.doWithFields(testClass, new FieldCallback() { @Override public void doWith(Field f) throws IllegalArgumentException, IllegalAccessException { f.setAccessible(true); JsVariable jsVariable = f.getAnnotation(JsVariable.class); if (jsVariable == null) return; String varName = jsVariable.value(); checkNotNull(varName, "@JsVariable.value() should not be null"); Object fieldVal = f.get(testInstance); Object val = getVal(jsVariable, f.getType(), fieldVal); put(rhinoContext, varName, val); if (fieldVal == null && val != null) { f.set(testInstance, val); } } }); return testInstance; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.systemoutprintln.util.logging.spring.LogPostProcessor.java
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() { @Override/*from www .j a v a 2 s. c om*/ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); if (field.getAnnotation(Log.class) != null) { Logger logger = LoggerFactory.getLogger(bean.getClass()); field.set(bean, logger); } } }); return bean; }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.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()); }/* w ww . ja v a2 s .com*/ } } }); 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 + "!"); }
From source file:de.olivergierke.ninjector.FieldInjectionRejectingBeanPostProcessor.java
@Override public Object postProcessBeforeInstantiation(final Class<?> beanClass, final String beanName) throws BeansException { ReflectionUtils.doWithFields(beanClass, new FieldCallback() { @Override//w w w .j a v a 2 s .co m public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { for (Class<? extends Annotation> annotationType : INJECTION_ANNOTATIONS) { if (AnnotationUtils.findAnnotation(field, annotationType) != null) { throw new BeanCreationNotAllowedException(beanName, String.format(ERROR, annotationType.getSimpleName(), field.getName(), beanClass.getSimpleName())); } } } }); return null; }
From source file:org.jdal.ui.bind.DirectFieldAccessor.java
/** * Create a new DirectFieldAccessor for the given target object. * @param target the target object to access *//*from ww w . j a v a2s .c om*/ public DirectFieldAccessor(final Object target) { Assert.notNull(target, "Target object must not be null"); this.target = target; ReflectionUtils.doWithFields(this.target.getClass(), new ReflectionUtils.FieldCallback() { public void doWith(Field field) { // jlm - FIX SPR-8398: avoid to overwrite shadowed fileds if (!fieldMap.containsKey(field.getName())) { fieldMap.put(field.getName(), field); } } }); this.typeConverterDelegate = new SimpleTypeConverter(); registerDefaultEditors(); setExtractOldValueForEditor(true); }
From source file:com.vilt.minium.script.test.impl.MiniumRhinoTestsSupport.java
protected Object createTestClassInstance(final MiniumRhinoTestContextManager contextManager) { try {//from w ww. jav a 2 s .co m final Object testInstance = getInstance(); contextManager.prepareTestInstance(testInstance); // extract annotated fields and bind them to rhino scope ReflectionUtils.doWithFields(testClass, new FieldCallback() { @Override public void doWith(Field f) throws IllegalArgumentException, IllegalAccessException { f.setAccessible(true); JsVariable jsVariable = f.getAnnotation(JsVariable.class); if (jsVariable == null) return; String varName = jsVariable.value(); checkNotNull(varName, "@JsVariable.value() should not be null"); Object fieldVal = f.get(testInstance); Object val = getVal(contextManager, jsVariable, f.getType(), fieldVal); put(scope, varName, val); if (fieldVal == null && val != null) { f.set(testInstance, val); } } }); return testInstance; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapStepExecutionDao.java
private static void copy(final StepExecution sourceExecution, final StepExecution targetExecution) { // Cheaper than full serialization is a reflective field copy, which is // fine for volatile storage ReflectionUtils.doWithFields(StepExecution.class, new ReflectionUtils.FieldCallback() { @Override// w ww .ja va 2 s.c o m public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); field.set(targetExecution, field.get(sourceExecution)); } }); }
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 av a 2s .co 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:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java
public static <T> T recurseReflect(final T obj, final DoInReflectionCallback callback) { if (obj == null) { return null; }// ww w. j a va 2 s . com ReflectionUtils.doWithFields(obj.getClass(), new FieldCallback() { public void doWith(Field arg0) throws IllegalArgumentException, IllegalAccessException { if (!ClassUtils.isPrimitiveOrWrapper(arg0.getType()) && !ClassUtils.isPrimitiveArray(arg0.getType()) && !ClassUtils.isPrimitiveWrapperArray(arg0.getType()) && !arg0.getType().isEnum() && (isLexBigClass(arg0.getType()) || Collection.class.isAssignableFrom(arg0.getType()) || Map.class.isAssignableFrom(arg0.getType()))) { arg0.setAccessible(true); Object recurse = arg0.get(obj); if (recurse != null) { if (CycleDetectingCallback.class.isAssignableFrom(recurse.getClass())) { System.out.println("ere"); } if (Collection.class.isAssignableFrom(recurse.getClass())) { Collection collection = (Collection) recurse; for (Object o : collection) { if (callback.actionRequired(o)) { collection.remove(o); collection.add(recurseReflect(o, callback)); } else { recurseReflect(o, callback); } } } else if (Map.class.isAssignableFrom(recurse.getClass())) { Map map = (Map) recurse; for (Object key : map.keySet()) { Object value = map.get(key); if (callback.actionRequired(key) || callback.actionRequired(value)) { map.remove(key); map.put(recurseReflect(key, callback), recurseReflect(value, callback)); } else { recurseReflect(key, callback); recurseReflect(value, callback); } } } else { if (callback.actionRequired(recurse)) { Object newObject = recurseReflect(recurse, callback); arg0.set(obj, newObject); } else { recurseReflect(recurse, callback); } } } } } }); return callback.doInReflection(obj); }
From source file:org.openinfinity.core.aspect.ArgumentBuilder.java
private void doRecursiveFieldLookUpAndCallFieldCallback( final ArgumentGatheringFieldCallback<Field, Object> argumentGatheringCallback, final Object[] objects) { for (final Object object : objects) { try {/*from ww w. j av a2 s. co m*/ if (object != null) { ReflectionUtils.doWithFields(object.getClass(), new FieldCallback() { public void doWith(Field field) { try { if (!field.isAccessible()) { field.setAccessible(Boolean.TRUE); } if (!(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()))) { argumentGatheringCallback.onField(field, object); LOGGER.debug("Accessing field: " + field.getName()); } } catch (Throwable e) { LOGGER.error("Failure occurred while accessing object field.", e); } } }); } } catch (Throwable throwable) { throw new SystemException(throwable); } } }