List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.hengyi.japp.sap.convert.impl.SapConverts.java
private String getSapFieldName(PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); SapConvertField scf = readMethod.getAnnotation(SapConvertField.class); if (scf != null) { return StringUtils.upperCase(scf.value()); }//from w ww . j av a2 s. c o m if (readMethod.getAnnotation(SapTransient.class) != null) { return null; } String beanPropertyName = descriptor.getName(); for (Field field : beanClass.getDeclaredFields()) { if (!field.getName().equals(beanPropertyName)) { continue; } if (field.getAnnotation(SapTransient.class) != null) { return null; } scf = field.getAnnotation(SapConvertField.class); if (scf != null) { return StringUtils.upperCase(scf.value()); } } return StringUtils.upperCase(beanPropertyName); }
From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader.java
private void validateField(Field field) { Name annotation = field.getAnnotation(Name.class); if (annotation != null && Strings.isEmpty(annotation.value())) { throw new ConfigurationException(getBundle().getString("configuration-name-attribute-cant-be-empty"), new IllegalArgumentException()); }// w w w . ja v a2 s . c o m }
From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java
public AnnotatedCLI(Class... clazz) { final List<AnnotatedOption> list = new ArrayList<AnnotatedOption>(); for (Class aClass : clazz) { for (Field field : aClass.getDeclaredFields()) { Param param = field.getAnnotation(Param.class); if (param != null) { list.add(new AnnotatedOption(aClass, field, param)); }//from w ww.j a va2 s. c o m Args arg = field.getAnnotation(Args.class); if (arg != null) { args.put(aClass, field); } } for (Constructor ctor : aClass.getConstructors()) { AnnotatedConstructor actor = new AnnotatedConstructor(aClass, ctor); Class[] types = ctor.getParameterTypes(); Annotation[][] annotations = ctor.getParameterAnnotations(); for (int i = 0; i < types.length; i++) { Class type = types[i]; Annotation[] ann = annotations[i]; for (Annotation annotation : ann) { if (annotation instanceof Param) { actor.addParam((Param) annotation, type); } } } if (actor.isValid()) { ctors.put(aClass, actor); } } } options = new Options(); mappings = Multimaps.newMultimap(Maps.<Class, Collection<AnnotatedOption>>newHashMap(), new Supplier<Collection<AnnotatedOption>>() { public Collection<AnnotatedOption> get() { return new ArrayList<AnnotatedOption>(); } }); for (AnnotatedConstructor constructor : ctors.values()) { for (AnnotatedConstructor.AnnotatedParam param : constructor.getParams()) { boolean hasArgs = !(param.getType().equals(boolean.class) || param.getType().equals(Boolean.class)); String option = param.getParam().option(); while (options.hasOption(option)) { option = option + option; } options.addOption(option, param.getParam().name(), hasArgs, param.getParam().desc()); } } for (AnnotatedOption opt : list) { boolean hasArgs = !(opt.field.getType().equals(boolean.class) || opt.field.getType().equals(Boolean.class)); while (options.hasOption(opt.getOpt())) { opt.setOpt(opt.getOpt() + opt.getOpt()); } options.addOption(opt.getOpt(), opt.getName(), hasArgs, opt.getParam().desc()); mappings.put(opt.clazz, opt); } }
From source file:de.micromata.genome.util.runtime.config.AbstractLocalSettingsConfigModel.java
@Override public String findCommentForProperty(String localProperty) { Field field = PrivateBeanUtils.findField(getClass(), localProperty); if (field == null) { return null; }/* w w w. j a va2s. co m*/ ALocalSettingsPath asp = field.getAnnotation(ALocalSettingsPath.class); if (asp == null) { return null; } return asp.comment(); }
From source file:com.heisenberg.impl.plugin.Descriptors.java
public Descriptor createTypeDescriptor(Plugin plugin) { Descriptor descriptor = new Descriptor(); if (plugin instanceof DataType) { descriptor.setDataType((DataType) plugin); } else if (plugin instanceof ActivityType) { descriptor.setActivityType((ActivityType) plugin); } else if (plugin instanceof DataType) { descriptor.setDataType((DataType) plugin); }/* ww w . ja v a 2 s . c o m*/ Class<?> pluginClass = plugin.getClass(); List<Field> fields = Reflection.getNonStaticFieldsRecursive(pluginClass); if (!fields.isEmpty()) { List<DescriptorField> configurationFields = new ArrayList<DescriptorField>(fields.size()); descriptor.setConfigurationFields(configurationFields); for (Field field : fields) { ConfigurationField configurationField = field.getAnnotation(ConfigurationField.class); if (field.getAnnotation(ConfigurationField.class) != null) { Descriptor fieldDescriptor = getDataTypeDescriptor(field); DescriptorField descriptorField = new DescriptorField(field, fieldDescriptor.getDataType(), configurationField); configurationFields.add(descriptorField); } } } Label label = pluginClass.getAnnotation(Label.class); if (label != null) { descriptor.setLabel(label.value()); } return descriptor; }
From source file:com.quancheng.saluki.boot.runner.GrpcReferenceRunner.java
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Class<?> searchType = bean.getClass(); while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { SalukiReference reference = field.getAnnotation(SalukiReference.class); if (reference != null) { Object value = null; try { value = applicationContext.getBean(field.getType()); } catch (NoSuchBeanDefinitionException e) { value = null;//from w ww. j av a2 s. c o m } if (value == null) { value = refer(reference, field.getType()); } try { if (!field.isAccessible()) { field.setAccessible(true); } field.set(bean, value); } catch (Throwable e) { logger.error("Failed to init remote service reference at filed " + field.getName() + " in class " + bean.getClass().getName() + ", cause: " + e.getMessage(), e); } } } searchType = searchType.getSuperclass(); } return bean; }
From source file:de.hackerspacebremen.format.JSONFormatter.java
@Override public T reformat(final String object, final Class<T> entityClass) throws FormatException { T result = null;/*w w w .j av a 2s. c om*/ final Map<String, Field> fieldMap = new HashMap<String, Field>(); if (entityClass.isAnnotationPresent(Entity.class)) { final Field[] fields = entityClass.getDeclaredFields(); for (final Field field : fields) { if (field.isAnnotationPresent(FormatPart.class)) { fieldMap.put(field.getAnnotation(FormatPart.class).key(), field); } } try { final JSONObject json = new JSONObject(object); result = entityClass.newInstance(); for (final String key : fieldMap.keySet()) { if (json.has(key)) { final Field field = fieldMap.get(key); final Method method = entityClass.getMethod(this.getSetter(field.getName()), new Class<?>[] { field.getType() }); final String type = field.getType().toString(); if (type.equals("class com.google.appengine.api.datastore.Key")) { method.invoke(result, KeyFactory.stringToKey(json.getString(key))); } else if (type.equals("class com.google.appengine.api.datastore.Text")) { method.invoke(result, new Text(json.getString(key))); } else if (type.equals("boolean")) { method.invoke(result, json.getBoolean(key)); } else if (type.equals("long")) { method.invoke(result, json.getLong(key)); } else if (type.equals("int")) { method.invoke(result, json.getInt(key)); } else { method.invoke(result, json.get(key)); } } } } catch (JSONException e) { logger.warning("JSONException occured: " + e.getMessage()); throw new FormatException(); } catch (NoSuchMethodException e) { logger.warning("NoSuchMethodException occured: " + e.getMessage()); throw new FormatException(); } catch (SecurityException e) { logger.warning("SecurityException occured: " + e.getMessage()); throw new FormatException(); } catch (IllegalAccessException e) { logger.warning("IllegalAccessException occured: " + e.getMessage()); throw new FormatException(); } catch (IllegalArgumentException e) { logger.warning("IllegalArgumentException occured: " + e.getMessage()); throw new FormatException(); } catch (InvocationTargetException e) { logger.warning("InvocationTargetException occured: " + e.getMessage()); throw new FormatException(); } catch (InstantiationException e) { logger.warning("InstantiationException occured: " + e.getMessage()); throw new FormatException(); } } return result; }
From source file:com.vilt.minium.script.test.impl.MiniumRhinoTestsSupport.java
protected Object createTestClassInstance(final MiniumRhinoTestContextManager contextManager) { try {//from w w w .j ava 2 s. c o 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:br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader.java
private String getKeyByAnnotation(Field field) { String key = null;/*from w w w.j a v a 2 s .c o m*/ Name nameAnnotation = field.getAnnotation(Name.class); if (Strings.isEmpty(nameAnnotation.value())) { throw new ConfigurationException(bundle.getString("configuration-name-attribute-cant-be-empty")); } else { key = nameAnnotation.value(); } return key; }
From source file:me.henrytao.observableorm.orm.ObservableModel.java
public T deserialize(Map<String, Object> map) throws IllegalAccessException { Field[] fields = getDeclaredFields(); for (Field f : fields) { if (!f.isAnnotationPresent(Column.class)) { continue; }/*from w w w .j a va2 s . c o m*/ Column column = f.getAnnotation(Column.class); if (!column.deserialize()) { continue; } f.setAccessible(true); String name = column.name(); Object value = map.get(name); Class type = f.getType(); Deserializer deserializer = deserializerMap.get(type); if (deserializer != null) { f.set(this, deserializer.deserialize(value)); } else if (boolean.class.isAssignableFrom(type)) { f.setBoolean(this, value == null ? false : (Boolean) value); } else if (double.class.isAssignableFrom(type)) { f.setDouble(this, value == null ? 0.0 : (Double) value); } else if (float.class.isAssignableFrom(type)) { f.setFloat(this, value == null ? 0f : (Float) value); } else if (int.class.isAssignableFrom(type)) { f.setInt(this, value == null ? 0 : (int) value); } else if (short.class.isAssignableFrom(type)) { f.setShort(this, value == null ? 0 : (short) value); } else if (byte.class.isAssignableFrom(type)) { f.setByte(this, value == null ? 0 : (byte) value); } else if (String.class.isAssignableFrom(type)) { f.set(this, value); } else if (JSONObject.class.isAssignableFrom(type)) { // todo: nested object should be another model } } return (T) this; }