List of usage examples for java.lang.annotation ElementType METHOD
ElementType METHOD
To view the source code for java.lang.annotation ElementType METHOD.
Click Source Link
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
private boolean isPropertyConstrained(final PropertyDescriptor ppropertyDescription, final boolean useField) { // cascaded counts as constrained // we must know if the @Valid annotation is on a field or a getter final JClassType jClass = this.beanHelper.getJClass(); if (useField && jClass.findField(ppropertyDescription.getPropertyName()).isAnnotationPresent(Valid.class)) { return true; } else if (!useField && jClass.findMethod(asGetter(ppropertyDescription), NO_ARGS).isAnnotationPresent(Valid.class)) { return true; }/*from www.java2s . c o m*/ // for non-cascaded properties for (final ConstraintDescriptor<?> constraint : ppropertyDescription.getConstraintDescriptors()) { final org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?> constraintHibernate = (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?>) constraint; if (constraintHibernate.getElementType() == (useField ? ElementType.FIELD : ElementType.METHOD)) { return true; } } return false; }
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/*from w w w .jav a 2 s . com*/ * <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; }
From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java
private void writeValidatePropertyMethod(final SourceWriter sw, final PropertyDescriptor ppropertyDescription, final boolean useField) throws UnableToCompleteException { final Class<?> elementClass = ppropertyDescription.getElementClass(); final JType elementType = this.beanHelper.getElementType(ppropertyDescription, useField); // private final <T> void validateProperty_{get}<p>( sw.print("private final <T> void "); if (useField) { sw.print(this.validateMethodFieldName(ppropertyDescription)); } else {//w ww .j a v a2 s . com sw.print(this.validateMethodGetterName(ppropertyDescription)); } sw.println("("); sw.indent(); sw.indent(); // final GwtValidationContext<T> context, sw.println("final GwtValidationContext<T> context,"); // final Set<ConstraintViolation<T>> violations, sw.println("final Set<ConstraintViolation<T>> violations,"); // BeanType object, sw.println(this.beanHelper.getTypeCanonicalName() + " object,"); // final <Type> value, sw.print("final "); sw.print(elementType.getParameterizedQualifiedSourceName()); sw.println(" value,"); // boolean honorValid, sw.println("boolean honorValid,"); // Class<?>... groups) { sw.println("Class<?>... groups) {"); sw.outdent(); // only write the checks if the property is constrained in some way if (this.isPropertyConstrained(ppropertyDescription, useField)) { // context = context.append("myProperty"); sw.print("final GwtValidationContext<T> myContext = context.append(\""); sw.print(ppropertyDescription.getPropertyName()); sw.println("\");"); // only check this property if the TraversableResolver says we can // Node leafNode = myContext.getPath().getLeafNode(); sw.println("Node leafNode = myContext.getPath().getLeafNode();"); // PathImpl path = myContext.getPath().getPathWithoutLeafNode(); sw.println("PathImpl path = myContext.getPath().getPathWithoutLeafNode();"); // boolean isReachable; sw.println("boolean isReachable;"); // try { sw.println("try {"); sw.indent(); // isReachable = myContext.getTraversableResolver().isReachable(object, leafNode, // myContext.getRootBeanClass(), path, ElementType); sw.println("isReachable = myContext.getTraversableResolver().isReachable(object, " + "leafNode, myContext.getRootBeanClass(), path, " + (useField ? asLiteral(ElementType.FIELD) : asLiteral(ElementType.METHOD)) + ");"); // } catch (Exception e) { sw.outdent(); sw.println("} catch (Exception e) {"); sw.indent(); // throw new ValidationException("TraversableResolver isReachable caused an exception", e); sw.println("throw new ValidationException(\"TraversableResolver isReachable caused an " + "exception\", e);"); // } sw.outdent(); sw.println("}"); // if (isReachable) { sw.println("if (isReachable) {"); sw.indent(); // TODO(nchalko) move this out of here to the Validate method if (ppropertyDescription.isCascaded() && this.hasValid(ppropertyDescription, useField)) { // if (honorValid && value != null) { sw.println("if (honorValid && value != null) {"); sw.indent(); // boolean isCascadable; sw.println("boolean isCascadable;"); // try { sw.println("try {"); sw.indent(); // isCascadable = myContext.getTraversableResolver().isCascadable(object, leafNode, // myContext.getRootBeanClass(), path, ElementType) sw.println("isCascadable = myContext.getTraversableResolver().isCascadable(object, " + "leafNode, myContext.getRootBeanClass(), path, " + (useField ? asLiteral(ElementType.FIELD) : asLiteral(ElementType.METHOD)) + ");"); // } catch (Exception e) { sw.outdent(); sw.println("} catch (Exception e) {"); sw.indent(); // throw new ValidationException("TraversableResolver isReachable caused an exception", e); sw.println("throw new ValidationException(\"TraversableResolver isCascadable caused an " + "exception\", e);"); // } sw.outdent(); sw.println("}"); // if (isCascadable) { sw.println("if (isCascadable) {"); sw.indent(); if (isIterableOrMap(elementClass)) { final JClassType associationType = this.beanHelper.getAssociationType(ppropertyDescription, useField); this.createBeanHelper(associationType); if (Map.class.isAssignableFrom(elementClass)) { this.writeValidateMap(sw, ppropertyDescription); } else { this.writeValidateIterable(sw, ppropertyDescription); } } else { this.createBeanHelper(elementClass); // if (!context.alreadyValidated(value)) { sw.println(" if (!context.alreadyValidated(value)) {"); sw.indent(); // violations.addAll(myContext.getValidator().validate(context, value, // groups)); sw.print("violations.addAll("); sw.println("myContext.getValidator().validate(myContext, value, groups));"); // } sw.outdent(); sw.println("}"); } // } sw.outdent(); sw.println("}"); // } sw.outdent(); sw.println("}"); } // It is possible for an annotation with the exact same values to be set on // both the field and the getter. // Keep track of the ones we have used to make sure we don't duplicate. final Set<Object> includedAnnotations = Sets.newHashSet(); int count = 0; for (final ConstraintDescriptor<?> constraint : ppropertyDescription.getConstraintDescriptors()) { if (this.areConstraintDescriptorGroupsValid(constraint)) { final Object annotation = constraint.getAnnotation(); if (this.hasMatchingAnnotation(ppropertyDescription, useField, constraint)) { final String constraintDescriptorVar = this .constraintDescriptorVar(ppropertyDescription.getPropertyName(), count); if (includedAnnotations.contains(annotation)) { // The annotation has been looked at once already during this validate property call // so we know the field and the getter are both annotated with the same constraint. if (!useField) { this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint, constraintDescriptorVar); } } else { if (useField) { this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint, constraintDescriptorVar); } else { // The annotation hasn't been looked at twice (yet) and we are validating a getter // Write the call if only the getter has this constraint applied to it final boolean hasField = this.beanHelper.hasField(ppropertyDescription); if (!hasField || hasField && !this.hasMatchingAnnotation(ppropertyDescription, true, constraint)) { this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint, constraintDescriptorVar); } } } includedAnnotations.add(annotation); } count++; } } // } sw.outdent(); sw.println("}"); } sw.outdent(); sw.println("}"); }