List of usage examples for java.lang.annotation ElementType CONSTRUCTOR
ElementType CONSTRUCTOR
To view the source code for java.lang.annotation ElementType CONSTRUCTOR.
Click Source Link
From source file:org.apache.bval.jsr.JsrMetaBeanFactory.java
/** * Add cascade validation and constraints from xml mappings * // www . jav a 2 s . c o m * @param beanClass * @param metabean * @throws IllegalAccessException * @throws InvocationTargetException */ private void addXmlConstraints(Class<?> beanClass, MetaBean metabean) throws IllegalAccessException, InvocationTargetException { for (final MetaConstraint<?, ? extends Annotation> metaConstraint : factory.getMetaConstraints(beanClass)) { Meta meta; AccessStrategy access = metaConstraint.getAccessStrategy(); boolean create = false; if (access == null) { // class level meta = null; } else if (access.getElementType() == ElementType.METHOD && !metaConstraint.getMember().getName().startsWith("get")) { // TODO: better getter test final Method method = Method.class.cast(metaConstraint.getMember()); meta = metabean.getMethod(method); final MetaMethod metaMethod; if (meta == null) { meta = new MetaMethod(metabean, method); metaMethod = MetaMethod.class.cast(meta); metabean.addMethod(method, metaMethod); } else { metaMethod = MetaMethod.class.cast(meta); } final Integer index = metaConstraint.getIndex(); if (index != null && index >= 0) { MetaParameter param = metaMethod.getParameter(index); if (param == null) { param = new MetaParameter(metaMethod, index); metaMethod.addParameter(index, param); } param.addAnnotation(metaConstraint.getAnnotation()); } else { metaMethod.addAnnotation(metaConstraint.getAnnotation()); } continue; } else if (access.getElementType() == ElementType.CONSTRUCTOR) { final Constructor<?> constructor = Constructor.class.cast(metaConstraint.getMember()); meta = metabean.getConstructor(constructor); final MetaConstructor metaConstructor; if (meta == null) { meta = new MetaConstructor(metabean, constructor); metaConstructor = MetaConstructor.class.cast(meta); metabean.addConstructor(constructor, metaConstructor); } else { metaConstructor = MetaConstructor.class.cast(meta); } final Integer index = metaConstraint.getIndex(); if (index != null && index >= 0) { MetaParameter param = metaConstructor.getParameter(index); if (param == null) { param = new MetaParameter(metaConstructor, index); metaConstructor.addParameter(index, param); } param.addAnnotation(metaConstraint.getAnnotation()); } else { metaConstructor.addAnnotation(metaConstraint.getAnnotation()); } continue; } else { // property level meta = metabean.getProperty(access.getPropertyName()); create = meta == null; if (create) { meta = addMetaProperty(metabean, access); } } if (!annotationProcessor.processAnnotation(metaConstraint.getAnnotation(), meta, beanClass, metaConstraint.getAccessStrategy(), new AppendValidationToMeta(meta == null ? metabean : meta), false) && create) { metabean.putProperty(access.getPropertyName(), null); } } for (final AccessStrategy access : factory.getValidAccesses(beanClass)) { if (access.getElementType() == ElementType.PARAMETER) { continue; } MetaProperty metaProperty = metabean.getProperty(access.getPropertyName()); boolean create = metaProperty == null; if (create) { metaProperty = addMetaProperty(metabean, access); } if (!annotationProcessor.addAccessStrategy(metaProperty, access) && create) { metabean.putProperty(access.getPropertyName(), null); } } }
From source file:org.jgentleframework.utils.ReflectUtils.java
/** * Phng thc s kim tra xem <code>annotation</code> ch nh c * {@link Target} tng ng hp l vi i tng <code>object</code> hay * khng (i tng <code>object</code> c th l bt k thc th ch nh * no cho php annotate annotation nh/*w w w . ja v a 2 s . c o m*/ * <code>Class, Interface, Annotation, Enum, Method, Field, Constructor, ...</code> * ) * * @param annotation * i tng <code>annotation</code> cn kim tra. * @param obj * i tng <code>object</code> cn kim tra * @return tr v? <b>true</b> nu c, nu khng tr v? <b>false</b>. */ public static boolean isValidTarget(Annotation annotation, Object obj) { if (annotation.annotationType().isAnnotationPresent(Target.class)) { Target target = annotation.annotationType().getAnnotation(Target.class); if (obj.getClass().isAnnotation()) { if (!Arrays.asList(target.value()).contains(ElementType.TYPE) || !Arrays.asList(target.value()).contains(ElementType.ANNOTATION_TYPE)) { return false; } } else if (ReflectUtils.isCast(Constructor.class, obj)) { if (!Arrays.asList(target.value()).contains(ElementType.CONSTRUCTOR)) { return false; } } else if (ReflectUtils.isField(obj)) { if (!Arrays.asList(target.value()).contains(ElementType.FIELD)) { return false; } } else if (ReflectUtils.isMethod(obj)) { if (!Arrays.asList(target.value()).contains(ElementType.METHOD)) { return false; } } else if (ReflectUtils.isClass(obj) || ReflectUtils.isInterface(obj)) { if (!Arrays.asList(target.value()).contains(ElementType.TYPE)) { return false; } } else { if (!obj.getClass().isAnnotation() && !ReflectUtils.isCast(Constructor.class, obj) && !ReflectUtils.isField(obj) && !ReflectUtils.isMethod(obj.getClass()) && !ReflectUtils.isClass(obj) && !ReflectUtils.isInterface(obj)) { return false; } } } return true; }