View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.webservice.generator.bcm;
9   
10  import java.util.List;
11  import java.util.Map;
12  
13  import it.imolinfo.jbi4corba.Logger;
14  import it.imolinfo.jbi4corba.LoggerFactory;
15  
16  import org.objectweb.asm.AnnotationVisitor;
17  import org.objectweb.asm.ClassAdapter;
18  import org.objectweb.asm.ClassVisitor;
19  import org.objectweb.asm.ClassWriter;
20  import org.objectweb.asm.FieldVisitor;
21  import org.objectweb.asm.MethodVisitor;
22  
23  /**
24   * XXX javadoc.
25   */
26  public class AnnotationWriterAdapter extends ClassAdapter {
27  
28    /**
29     * Logger.
30     */
31    private static final Logger LOG
32            = LoggerFactory.getLogger(AnnotationWriterAdapter.class);
33  
34  //HAS A ...
35    public AnnotationsMaps tracer;
36    
37    /**
38     * The Class Writer.
39     */
40    protected ClassWriter classWriter = null;
41  
42    /**
43     * The class name.
44     */
45    protected String className = null;
46  
47    
48  
49    /**
50     * Constructor.
51     *
52     * @param    cv        The class visitor
53     * @param    tracer    The tracer
54     */
55    public AnnotationWriterAdapter(ClassVisitor cv, AnnotationsMaps tracer) {
56      super(cv);
57  
58      this.tracer = tracer;
59    }
60  
61    /**
62     * Override.
63     * @param version    The version
64     * @param access     The access
65     * @param name       The name
66     * @param signature  The signature
67     * @param superName  The super name
68     * @param interfaces The interfaces
69     */
70    @Override
71    public void visit(int version,
72                      int access,
73                      String name,
74                      String signature,
75                      String superName,
76                      String [] interfaces) {
77  
78      super.visit(version, access, name, signature, superName, interfaces);
79  
80      // <desc, (visible, value)>
81      Map<String, AnnotationVisibleAndValue> ann = tracer.annotationOnClass;
82  
83      if (ann == null) {
84        LOG.debug("visit. NO ANNOTATIONS FOR " + name);
85        return;
86      }
87      //else
88      for (String annDesc : ann.keySet()) {
89        AnnotationVisibleAndValue annotationVisibleAndValue = ann.get(annDesc);
90  
91        if (annotationVisibleAndValue == null) {
92          LOG.debug("skip ... " + annDesc);
93          continue;
94        } else {
95          LOG.debug("Annotation. desc=" + annDesc + "; visible="
96            + annotationVisibleAndValue.visible);
97        }
98  
99        AnnotationVisitor annVisitor
100         = super.visitAnnotation(annDesc, annotationVisibleAndValue.visible);
101 
102       writeAnnotationValue(annVisitor, annotationVisibleAndValue.value);
103       annVisitor.visitEnd();
104     }
105 
106   }
107 
108   /**
109    * Override.
110    * @param access      The access
111    * @param name        The name
112    * @param desc        The description
113    * @param signature   The signature
114    * @param exceptions  The exceptions
115    * @return            The return
116    */
117   @Override
118   public MethodVisitor visitMethod(int access,
119                                    String name,
120                                    String desc,
121                                    String signature,
122                                    String[] exceptions) {
123     MethodVisitor methodVisitor
124       = super.visitMethod(access, name, desc, signature, exceptions);
125 
126     Map<String, AnnotationVisibleAndValue> ann
127       = tracer.annotationOnMethod.get(name);
128 
129     if (ann == null) {
130       LOG.debug("visitMethod. NO ANNOTATIONS FOR " + name);
131       return methodVisitor;
132     }
133 
134     // else
135 
136     for (String annName : ann.keySet()) {
137       AnnotationVisibleAndValue annotationVisibleAndValue = ann.get(annName);
138 
139       if (annotationVisibleAndValue == null) {
140     	  continue;
141       }
142       //else
143       AnnotationVisitor annVisitor = methodVisitor.visitAnnotation(annName,
144         annotationVisibleAndValue.visible);
145 
146       writeAnnotationValue(annVisitor, annotationVisibleAndValue.value);
147       annVisitor.visitEnd();
148     }
149 
150     return methodVisitor;
151   }
152 
153   /**
154    * Override.
155    * @param access      The access
156    * @param name        The name
157    * @param desc        The description
158    * @param signature   The signature
159    * @param value       The value
160    * @return            The return
161    */
162   @Override
163   public FieldVisitor visitField(int access,
164                                  String name,
165                                  String desc,
166                                  String signature,
167                                  Object value) {
168     FieldVisitor fieldVisitor
169       = super.visitField(access, name, desc, signature, value);
170 
171     Map<String, AnnotationVisibleAndValue> ann
172       = tracer.annotationOnField.get(name);
173 
174     if (ann == null) {
175       LOG.debug("visitField. NO ANNOTATIONS FOR " + name);
176       return fieldVisitor;
177     }
178 
179     // else
180 
181     for (String annName : ann.keySet()) {
182       AnnotationVisibleAndValue annotationVisibleAndValue = ann.get(annName);
183 
184       if (annotationVisibleAndValue == null) {
185         LOG.debug("skip ... " + annName);
186         continue;
187       }
188       //else
189       AnnotationVisitor annVisitor = fieldVisitor.visitAnnotation(annName,
190         annotationVisibleAndValue.visible);
191 
192       writeAnnotationValue(annVisitor, annotationVisibleAndValue.value);
193       annVisitor.visitEnd();
194     }
195 
196     return fieldVisitor;
197   }
198 
199 
200 /**
201  * @param visitor    The visitor
202  * @param valueList  The value list
203  */
204   private void writeAnnotationValue(
205     AnnotationVisitor visitor, List<AnnotationValue> valueList) {
206 
207     if (valueList == null) {
208         LOG.debug("No annotation value.");
209         return;
210     }
211 
212     for (AnnotationValue annValue : valueList) {
213 
214       LOG.debug("AnnotationValue["+ annValue.type + "]. name=" + annValue.name
215         + "; desc=" + annValue.desc + "; value=" + annValue.value);
216 
217       switch (annValue.type) {
218       case AnnotationValue.ANNOTATION_VISIT:
219           visitor.visit(annValue.name, annValue.value);
220           break;
221       case AnnotationValue.ANNOTATION_VISIT_ANNOTATION:
222         AnnotationVisitor visitorNested
223           = visitor.visitAnnotation(annValue.name, annValue.desc);
224 
225         writeAnnotationValue(visitorNested, annValue.nestedAnnotationValue);
226         break;
227       case AnnotationValue.ANNOTATION_VISIT_ARRAY:
228           AnnotationVisitor visitorArray = visitor.visitArray(annValue.name);
229 
230           writeAnnotationValue(visitorArray, annValue.nestedAnnotationValue);
231           visitorArray.visitEnd();
232           break;
233       case AnnotationValue.ANNOTATION_VISIT_ENUM:
234           visitor.visitEnum(annValue.name, annValue.desc, (String) annValue.value);
235           break;
236       }
237 
238     }
239   }
240 
241 }