List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:edu.usu.sdl.openstorefront.validation.MinValueRule.java
@Override protected boolean validate(Field field, Object dataObject) { boolean valid = true; Min min = field.getAnnotation(Min.class); if (min != null) { try {//from w w w . j a v a 2s . c o m String value = BeanUtils.getProperty(dataObject, field.getName()); if (value != null) { try { BigDecimal numberValue = new BigDecimal(value); if (numberValue.compareTo(BigDecimal.valueOf(min.value())) == -1) { valid = false; } } catch (NumberFormatException e) { throw new OpenStorefrontRuntimeException("This annotation is for numbers only", "Programming error"); } } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.", ex); } } return valid; }
From source file:be.fedict.eid.dss.ws.ServiceConsumerInstanceResolver.java
private void injectServices(T endpoint, SignatureVerificationService signatureVerificationService, DocumentService documentService) { LOG.debug("injecting services into JAX-WS endpoint..."); Field[] fields = endpoint.getClass().getDeclaredFields(); for (Field field : fields) { EJB ejbAnnotation = field.getAnnotation(EJB.class); if (null == ejbAnnotation) { continue; }/*from www .j a v a2 s . co m*/ if (field.getType().equals(SignatureVerificationService.class)) { field.setAccessible(true); try { field.set(endpoint, signatureVerificationService); } catch (Exception e) { throw new RuntimeException("injection error: " + e.getMessage(), e); } } else if (field.getType().equals(DocumentService.class)) { field.setAccessible(true); try { field.set(endpoint, documentService); } catch (Exception e) { throw new RuntimeException("injection error: " + e.getMessage(), e); } } } }
From source file:cn.sel.dvw.SimpleObjectRowMapper.java
@Override public T mapRow(ResultSet resultSet, int rowNum) throws SQLException { T rowObj;//from w w w . j a va 2 s . co m try { rowObj = clazz.getConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { e.printStackTrace(); throw new SQLException("Failed to instantiate the result object!", e); } Field[] fields = clazz.getFields(); for (Field field : fields) { MProperty annotation = field.getAnnotation(MProperty.class); String fieldName = field.getName(); String columnName = null; boolean useSetter = false; if (annotation != null) { columnName = annotation.db_col_name(); useSetter = annotation.useSetter(); } if (columnName == null || columnName.isEmpty()) { columnName = fieldName; } Object value = resultSet.getObject(columnName); if (value != null) { try { if (useSetter) { PropertyUtils.setSimpleProperty(rowObj, fieldName, value); } else { field.setAccessible(true); field.set(rowObj, value); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); throw new SQLException(String.format("Failed to set value for property '%s'!", fieldName)); } } } return rowObj; }
From source file:com.github.jiahut.demo.utils.LoggerPostProcessor.java
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { List<Field> fields = Arrays.asList(bean.getClass().getDeclaredFields()); for (Field field : fields) { if (Logger.class.isAssignableFrom(field.getType()) && field.getAnnotation(InjectLogger.class) != null) { logger.debug("Attempting to inject a SLF4J logger on bean: " + bean.getClass()); if (field != null && (field.getModifiers() & Modifier.STATIC) == 0) { field.setAccessible(true); try { field.set(bean, LoggerFactory.getLogger(bean.getClass())); logger.debug("Successfully injected a SLF4J logger on bean: " + bean.getClass()); } catch (IllegalArgumentException e) { logger.warn("Could not inject logger for class: " + bean.getClass(), e); } catch (IllegalAccessException e) { logger.warn("Could not inject logger for class: " + bean.getClass(), e); }/*w w w. j av a 2s .co m*/ } } } return bean; }
From source file:com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java
@Override public void delete(Object o, Field field) throws SerializerException { final Property property = field.getAnnotation(Property.class); if ((null != property) && (property.cascadeDelete() == true)) { throw new SerializerException("Not implemented"); }/*from ww w. java2 s . co m*/ }
From source file:com.hlex.ondb.entity.AnnoEntityHelper.java
/** * Seek for @Major key and create//from w ww .j a v a2s . com * "fieldname/fieldvalue/fieldname/fieldvalue/" * * * @param type FindingAnnoation Target : MajorKey or MinorKey * @return * @throws com.hlex.ondb.exception.NullKeyException */ public List<String> getKeyByAnnotation(Object o, Class<? extends Annotation> type) throws NullKeyException { //returned variable List<String> key = new ArrayList(); //field all field for @MajorKey Field[] fs = FieldUtils.getAllFields(o.getClass()); for (Field f : fs) { Annotation mjk = f.getAnnotation(type); Object value = null; if (mjk != null) { try { value = FieldUtils.readField(o, f.getName(), true); //null value case if (value == null) { throw new NullKeyException(f.getName() + " has null value"); } //add /fieldname/fieldvalue key.add(f.getName()); key.add(value.toString()); } catch (IllegalAccessException ex) { Logger.getLogger(AnnoEntityHelper.class.getName()).log(Level.SEVERE, null, ex); } } } //no @majorkey case if (key.isEmpty()) { throw new NullKeyException("no " + type.getSimpleName() + " key"); } return key; }
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//www. j a v a2 s .c om 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:almira.sample.util.LogPostProcessor.java
@Override public Object postProcessBeforeInitialization(final Object bean, String beanName) { ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() { @Override//from w ww .j a v a2s .c o m public void doWith(Field field) throws IllegalAccessException { ReflectionUtils.makeAccessible(field); if (field.getAnnotation(Log.class) != null) { Logger logger = LoggerFactory.getLogger(bean.getClass().getName()); field.set(bean, logger); } } }); return bean; }
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); }/*www . ja va 2 s . c om*/ } } }); return bean; }
From source file:com.threewks.thundr.introspection.ClassIntrospector.java
public <T> List<Field> listInjectionFields(Class<T> type) { List<Field> injectionFields = new ArrayList<Field>(); if (supportsInjection) { Field[] fields = ReflectUtil.getSupportedFields(type); for (Field field : fields) { boolean shouldInject = field.getAnnotation(Inject.class) != null; if (shouldInject) { injectionFields.add(field); }/*from w w w .j a va 2s .c om*/ } } return injectionFields; }