1
2
3
4
5
6
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
25
26 public class AnnotationWriterAdapter extends ClassAdapter {
27
28
29
30
31 private static final Logger LOG
32 = LoggerFactory.getLogger(AnnotationWriterAdapter.class);
33
34
35 public AnnotationsMaps tracer;
36
37
38
39
40 protected ClassWriter classWriter = null;
41
42
43
44
45 protected String className = null;
46
47
48
49
50
51
52
53
54
55 public AnnotationWriterAdapter(ClassVisitor cv, AnnotationsMaps tracer) {
56 super(cv);
57
58 this.tracer = tracer;
59 }
60
61
62
63
64
65
66
67
68
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
81 Map<String, AnnotationVisibleAndValue> ann = tracer.annotationOnClass;
82
83 if (ann == null) {
84 LOG.debug("visit. NO ANNOTATIONS FOR " + name);
85 return;
86 }
87
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
110
111
112
113
114
115
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
135
136 for (String annName : ann.keySet()) {
137 AnnotationVisibleAndValue annotationVisibleAndValue = ann.get(annName);
138
139 if (annotationVisibleAndValue == null) {
140 continue;
141 }
142
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
155
156
157
158
159
160
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
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
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
202
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 }