List of usage examples for org.springframework.util ReflectionUtils getField
@Nullable public static Object getField(Field field, @Nullable Object target)
From source file:cn.guoyukun.spring.utils.AopProxyUtils.java
/** * ??/*from w ww . ja v a 2 s .c om*/ * see http://jinnianshilongnian.iteye.com/blog/1894465 * @param proxy * @return */ public static boolean isMultipleProxy(Object proxy) { try { ProxyFactory proxyFactory = null; if (AopUtils.isJdkDynamicProxy(proxy)) { proxyFactory = findJdkDynamicProxyFactory(proxy); } if (AopUtils.isCglibProxy(proxy)) { proxyFactory = findCglibProxyFactory(proxy); } TargetSource targetSource = (TargetSource) ReflectionUtils.getField(ProxyFactory_targetSource_FIELD, proxyFactory); return AopUtils.isAopProxy(targetSource.getTarget()); } catch (Exception e) { throw new IllegalArgumentException( "proxy args maybe not proxy with cglib or jdk dynamic proxy. this method not support", e); } }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.CompositeIdHashAndRangeKeyExtractor.java
@SuppressWarnings("unchecked") @Override//from w w w . ja v a2 s.co m public H getHashKey(ID id) { Method method = hashAndRangeKeyMethodExtractor.getHashKeyMethod(); if (method != null) { return (H) ReflectionUtils.invokeMethod(method, id); } else { return (H) ReflectionUtils.getField(hashAndRangeKeyMethodExtractor.getHashKeyField(), id); } }
From source file:net.prasenjit.auth.validation.FieldMatchValidator.java
/** {@inheritDoc} */ @Override/*from w ww. j a v a 2 s.c om*/ public boolean isValid(final Object value, final ConstraintValidatorContext context) { try { Field field = ReflectionUtils.findField(value.getClass(), firstFieldName); ReflectionUtils.makeAccessible(field); final Object firstObj = ReflectionUtils.getField(field, value); field = ReflectionUtils.findField(value.getClass(), secondFieldName); ReflectionUtils.makeAccessible(field); final Object secondObj = ReflectionUtils.getField(field, value); return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj); } catch (final Exception ignore) { // ignore } return true; }
From source file:org.socialsignin.spring.data.dynamodb.repository.support.CompositeIdHashAndRangeKeyExtractor.java
@Override public Object getRangeKey(ID id) { Method method = hashAndRangeKeyMethodExtractor.getRangeKeyMethod(); if (method != null) { return ReflectionUtils.invokeMethod(method, id); } else {//from w ww . ja v a 2 s. c o m return ReflectionUtils.getField(hashAndRangeKeyMethodExtractor.getRangeKeyField(), id); } }
From source file:com.ryantenney.metrics.spring.GaugeFieldAnnotationBeanPostProcessor.java
@Override protected void withField(final Object bean, String beanName, Class<?> targetClass, final Field field) { ReflectionUtils.makeAccessible(field); final Gauge annotation = field.getAnnotation(Gauge.class); final String metricName = Util.forGauge(targetClass, field, annotation); metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() { @Override// w w w.j a v a 2 s .c o m public Object getValue() { Object value = ReflectionUtils.getField(field, bean); if (value instanceof com.codahale.metrics.Gauge) { value = ((com.codahale.metrics.Gauge<?>) value).getValue(); } return value; } }); LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName()); }
From source file:py.una.pol.karaku.dao.entity.interceptors.BigDecimalInterceptor.java
@Override public void intercept(Operation o, Field field, Object bean) { BigDecimal value = (BigDecimal) ReflectionUtils.getField(field, bean); if (value == null) { return;// w w w . j a va 2 s .co m } if (value.longValue() < 1) { value = value.add(BigDecimal.ONE); } BigDecimal trailed = value.stripTrailingZeros(); int precision = trailed.scale(); if (precision > MAXIMUM_PRECISION) { throw new KarakuRuntimeException( String.format("Attribute '%s' of bean '%s' has a precision of {%d}, maximum allowed is {%d}", field.getName(), bean.getClass().getSimpleName(), precision, MAXIMUM_PRECISION)); } }
From source file:com.ciphertool.sentencebuilder.etl.parsers.FrequencyFileParserTest.java
@Test public void testSetFilename() { String fileNameToSet = "arbitraryFileName"; FrequencyFileParser frequencyFileParser = new FrequencyFileParser(); frequencyFileParser.setFileName(fileNameToSet); Field fileNameField = ReflectionUtils.findField(FrequencyFileParser.class, "fileName"); ReflectionUtils.makeAccessible(fileNameField); String fileNameFromObject = (String) ReflectionUtils.getField(fileNameField, frequencyFileParser); assertSame(fileNameToSet, fileNameFromObject); }
From source file:com.dianping.avatar.cache.util.CacheAnnotationUtils.java
/** * Retrieve the cache key values from entity instance */// www .j av a 2s . com public static Object[] getCacheKeyValues(Object entity) { if (entity == null) { throw new IllegalArgumentException("Entity is null."); } Class<?> cz = entity.getClass(); Cache cache = cz.getAnnotation(Cache.class); if (cache == null) { throw new SystemException("The entity must be annotated by Cache."); } Field[] fields = ClassUtils.getDeclaredFields(cz); final List<OrderedField> cacheFields = new ArrayList<OrderedField>(); // Extract annotated fields for (int i = 0; i < fields.length; i++) { Field f = fields[i]; CacheParam fCache = f.getAnnotation(CacheParam.class); if (fCache != null) { cacheFields.add(new OrderedField(f, i, fCache.order())); } } // Extract declared fields for (int i = 0; i < cache.fields().length; i++) { String fieldName = cache.fields()[i]; if (fieldName.isEmpty()) { continue; } Field f = ReflectionUtils.findField(cz, fieldName); if (f == null) { throw new IllegalArgumentException( "Invalid cahce parameter " + fieldName + ", the filed is not exists."); } cacheFields.add(new OrderedField(f, i, -Integer.MAX_VALUE + i)); } Collections.sort(cacheFields); Object[] values = new Object[cacheFields.size()]; for (int i = 0; i < cacheFields.size(); i++) { OrderedField oField = cacheFields.get(i); ReflectionUtils.makeAccessible((Field) oField.field); values[i] = ReflectionUtils.getField((Field) oField.field, entity); } return values; }
From source file:com.ciphertool.sentencebuilder.etl.parsers.PartOfSpeechFileParserTest.java
@Test public void testSetFilename() { String fileNameToSet = "arbitraryFileName"; PartOfSpeechFileParser partOfSpeechFileParser = new PartOfSpeechFileParser(); partOfSpeechFileParser.setFileName(fileNameToSet); Field fileNameField = ReflectionUtils.findField(PartOfSpeechFileParser.class, "fileName"); ReflectionUtils.makeAccessible(fileNameField); String fileNameFromObject = (String) ReflectionUtils.getField(fileNameField, partOfSpeechFileParser); assertSame(fileNameToSet, fileNameFromObject); }
From source file:com.javaetmoi.core.spring.vfs.Vfs2ResourceHandlerRegistration.java
@Override protected ResourceHttpRequestHandler getRequestHandler() { Field locationsField = ReflectionUtils.findField(ResourceHandlerRegistration.class, "locations"); ReflectionUtils.makeAccessible(locationsField); @SuppressWarnings("unchecked") List<Resource> locations = (List<Resource>) ReflectionUtils.getField(locationsField, this); Field cachePeriodField = ReflectionUtils.findField(ResourceHandlerRegistration.class, "cachePeriod"); ReflectionUtils.makeAccessible(cachePeriodField); Integer cachePeriod = (Integer) ReflectionUtils.getField(cachePeriodField, this); // Initial code is replace by a new Vfs2ResourceHttpRequestHandler() Assert.isTrue(!CollectionUtils.isEmpty(locations), "At least one location is required for resource handling."); ResourceHttpRequestHandler requestHandler = new Vfs2ResourceHttpRequestHandler(); requestHandler.setLocations(locations); if (cachePeriod != null) { requestHandler.setCacheSeconds(cachePeriod); }//from ww w . j av a 2s.c o m return requestHandler; }