List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.flipkart.polyguice.dropwiz.DropConfigProvider.java
private Object getValueFromFields(String path, Class<?> type, Object inst) throws Exception { Field[] fields = type.getDeclaredFields(); for (Field field : fields) { JsonProperty ann = field.getAnnotation(JsonProperty.class); if (ann != null) { String annName = ann.value(); if (StringUtils.isBlank(annName)) { annName = ann.defaultValue(); }//from w w w.java2s. co m if (StringUtils.isBlank(annName)) { annName = field.getName(); } if (StringUtils.equals(path, annName)) { boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } Object value = field.get(inst); if (!accessible) { field.setAccessible(false); } return value; } } } return null; }
From source file:com.graphaware.importer.cache.BaseCaches.java
/** * {@inheritDoc}//from ww w. ja va 2 s .c o m */ @Override public void inject(Importer importer) { Assert.notNull(importer); for (Field field : ReflectionUtils.getAllFields(importer.getClass())) { if (field.getAnnotation(InjectCache.class) != null) { field.setAccessible(true); try { field.set(importer, getCache(field.getAnnotation(InjectCache.class).name())); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } }
From source file:com.graphaware.importer.cache.BaseCaches.java
/** * {@inheritDoc}//from w w w . j a v a2 s . c om */ @Override public Set<String> createdCaches(Importer importer) { Assert.notNull(importer); Set<String> result = new HashSet<>(); for (Field field : ReflectionUtils.getAllFields(importer.getClass())) { if (field.getAnnotation(InjectCache.class) != null && field.getAnnotation(InjectCache.class).creator()) { result.add(field.getAnnotation(InjectCache.class).name()); } } return result; }
From source file:com.googlesource.gerrit.plugins.rabbitmq.config.AMQProperties.java
public AMQProperties(PluginProperties properties) { this.message = properties.getSection(Message.class); this.headers = new HashMap<>(); for (Section section : properties.getSections()) { for (Field f : section.getClass().getFields()) { if (f.isAnnotationPresent(MessageHeader.class)) { MessageHeader mh = f.getAnnotation(MessageHeader.class); try { Object value = f.get(section); if (value == null) { continue; }//from www. ja va 2s . c o m switch (f.getType().getSimpleName()) { case "String": headers.put(mh.value(), value.toString()); break; case "Integer": headers.put(mh.value(), Integer.valueOf(value.toString())); break; case "Long": headers.put(mh.value(), Long.valueOf(value.toString())); break; case "Boolean": headers.put(mh.value(), Boolean.valueOf(value.toString())); break; default: break; } } catch (IllegalAccessException | IllegalArgumentException ex) { LOGGER.warn("Cannot access field {}. Cause: {}", f.getName(), ex.getMessage()); } } } } }
From source file:eu.artofcoding.beetlejuice.spring.Slf4jPostProcessor.java
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() { @SuppressWarnings("unchecked") public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); // Check if the field is annoted with @Slf4j if (field.getAnnotation(Slf4j.class) != null) { logger.debug("Injecting Logger into " + beanName + "/" + bean.getClass()); //Slf4j slf4jAnnotation = field.getAnnotation(Slf4j.class); Logger logger = LoggerFactory.getLogger(bean.getClass()); field.set(bean, logger); }// w w w .ja v a 2 s . c om } }); return bean; }
From source file:com.googlecode.dex2jar.tools.BaseCmd.java
protected void initOptionFromClass(Class<?> clz) { if (clz == null) { return;//from w w w. jav a 2 s . co m } else { initOptionFromClass(clz.getSuperclass()); } Field[] fs = clz.getDeclaredFields(); for (Field f : fs) { Opt opt = f.getAnnotation(Opt.class); if (opt != null) { f.setAccessible(true); if (!opt.hasArg()) { Class<?> type = f.getType(); if (!type.equals(boolean.class)) { throw new RuntimeException( "the type of " + f + " must be boolean, as it is declared as no args"); } boolean b; try { b = (Boolean) f.get(this); } catch (Exception e) { throw new RuntimeException(e); } if (b) { throw new RuntimeException( "the value of " + f + " must be false, as it is declared as no args"); } } Option option = new Option(opt.opt(), opt.hasArg(), opt.description()); option.setRequired(opt.required()); if (!"".equals(opt.longOpt())) { option.setLongOpt(opt.longOpt()); } if (!"".equals(opt.argName())) { option.setArgName(opt.argName()); } options.addOption(option); map.put(opt.opt(), f); } } }
From source file:edu.usu.sdl.openstorefront.validation.ForeignKeyRule.java
@Override protected boolean validate(Field field, Object dataObject) { boolean valid = true; if (dataObject != null) { FK fk = field.getAnnotation(FK.class); if (fk != null) { if (fk.enforce()) { try { String value = BeanUtils.getProperty(dataObject, field.getName()); if (value != null) { Class fkClass = fk.value(); Service serviceProxy = ServiceProxyFactory.getServiceProxy(); if (StringUtils.isNotBlank(fk.referencedField())) { Object referenceEntity = fkClass.newInstance(); Field referenceField = ReflectionUtil.getField(referenceEntity, fk.referencedField()); if (referenceField != null) { if (referenceEntity instanceof BaseEntity) { referenceField.setAccessible(true); referenceField.set(referenceEntity, value); Object entity = ((BaseEntity) referenceEntity).find(); if (entity == null) { valid = false; }//from ww w .j a va2s.co m } else { throw new OpenStorefrontRuntimeException( "Reference Class is not a Base Entity: " + fkClass.getName()); } } else { throw new OpenStorefrontRuntimeException("Reference field: " + fk.referencedField() + " not on Class: " + fkClass.getSimpleName(), "check code"); } } else { Object entity = serviceProxy.getPersistenceService().findById(fkClass, value); if (entity == null) { valid = false; } } } } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) { throw new OpenStorefrontRuntimeException( "Unexpected error occur trying get original field value.", ex); } } } } return valid; }
From source file:com.ls.http.base.handler.MultipartRequestHandler.java
private void formMultipartEntityObject(Object source) { Class<?> currentClass = source.getClass(); while (!Object.class.equals(currentClass)) { Field[] fields = currentClass.getDeclaredFields(); for (int counter = 0; counter < fields.length; counter++) { Field field = fields[counter]; Expose expose = field.getAnnotation(Expose.class); if (expose != null && !expose.deserialize() || Modifier.isTransient(field.getModifiers())) { continue;// We don't have to copy ignored fields. }// w w w. jav a2 s. c o m field.setAccessible(true); Object value; String name; SerializedName serializableName = field.getAnnotation(SerializedName.class); if (serializableName != null) { name = serializableName.value(); } else { name = field.getName(); } try { value = field.get(source); addEntity(name, value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } currentClass = currentClass.getSuperclass(); } httpentity = entity.build(); }
From source file:com.cognifide.qa.bb.aem.ui.DialogFieldProvider.java
/** * Looks for DialogField annotation, reads locator data from the annotation instance * and asks AemDialogFieldResolver to produce a dialog field instance for the class field. *///from w w w. j a v a 2 s .com @Override public Optional<Object> provideValue(Object pageObject, Field field, PageObjectContext context) { AemDialogFieldResolver aemDialogFieldResolver = fieldResolverProvider.get(); DialogField dialogFieldAnnotation = field.getAnnotation(DialogField.class); String searchBy; Class<?> type = field.getType(); Object dialogField = null; if (StringUtils.isNotEmpty(dialogFieldAnnotation.label())) { searchBy = dialogFieldAnnotation.label(); dialogField = aemDialogFieldResolver.getField(searchBy, type); } else if (StringUtils.isNotEmpty(dialogFieldAnnotation.css())) { searchBy = dialogFieldAnnotation.css(); dialogField = aemDialogFieldResolver.getFieldByCss(searchBy, type); } else if (StringUtils.isNotEmpty(dialogFieldAnnotation.name())) { searchBy = dialogFieldAnnotation.name(); dialogField = aemDialogFieldResolver.getFieldByName(searchBy, type); } else if (StringUtils.isNotEmpty(dialogFieldAnnotation.xpath())) { searchBy = dialogFieldAnnotation.xpath(); dialogField = aemDialogFieldResolver.getFieldByXpath(searchBy, type); } return Optional.ofNullable(dialogField); }
From source file:com.ripariandata.timberwolf.conf4j.ConfigFileParser.java
public ConfigFileParser(final Object bean) { for (Class c = bean.getClass(); c != null; c = c.getSuperclass()) { for (Field f : c.getDeclaredFields()) { ConfigEntry entry = f.getAnnotation(ConfigEntry.class); if (entry != null) { fields.put(entry.name(), new FieldSetter(bean, f)); entries.add(entry);//w w w .j av a2 s . c o m } } } }