List of usage examples for org.springframework.util ReflectionUtils getField
@Nullable public static Object getField(Field field, @Nullable Object target)
From source file:org.web4thejob.orm.serial.MyXStreamMarshaller.java
private void customizeMappers(XStream xstream) { // huge HACK since there seems to be no direct way of adding // HibernateMapper final Converter reflectionConverter = xstream.getConverterLookup().lookupConverterForType(Entity.class); if (!ReflectionConverter.class.isInstance(reflectionConverter)) throw new IllegalStateException("expected " + ReflectionConverter.class.getName() + " but got " + reflectionConverter.getClass().getName()); final Field field = ReflectionUtils.findField(ReflectionConverter.class, "mapper"); ReflectionUtils.makeAccessible(field); CachingMapper mapper = (CachingMapper) ReflectionUtils.getField(field, reflectionConverter); mapper = new CachingMapper(new MyHibernateMapper(mapper)); ReflectionUtils.setField(field, reflectionConverter, mapper); }
From source file:py.una.pol.karaku.dao.entity.interceptors.TimeInterceptor.java
@Override public void intercept(Operation op, Field f, Object bean) { Object o = ReflectionUtils.getField(f, bean); if (o == null) { return;// w w w .j a v a 2s. c o m } Time t = f.getAnnotation(Time.class); Date date = (Date) o; Calendar c = Calendar.getInstance(); c.setTime(date); if ((t == null) || t.type().equals(Time.Type.DATE)) { this.handleDate(c); } else if (t.type().equals(Time.Type.TIME)) { this.handleTime(c); } // DATETIME no es manejado por que no requeire ningun // trato especial c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); ReflectionUtils.setField(f, bean, c.getTime()); }
From source file:egov.data.ibatis.repository.support.SqlMapRepositoryFactory.java
public SqlMapRepositoryFactory(SqlMapClient sqlMapClient) { Assert.notNull(sqlMapClient, "SqlMapClient must not be null!"); this.sqlMapClient = (SqlMapClientImpl) sqlMapClient; this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient); if (ExtendedSqlMapClient.class.isAssignableFrom(sqlMapClient.getClass())) { this.sqlMapExecutorDelegate = ((ExtendedSqlMapClient) sqlMapClient).getDelegate(); } else if (hasSqlMapExecutorDelegate(sqlMapClient)) { Field field = findSqlMapExecutorDelegate(sqlMapClient); field.setAccessible(true);/* w w w.j a va 2 s .c o m*/ this.sqlMapExecutorDelegate = (SqlMapExecutorDelegate) ReflectionUtils.getField(field, sqlMapClient); } else { throw new IllegalArgumentException("not found SqlMapExecutorDelegate in SqlMapClient."); } }
From source file:com.ace.erp.common.inject.support.InjectBaseDependencyHelper.java
/** * ??//w w w.j a va2 s . c o m * * @param target * @param annotation */ private static Set<Object> findDependencies(final Object target, final Class<? extends Annotation> annotation) { final Set<Object> candidates = Sets.newHashSet(); ReflectionUtils.doWithFields(target.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); Object obj = ReflectionUtils.getField(field, target); candidates.add(obj); } }, new ReflectionUtils.FieldFilter() { @Override public boolean matches(Field field) { return field.isAnnotationPresent(annotation); } }); ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(method); PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); candidates.add(ReflectionUtils.invokeMethod(descriptor.getReadMethod(), target)); } }, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method method) { boolean hasAnnotation = false; hasAnnotation = method.isAnnotationPresent(annotation); if (!hasAnnotation) { return false; } boolean hasReadMethod = false; PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); hasReadMethod = descriptor != null && descriptor.getReadMethod() != null; if (!hasReadMethod) { return false; } return true; } }); return candidates; }
From source file:org.web4thejob.module.CalendarModule.java
@Override public Collection<L10nString> getLocalizableStrings(final Set<Class> classes) { final Set<Class> localizableModuleClasses = new HashSet<Class>(); final List<L10nString> strings = new ArrayList<L10nString>(); //add here all module classes that need to display localizable resources on external panels localizableModuleClasses.add(CalendarSettingEnum.class); for (Class<?> clazz : localizableModuleClasses) { ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { @Override// w w w . jav a2 s . c om public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { strings.add((L10nString) field.get(null)); } }, new ReflectionUtils.FieldFilter() { @Override public boolean matches(Field field) { return ReflectionUtils.isPublicStaticFinal(field) && L10nString.class.equals(field.getType()) && classes.contains( ((L10nString) ReflectionUtils.getField(field, null)).getDeclaringClass()); } }); } return strings; }
From source file:com.ryantenney.metrics.spring.MetricAnnotationBeanPostProcessor.java
@Override protected void withField(Object bean, String beanName, Class<?> targetClass, Field field) { final Metric annotation = field.getAnnotation(Metric.class); final String metricName = Util.forMetricField(targetClass, field, annotation); final Class<?> type = field.getType(); if (!com.codahale.metrics.Metric.class.isAssignableFrom(type)) { throw new IllegalArgumentException("Field " + targetClass.getCanonicalName() + "." + field.getName() + " must be a subtype of " + com.codahale.metrics.Metric.class.getCanonicalName()); }/*from w ww . j av a 2s . com*/ ReflectionUtils.makeAccessible(field); // Get the value of the field annotated with @Metric com.codahale.metrics.Metric metric = (com.codahale.metrics.Metric) ReflectionUtils.getField(field, bean); if (metric == null) { // If null, create a metric of the appropriate type and inject it metric = getMetric(metrics, type, metricName); ReflectionUtils.setField(field, bean, metric); LOG.debug("Injected metric {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName()); } else { // If non-null, register that instance of the metric try { // Attempt to register that instance of the metric metrics.register(metricName, metric); LOG.debug("Registered metric {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName()); } catch (IllegalArgumentException ex1) { // A metric is already registered under that name // (Cannot determine the cause without parsing the Exception's message) try { metric = getMetric(metrics, type, metricName); ReflectionUtils.setField(field, bean, metric); LOG.debug("Injected metric {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName()); } catch (IllegalArgumentException ex2) { // A metric of a different type is already registered under that name throw new IllegalArgumentException("Error injecting metric for field " + targetClass.getCanonicalName() + "." + field.getName(), ex2); } } } }
From source file:com.ciphertool.zodiacengine.dao.CipherDaoTest.java
@Test public void testSetMongoTemplate() { CipherDao cipherDao = new CipherDao(); cipherDao.setMongoTemplate(mongoTemplateMock); Field mongoOperationsField = ReflectionUtils.findField(CipherDao.class, "mongoOperations"); ReflectionUtils.makeAccessible(mongoOperationsField); MongoOperations mongoOperationsFromObject = (MongoOperations) ReflectionUtils.getField(mongoOperationsField, cipherDao);/*from ww w . j av a 2s . c om*/ assertSame(mongoTemplateMock, mongoOperationsFromObject); }
From source file:org.sakuli.javaDSL.AbstractSakuliTest.java
private static Object getField(Object target, String name) { if (target != null) { Field field = ReflectionUtils.findField(target.getClass(), name); if (field != null) { ReflectionUtils.makeAccessible(field); return ReflectionUtils.getField(field, target); }//from www . j a va2 s .c om } return null; }
From source file:py.una.pol.karaku.dao.entity.interceptors.AbstractInterceptor.java
/** * Retorna el valor de un field de un bean dado. * /* w w w .ja va2s. c om*/ * <p> * Es til cuando se necesita obtener valores de otros fields del bean que * se esta interceptando. * </p> * * @param bean * del cual quitar el valor * @param field * nombre del atributo * @return valor extrado */ protected Object getFieldValueOfBean(@Nonnull Object bean, @Nonnull String field) { Field unique = ReflectionUtils.findField(bean.getClass(), field); unique.setAccessible(true); Object uniqueColumn = ReflectionUtils.getField(unique, bean); unique.setAccessible(false); return uniqueColumn; }
From source file:com.ciphertool.sentencebuilder.dao.UniqueWordListDaoTest.java
@Test @SuppressWarnings("unchecked") public void testConstructor() { WordDao wordDaoMock = mock(WordDao.class); when(wordDaoMock.findAllUniqueWords()).thenReturn(wordsToReturn); UniqueWordListDao uniqueWordListDao = new UniqueWordListDao(wordDaoMock); Field wordListField = ReflectionUtils.findField(UniqueWordListDao.class, "wordList"); ReflectionUtils.makeAccessible(wordListField); List<Word> wordListFromObject = (List<Word>) ReflectionUtils.getField(wordListField, uniqueWordListDao); assertEquals(3, wordListFromObject.size()); assertTrue(wordListFromObject.containsAll(wordsToReturn)); assertTrue(wordsToReturn.containsAll(wordListFromObject)); verify(wordDaoMock, times(1)).findAllUniqueWords(); }