List of usage examples for java.lang.reflect AnnotatedElement getAnnotation
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
From source file:org.guiceyfruit.spring.support.AutowiredMemberProvider.java
/** * Returns a new filter on the given member to respect the use of {@link Qualifier} annotations or * annotations annotated with {@link Qualifier} *//* w ww .ja v a 2 s .c o m*/ protected Predicate<Binding> createQualifierFilter(Member member, Annotation[] parameterAnnotations) { if (member instanceof AnnotatedElement) { AnnotatedElement annotatedElement = (AnnotatedElement) member; final Qualifier qualifier = annotatedElement.getAnnotation(Qualifier.class); if (qualifier != null) { final String expectedValue = qualifier.value(); final boolean notEmptyValue = Strings.isNotEmpty(expectedValue); return new Predicate<Binding>() { public boolean matches(Binding binding) { String value = annotationName(binding); // we cannot use @Qualified as a binding annotation // so we can't test for just a @Qualified binding with no text // so lets just test for a non-empty string if (notEmptyValue) { return Comparators.equal(expectedValue, value); } else { return Strings.isNotEmpty(value); } } @Override public String toString() { return "@Autowired @Qualifier(" + expectedValue + ")"; } }; } // lets iterate through all of the annotations looking for a qualifier Set<Annotation> qualifiedAnnotations = Sets.newHashSet(); Annotation[] annotations = annotatedElement.getAnnotations(); for (Annotation annotation : annotations) { if (isQualified(annotation)) { qualifiedAnnotations.add(annotation); } } if (parameterAnnotations != null) { for (Annotation annotation : parameterAnnotations) { if (isQualified(annotation)) { qualifiedAnnotations.add(annotation); } } } int size = qualifiedAnnotations.size(); if (size == 1) { final Annotation annotation = Iterables.getOnlyElement(qualifiedAnnotations); return new Predicate<Binding>() { public boolean matches(Binding binding) { Annotation actualAnnotation = binding.getKey().getAnnotation(); return actualAnnotation != null && actualAnnotation.equals(annotation); } @Override public String toString() { return "@Autowired " + annotation; } }; } else if (size > 0) { throw new ProvisionException("Too many qualified annotations " + qualifiedAnnotations + " when trying to inject " + member); } } return new Predicate<Binding>() { public boolean matches(Binding binding) { return true; } @Override public String toString() { return "@Autowired"; } }; }
From source file:com.haulmont.cuba.core.sys.MetadataLoader.java
protected void postProcessProperty(MetaClass metaClass, MetaProperty metaProperty) { // init inverse properties MetaProperty inverseProp = metaProperty.getInverse(); if (inverseProp != null && inverseProp.getInverse() == null) { ((MetaPropertyImpl) inverseProp).setInverse(metaProperty); }/*from w ww .ja v a 2s .co m*/ if (metaProperty.getRange() == null || !metaProperty.getRange().isClass()) return; AnnotatedElement annotatedElement = metaProperty.getAnnotatedElement(); OnDelete onDelete = annotatedElement.getAnnotation(OnDelete.class); if (onDelete != null) { Map<String, Object> metaAnnotations = metaClass.getAnnotations(); MetaProperty[] properties = (MetaProperty[]) metaAnnotations.get(OnDelete.class.getName()); properties = (MetaProperty[]) ArrayUtils.add(properties, metaProperty); metaAnnotations.put(OnDelete.class.getName(), properties); } OnDeleteInverse onDeleteInverse = annotatedElement.getAnnotation(OnDeleteInverse.class); if (onDeleteInverse != null) { Map<String, Object> metaAnnotations = metaProperty.getRange().asClass().getAnnotations(); MetaProperty[] properties = (MetaProperty[]) metaAnnotations.get(OnDeleteInverse.class.getName()); properties = (MetaProperty[]) ArrayUtils.add(properties, metaProperty); metaAnnotations.put(OnDeleteInverse.class.getName(), properties); } }
From source file:com.medallia.spider.api.DynamicInputImpl.java
private static Object parseSingleValue(Class<?> rt, String v, AnnotatedElement anno, Map<Class<?>, InputArgParser<?>> inputArgParsers) { if (rt.isEnum()) { String vlow = v.toLowerCase(); for (Enum e : rt.asSubclass(Enum.class).getEnumConstants()) { if (e.name().toLowerCase().equals(vlow)) return e; }// w w w . j a v a 2 s .com throw new AssertionError("Enum constant not found: " + v); } else if (rt == Integer.class) { // map blank strings to null return Strings.hasContent(v) ? Integer.valueOf(v) : null; } else if (rt == Integer.TYPE) { // primitive int must have a value return Integer.valueOf(v); } else if (rt == Long.class) { // map blank strings to null return Strings.hasContent(v) ? Long.valueOf(v) : null; } else if (rt == Long.TYPE) { // primitive long must have a value return Long.valueOf(v); } else if (rt == Double.class) { // map blank strings to null return Strings.hasContent(v) ? Double.valueOf(v) : null; } else if (rt == Double.TYPE) { // primitive double must have a value return Double.valueOf(v); } else if (rt == String.class) { return v; } else if (rt.isArray()) { Input.List ann = anno.getAnnotation(Input.List.class); if (ann == null) throw new AssertionError("Array type but no annotation (see " + Input.class + "): " + anno); String separator = ann.separator(); String[] strVals = v.split(separator, -1); Class<?> arrayType = rt.getComponentType(); Object a = Array.newInstance(arrayType, strVals.length); for (int i = 0; i < strVals.length; i++) { Array.set(a, i, parseSingleValue(arrayType, strVals[i], anno, inputArgParsers)); } return a; } else if (inputArgParsers != null) { InputArgParser<?> argParser = inputArgParsers.get(rt); if (argParser != null) { return argParser.parse(v); } } throw new AssertionError("Unknown return type " + rt + " (val: " + v + ")"); }
From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java
private void loadPropertyAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) { SystemLevel systemLevel = annotatedElement.getAnnotation(SystemLevel.class); if (systemLevel != null) { metaProperty.getAnnotations().put(SystemLevel.class.getName(), systemLevel.value()); metaProperty.getAnnotations().put(SystemLevel.class.getName() + SystemLevel.PROPAGATE, systemLevel.propagate()); }/*from www .j a v a 2 s . com*/ IgnoreUserTimeZone ignoreUserTimeZone = annotatedElement.getAnnotation(IgnoreUserTimeZone.class); if (ignoreUserTimeZone != null) { metaProperty.getAnnotations().put(IgnoreUserTimeZone.class.getName(), ignoreUserTimeZone.value()); } com.haulmont.chile.core.annotations.MetaProperty metaPropertyAnnotation = annotatedElement .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class); if (metaPropertyAnnotation != null) { String[] related = metaPropertyAnnotation.related(); if (!(related.length == 1 && related[0].equals(""))) { metaProperty.getAnnotations().put("relatedProperties", Joiner.on(',').join(related)); } } }
From source file:com.medsphere.fileman.FMRecord.java
private boolean setValue(AnnotatedElement ele, String value) { FMAnnotateFieldInfo annote = ele.getAnnotation(FMAnnotateFieldInfo.class); FIELDTYPE fieldType = annote.fieldType(); boolean retVal = true; Object obj = null;/* w ww . j a va2 s .c o m*/ try { if (value != null && !value.equals("")) { switch (fieldType) { case DATE: { obj = FMUtil.fmDateToDate(value); break; } case POINTER_TO_FILE: { obj = Integer.valueOf(value); break; } case NUMERIC: { obj = Double.valueOf(value); break; } default: { obj = value; break; } } } if (ele instanceof Field) { Field field = (Field) ele; if (valuesChanged(field.get(this), obj)) { field.set(this, obj); } } else if (ele instanceof Method) { Method method = (Method) ele; if (valuesChanged(method.getDefaultValue(), obj)) { invokeSetter(ele, method, obj); } } addModifiedField(annote.number()); } catch (IllegalAccessException e) { // Bad but not overall fatal, so keep going. retVal = false; } catch (ParseException e) { // Bad but not overall fatal, so keep going. retVal = false; } catch (NumberFormatException e) { // Bad but not overall fatal, so keep going. retVal = false; } finally { // Empty block. Don't throw exceptions out of here. } return retVal; }
From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java
@Nullable protected Datatype getDatatype(AnnotatedElement annotatedElement) { com.haulmont.chile.core.annotations.MetaProperty annotation = annotatedElement .getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class); return annotation != null && !annotation.datatype().equals("") ? Datatypes.get(annotation.datatype()) : null;/*from w w w . ja v a 2 s. c o m*/ }
From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java
protected void assignPropertyType(AnnotatedElement field, MetaProperty property, Range range) { if (range.isClass()) { Composition composition = field.getAnnotation(Composition.class); if (composition != null) { ((MetaPropertyImpl) property).setType(MetaProperty.Type.COMPOSITION); } else {/*from ww w .ja va2 s .co m*/ ((MetaPropertyImpl) property).setType(MetaProperty.Type.ASSOCIATION); } } else if (range.isDatatype()) { ((MetaPropertyImpl) property).setType(MetaProperty.Type.DATATYPE); } else if (range.isEnum()) { ((MetaPropertyImpl) property).setType(MetaProperty.Type.ENUM); } else { throw new UnsupportedOperationException(); } }
From source file:org.jdal.ui.ViewSupport.java
/** * Bind controls following the same name convention or annotated with Property annotation. *//*from w ww. ja v a2s. com*/ public void autobind() { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(getModel()); PropertyAccessor viewPropertyAccessor = new DirectFieldAccessor(this); // Parse Property annotations List<AnnotatedElement> elements = AnnotatedElementAccessor.findAnnotatedElements(Property.class, getClass()); for (AnnotatedElement ae : elements) { Property p = ae.getAnnotation(Property.class); InitializationConfig config = getInitializationConfig(ae.getAnnotation(Initializer.class)); bindAndInitializeControl(p.value(), viewPropertyAccessor.getPropertyValue(((Field) ae).getName()), config); this.ignoredProperties.add(p.value()); } // Iterate on model properties for (PropertyDescriptor pd : bw.getPropertyDescriptors()) { String propertyName = pd.getName(); if (!ignoredProperties.contains(propertyName) && viewPropertyAccessor.isReadableProperty(propertyName)) { Object control = viewPropertyAccessor.getPropertyValue(propertyName); if (control != null) { if (log.isDebugEnabled()) log.debug("Found control: " + control.getClass().getSimpleName() + " for property: " + propertyName); TypeDescriptor descriptor = viewPropertyAccessor.getPropertyTypeDescriptor(propertyName); InitializationConfig config = getInitializationConfig( descriptor.getAnnotation(Initializer.class)); // do bind bindAndInitializeControl(propertyName, control, config); } } } }
From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java
protected Class getTypeOverride(AnnotatedElement element) { Temporal temporal = element.getAnnotation(Temporal.class); if (temporal != null && temporal.value().equals(TemporalType.DATE)) return java.sql.Date.class; else if (temporal != null && temporal.value().equals(TemporalType.TIME)) return java.sql.Time.class; else// w w w.j av a 2s. c om return null; }