KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > annotation > factory > javassist > DefaultValueAnnotationValidator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.annotation.factory.javassist;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.jboss.annotation.factory.AnnotationValidationException;
31 import org.jboss.annotation.factory.AnnotationValidator;
32
33 import javassist.ClassPool;
34 import javassist.CtClass;
35 import javassist.CtMethod;
36 import javassist.NotFoundException;
37 import javassist.bytecode.AnnotationDefaultAttribute;
38 import javassist.bytecode.MethodInfo;
39 import javassist.bytecode.annotation.MemberValue;
40 import javassist.scopedpool.ScopedClassPoolRepository;
41 import javassist.scopedpool.ScopedClassPoolRepositoryImpl;
42
43 /**
44  * Validates if all attributes have been filled in for an annotation.
45  * Attempts to read default values where they exist
46  *
47  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
48  * @version $Revision: 57133 $
49  */

50 public class DefaultValueAnnotationValidator implements AnnotationValidator
51 {
52    ScopedClassPoolRepository repository = ScopedClassPoolRepositoryImpl.getInstance();
53    
54    public void validate(Map JavaDoc<String JavaDoc, Object JavaDoc> map, Class JavaDoc annotation)
55    {
56       ArrayList JavaDoc<String JavaDoc> notAssignedAttributes = null;
57       CtClass ctClass = null;
58       
59       Method JavaDoc[] methods = getDeclaredMethods(annotation);
60       for (int i = 0 ; i < methods.length ; i++)
61       {
62          if (map.get(methods[i].getName()) == null)
63          {
64             if (ctClass == null)
65             {
66                ctClass = getCtClass(annotation);
67             }
68             
69             CtMethod method;
70             try
71             {
72                method = ctClass.getDeclaredMethod(methods[i].getName());
73             }
74             catch (NotFoundException e)
75             {
76                throw new RuntimeException JavaDoc("Unable to find method " + methods[i].getName() + " for " + annotation.getName());
77             }
78             Object JavaDoc defaultValue = null;
79             MethodInfo minfo = method.getMethodInfo2();
80             AnnotationDefaultAttribute defAttr = (AnnotationDefaultAttribute)minfo.getAttribute(AnnotationDefaultAttribute.tag);
81             
82             if (defAttr != null)
83             {
84                MemberValue value = defAttr.getDefaultValue(); // default value of age
85
MemberValueGetter getter = new MemberValueGetter(methods[i]);
86                value.accept(getter);
87                defaultValue = getter.getValue();
88             }
89             
90             if (defaultValue != null)
91             {
92                map.put(methods[i].getName(), defaultValue);
93             }
94             else
95             {
96                if (notAssignedAttributes == null)
97                {
98                   notAssignedAttributes = new ArrayList JavaDoc<String JavaDoc>();
99                }
100                notAssignedAttributes.add(methods[i].getName());
101             }
102
103          }
104       }
105
106       if (notAssignedAttributes != null)
107       {
108          throw new AnnotationValidationException("Unable to fill in default attributes for " + annotation + " " + notAssignedAttributes);
109       }
110    }
111    
112    
113    CtClass getCtClass(Class JavaDoc clazz)
114    {
115       try
116       {
117          ClassPool pool = repository.findClassPool(clazz.getClassLoader());
118          return pool.get(clazz.getName());
119       }
120       catch (NotFoundException e)
121       {
122          throw new RuntimeException JavaDoc("Unable to load CtClass for " + clazz, e);
123       }
124    }
125
126    private Method JavaDoc[] getDeclaredMethods(final Class JavaDoc clazz)
127    {
128       return AccessController.doPrivileged(new PrivilegedAction JavaDoc<Method JavaDoc[]>()
129       {
130          public Method JavaDoc[] run()
131          {
132             return clazz.getDeclaredMethods();
133          };
134       });
135    }
136 }
137
138
Popular Tags