1 package org.apache.beehive.controls.runtime.generator.apt; 2 3 20 21 import java.io.File ; 22 import java.lang.annotation.Annotation ; 23 import java.lang.reflect.Method ; 24 import java.net.MalformedURLException ; 25 import java.net.URI ; 26 import java.net.URL ; 27 import java.text.ParseException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Map.Entry; 35 36 import org.apache.beehive.controls.api.bean.AnnotationMemberTypes; 37 import org.apache.beehive.controls.api.bean.AnnotationConstraints.MembershipRule; 38 import org.apache.beehive.controls.api.bean.AnnotationConstraints.MembershipRuleValues; 39 import org.apache.beehive.controls.api.properties.PropertyKey; 40 import org.apache.beehive.controls.api.properties.PropertySet; 41 import org.apache.beehive.controls.runtime.bean.AnnotationConstraintValidator; 42 43 import com.sun.mirror.declaration.AnnotationMirror; 44 import com.sun.mirror.declaration.AnnotationTypeDeclaration; 45 import com.sun.mirror.declaration.AnnotationTypeElementDeclaration; 46 import com.sun.mirror.declaration.AnnotationValue; 47 import com.sun.mirror.declaration.Declaration; 48 import com.sun.mirror.declaration.MethodDeclaration; 49 import com.sun.mirror.declaration.TypeDeclaration; 50 import com.sun.mirror.type.AnnotationType; 51 52 57 public class AnnotationConstraintAptValidator extends AnnotationConstraintValidator 58 { 59 60 public AnnotationConstraintAptValidator() 61 { 62 super(); 63 } 64 65 79 public static void validate(Declaration d) throws IllegalArgumentException 80 { 81 Collection <AnnotationMirror> mirrors = d.getAnnotationMirrors(); 82 83 for (AnnotationMirror m : mirrors) 87 { 88 AnnotationType type = m.getAnnotationType(); 89 AnnotationTypeDeclaration decl = m.getAnnotationType().getDeclaration(); 90 if (decl.getAnnotation(PropertySet.class) != null) 91 { 92 Iterator <Map.Entry <AnnotationTypeElementDeclaration, AnnotationValue>> i = m 93 .getElementValues().entrySet().iterator(); 94 while (i.hasNext()) 95 { 96 Map.Entry <AnnotationTypeElementDeclaration, AnnotationValue> entry = i 97 .next(); 98 Collection <Annotation > annotations = getMemberTypeAnnotations(entry.getKey()); 99 if (annotations.size() > 0) 100 { 101 Annotation [] anArray = new Annotation [annotations.size()]; 102 annotations.toArray(anArray); 103 validate(anArray, entry.getValue().getValue()); 104 } 105 } 106 107 if (decl.getAnnotation(MembershipRule.class) != null) 109 { 110 try 111 { 112 String declClassName = decl.getQualifiedName(); 113 114 TypeDeclaration owningClass = decl.getDeclaringType(); 115 if (owningClass != null) 116 declClassName = owningClass.getQualifiedName() + "$" + decl.getSimpleName(); 117 118 Class clazz = Class.forName(declClassName); 119 Annotation a = d.getAnnotation(clazz); 120 validateMembership(a); 121 } 122 catch (ClassNotFoundException cnfe) 123 { 124 } 126 } 127 } 128 } 129 130 if (d instanceof TypeDeclaration) 133 { 134 TypeDeclaration td = ((TypeDeclaration) d); 135 for (Declaration md : td.getMethods()) 136 validate(md); 137 for (Declaration fd : td.getFields()) 138 validate(fd); 139 } 140 else if (d instanceof MethodDeclaration) 142 { 143 for (Declaration pd : ((MethodDeclaration) d).getParameters()) 144 validate(pd); 145 } 146 147 } 148 149 private static Collection <Annotation > getMemberTypeAnnotations( 150 Declaration decl) 151 { 152 Collection <Annotation > annotations = new ArrayList <Annotation >(); 153 for (AnnotationMirror am : decl.getAnnotationMirrors()) 154 { 155 AnnotationType at = am.getAnnotationType(); 156 try 157 { 158 if (at.getContainingType() == null) 159 continue; 160 String containingClassName = at.getContainingType() 161 .getDeclaration().getQualifiedName(); 162 if (containingClassName.equals(AnnotationMemberTypes.class 163 .getName())) 164 { 165 String memberTypeName = at.getDeclaration().getSimpleName(); 166 Class clazz = Class.forName(containingClassName + "$" 167 + memberTypeName); 168 Annotation a = decl.getAnnotation(clazz); 169 if (null != a) 170 { 171 annotations.add(a); 172 } 173 } 174 } 175 catch (ClassNotFoundException e) 176 { 177 } 178 } 179 return annotations; 180 } 181 } 182 | Popular Tags |