public static void validate ( Declaration d ) throws IllegalArgumentException { Collection < AnnotationMirror > mirrors = d . getAnnotationMirrors ( ) ; // for each annotations defined on the declaration, if the annotation // is a PropertySet, ensure the values assigned to the properties // satisfy all PropertySet and PropertyType constraints. for ( AnnotationMirror m : mirrors ) { AnnotationTypeDeclaration decl = m . getAnnotationType ( ) . getDeclaration ( ) ; /* when embedding this annotation processor in an IDE, the declaration could be null when it doesn't resolve to a valid type. In this case, just continue processing declarations. */ if ( decl == null ) { continue ; } else if ( decl . getAnnotation ( PropertySet . class ) != null ) { Iterator < Map . Entry < AnnotationTypeElementDeclaration , AnnotationValue > > i = m . getElementValues ( ) . entrySet ( ) . iterator ( ) ; while ( i . hasNext ( ) ) { Map . Entry < AnnotationTypeElementDeclaration , AnnotationValue > entry = i . next ( ) ; Collection < Annotation > annotations = getMemberTypeAnnotations ( entry . getKey ( ) ) ; if ( annotations . size ( ) > 0 ) { Annotation [ ] anArray = new Annotation [ annotations . size ( ) ] ; annotations . toArray ( anArray ) ; validate ( anArray , entry . getValue ( ) . getValue ( ) ) ; } } //If a membership rule is defined on the property set, ensure the rule is satisfied. if ( decl . getAnnotation ( MembershipRule . class ) != null ) { try { String declClassName = decl . getQualifiedName ( ) ; TypeDeclaration owningClass = decl . getDeclaringType ( ) ; if ( owningClass != null ) declClassName = owningClass . getQualifiedName ( ) + "$" + decl . getSimpleName ( ) ; Class clazz = Class . forName ( declClassName ) ; Annotation a = d . getAnnotation ( clazz ) ; validateMembership ( a ) ; } catch ( ClassNotFoundException cnfe ) { //should not happen } } } } // If the declaration is a class or interface, validate its methods // and fields. if ( d instanceof TypeDeclaration ) { TypeDeclaration td = ( ( TypeDeclaration ) d ) ; for ( Declaration md : td . getMethods ( ) ) validate ( md ) ; for ( Declaration fd : td . getFields ( ) ) validate ( ) ; } // If the delcaration is a method, validate its parameters. else if ( d instanceof MethodDeclaration ) { for ( Declaration pd : ( ( MethodDeclaration ) d ) . getParameters ( ) ) validate ( pd ) ; } }
This method ensures that any control property value assignment satisfies all property constraints . This method should be called from an annotation processor to ensure declarative control property assignment using annotations are validated at build time . This method is currently called from ControlAnnotationProcessor and ControlClientAnnotationProcessor .