KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > compiler > AnnotationInfoCreator


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.aop.annotation.compiler;
23
24 import javassist.ClassPool;
25 import javassist.CtClass;
26 import javassist.NotFoundException;
27 import javassist.bytecode.ConstPool;
28 import javassist.bytecode.annotation.AnnotationMemberValue;
29 import javassist.bytecode.annotation.ArrayMemberValue;
30 import javassist.bytecode.annotation.CharMemberValue;
31 import javassist.bytecode.annotation.MemberValue;
32 import javassist.bytecode.annotation.StringMemberValue;
33
34 import org.jboss.aop.annotation.factory.duplicate.ast.ASTAnnotation;
35 import org.jboss.aop.annotation.factory.duplicate.ast.ASTChar;
36 import org.jboss.aop.annotation.factory.duplicate.ast.ASTIdentifier;
37 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValueArrayInitializer;
38 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValuePair;
39 import org.jboss.aop.annotation.factory.duplicate.ast.ASTMemberValuePairs;
40 import org.jboss.aop.annotation.factory.duplicate.ast.ASTSingleMemberValue;
41 import org.jboss.aop.annotation.factory.duplicate.ast.ASTStart;
42 import org.jboss.aop.annotation.factory.duplicate.ast.ASTString;
43 import org.jboss.aop.annotation.factory.duplicate.ast.AnnotationParserVisitor;
44 import org.jboss.aop.annotation.factory.duplicate.ast.SimpleNode;
45
46 import java.util.Iterator JavaDoc;
47 import java.util.Set JavaDoc;
48
49 /**
50  * Comment
51  *
52  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
53  * @version $Revision: 57019 $
54  */

55 public class AnnotationInfoCreator implements AnnotationParserVisitor
56 {
57    private ClassPool pool;
58    private ConstPool cp;
59    private javassist.bytecode.annotation.Annotation base;
60
61    public AnnotationInfoCreator(ClassPool pool, ConstPool cp, javassist.bytecode.annotation.Annotation base)
62    {
63       this.pool = pool;
64       this.cp = cp;
65       this.base = base;
66    }
67
68    public Object JavaDoc visit(ASTMemberValuePairs node, Object JavaDoc data)
69    {
70       node.childrenAccept(this, data);
71       return null;
72    }
73
74    public Object JavaDoc visit(ASTMemberValuePair node, Object JavaDoc data)
75    {
76       String JavaDoc name = node.getIdentifier().getValue();
77       MemberValue mv = base.getMemberValue(name);
78       if (mv == null)
79       {
80          throw new RuntimeException JavaDoc("member value is null for created annotation: " + base.getTypeName() + " for name: " + name);
81       }
82       node.getValue().jjtAccept(this, mv);
83       return data;
84    }
85
86    public Object JavaDoc visit(ASTSingleMemberValue node, Object JavaDoc data)
87    {
88       if (base.getMemberNames().size() > 1) throw new RuntimeException JavaDoc("single value expected from annotation: " + base.getTypeName());
89
90       Set JavaDoc set = base.getMemberNames();
91       MemberValue mv = base.getMemberValue((String JavaDoc) set.iterator().next());
92       node.getValue().jjtAccept(this, mv);
93       return data;
94    }
95
96    public Object JavaDoc visit(ASTIdentifier node, Object JavaDoc data)
97    {
98       MemberValue mv = (MemberValue) data;
99       MemberValuePopulate mvc = new MemberValuePopulate(node.getValue());
100       mv.accept(mvc);
101       return null;
102    }
103
104    public Object JavaDoc visit(ASTString node, Object JavaDoc data)
105    {
106       StringMemberValue mv = (StringMemberValue) data;
107       mv.setValue(node.getValue());
108       return null;
109    }
110
111    public Object JavaDoc visit(ASTChar node, Object JavaDoc data)
112    {
113       CharMemberValue mv = (CharMemberValue) data;
114       mv.setValue(node.getValue());
115       return null;
116    }
117
118
119    public Object JavaDoc visit(ASTMemberValueArrayInitializer node, Object JavaDoc data)
120    {
121       ArrayMemberValue mv = (ArrayMemberValue) data;
122       int size = node.jjtGetNumChildren();
123       MemberValue[] elements = new MemberValue[size];
124       MemberValueCreation mvc = new MemberValueCreation(cp);
125       for (int i = 0; i < size; i++)
126       {
127          mv.getType().accept(mvc);
128          elements[i] = mvc.value;
129          node.jjtGetChild(i).jjtAccept(this, elements[i]);
130       }
131       mv.setValue(elements);
132       return null;
133    }
134
135    public Object JavaDoc visit(ASTAnnotation node, Object JavaDoc data)
136    {
137       try
138       {
139          CtClass annotation = pool.get(node.getIdentifier());
140          javassist.bytecode.annotation.Annotation info = new javassist.bytecode.annotation.Annotation(cp, annotation);
141          AnnotationInfoCreator creator = new AnnotationInfoCreator(pool, cp, info);
142          node.childrenAccept(creator, data);
143          AnnotationMemberValue mv = (AnnotationMemberValue) data;
144          mv.setValue(info);
145       }
146       catch (NotFoundException e)
147       {
148          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
149
}
150       return null;
151    }
152
153    // Unneeded
154

155    public Object JavaDoc visit(SimpleNode node, Object JavaDoc data)
156    {
157       return null;
158    }
159
160    public Object JavaDoc visit(ASTStart node, Object JavaDoc data)
161    {
162       return null;
163    }
164
165    public static javassist.bytecode.annotation.Annotation createAnnotationInfo(ClassPool pool, ConstPool cp, ASTAnnotation node) throws Exception JavaDoc
166    {
167       CtClass annotation = pool.get(node.getIdentifier());
168       javassist.bytecode.annotation.Annotation info = new javassist.bytecode.annotation.Annotation(cp, annotation);
169       AnnotationInfoCreator creator = new AnnotationInfoCreator(pool, cp, info);
170       if (node.jjtGetNumChildren() > 0)
171       {
172          node.jjtGetChild(0).jjtAccept(creator, null);
173       }
174       return info;
175    }
176
177 }
178
Popular Tags