KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > depend > DependencyVisitor


1 /***
2  * ASM examples: examples showing how ASM can be used
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.depend;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import org.objectweb.asm.AnnotationVisitor;
38 import org.objectweb.asm.Attribute;
39 import org.objectweb.asm.ClassVisitor;
40 import org.objectweb.asm.FieldVisitor;
41 import org.objectweb.asm.Label;
42 import org.objectweb.asm.MethodVisitor;
43 import org.objectweb.asm.Type;
44 import org.objectweb.asm.signature.SignatureReader;
45 import org.objectweb.asm.signature.SignatureVisitor;
46
47 /**
48  * DependencyVisitor
49  *
50  * @author Eugene Kuleshov
51  */

52 public class DependencyVisitor implements
53         AnnotationVisitor,
54         SignatureVisitor,
55         ClassVisitor,
56         FieldVisitor,
57         MethodVisitor
58 {
59     Set JavaDoc<String JavaDoc> packages = new HashSet JavaDoc<String JavaDoc>();
60
61     Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Integer JavaDoc>> groups = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Integer JavaDoc>>();
62
63     Map JavaDoc<String JavaDoc, Integer JavaDoc> current;
64
65     public Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, Integer JavaDoc>> getGlobals() {
66         return groups;
67     }
68
69     public Set JavaDoc<String JavaDoc> getPackages() {
70         return packages;
71     }
72
73     // ClassVisitor
74

75     public void visit(
76         int version,
77         int access,
78         String JavaDoc name,
79         String JavaDoc signature,
80         String JavaDoc superName,
81         String JavaDoc[] interfaces)
82     {
83         String JavaDoc p = getGroupKey(name);
84         current = groups.get(p);
85         if (current == null) {
86             current = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
87             groups.put(p, current);
88         }
89
90         if (signature == null) {
91             addName(superName);
92             addNames(interfaces);
93         } else {
94             addSignature(signature);
95         }
96     }
97
98     public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
99         addDesc(desc);
100         return this;
101     }
102
103     public void visitAttribute(Attribute attr) {
104     }
105
106     public FieldVisitor visitField(
107         int access,
108         String JavaDoc name,
109         String JavaDoc desc,
110         String JavaDoc signature,
111         Object JavaDoc value)
112     {
113         if (signature == null) {
114             addDesc(desc);
115         } else {
116             addTypeSignature(signature);
117         }
118         if (value instanceof Type)
119             addType((Type) value);
120         return this;
121     }
122
123     public MethodVisitor visitMethod(
124         int access,
125         String JavaDoc name,
126         String JavaDoc desc,
127         String JavaDoc signature,
128         String JavaDoc[] exceptions)
129     {
130         if (signature == null) {
131             addMethodDesc(desc);
132         } else {
133             addSignature(signature);
134         }
135         addNames(exceptions);
136         return this;
137     }
138
139     public void visitSource(String JavaDoc source, String JavaDoc debug) {
140     }
141
142     public void visitInnerClass(
143         String JavaDoc name,
144         String JavaDoc outerName,
145         String JavaDoc innerName,
146         int access)
147     {
148         // addName( outerName);
149
// addName( innerName);
150
}
151
152     public void visitOuterClass(String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
153         // addName(owner);
154
// addMethodDesc(desc);
155
}
156
157     // MethodVisitor
158

159     public AnnotationVisitor visitParameterAnnotation(
160         int parameter,
161         String JavaDoc desc,
162         boolean visible)
163     {
164         addDesc(desc);
165         return this;
166     }
167
168     public void visitTypeInsn(int opcode, String JavaDoc desc) {
169         if (desc.charAt(0) == '[')
170             addDesc(desc);
171         else
172             addName(desc);
173     }
174
175     public void visitFieldInsn(
176         int opcode,
177         String JavaDoc owner,
178         String JavaDoc name,
179         String JavaDoc desc)
180     {
181         addName(owner);
182         addDesc(desc);
183     }
184
185     public void visitMethodInsn(
186         int opcode,
187         String JavaDoc owner,
188         String JavaDoc name,
189         String JavaDoc desc)
190     {
191         addName(owner);
192         addMethodDesc(desc);
193     }
194
195     public void visitLdcInsn(Object JavaDoc cst) {
196         if (cst instanceof Type)
197             addType((Type) cst);
198     }
199
200     public void visitMultiANewArrayInsn(String JavaDoc desc, int dims) {
201         addDesc(desc);
202     }
203
204     public void visitLocalVariable(
205         String JavaDoc name,
206         String JavaDoc desc,
207         String JavaDoc signature,
208         Label start,
209         Label end,
210         int index)
211     {
212         addTypeSignature(signature);
213     }
214
215     public AnnotationVisitor visitAnnotationDefault() {
216         return this;
217     }
218
219     public void visitCode() {
220     }
221
222     public void visitInsn(int opcode) {
223     }
224
225     public void visitIntInsn(int opcode, int operand) {
226     }
227
228     public void visitVarInsn(int opcode, int var) {
229     }
230
231     public void visitJumpInsn(int opcode, Label label) {
232     }
233
234     public void visitLabel(Label label) {
235     }
236
237     public void visitIincInsn(int var, int increment) {
238     }
239
240     public void visitTableSwitchInsn(
241         int min,
242         int max,
243         Label dflt,
244         Label[] labels)
245     {
246     }
247
248     public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
249     }
250
251     public void visitTryCatchBlock(
252         Label start,
253         Label end,
254         Label handler,
255         String JavaDoc type)
256     {
257         addName(type);
258     }
259
260     public void visitLineNumber(int line, Label start) {
261     }
262
263     public void visitMaxs(int maxStack, int maxLocals) {
264     }
265
266     // AnnotationVisitor
267

268     public void visit(String JavaDoc name, Object JavaDoc value) {
269         if (value instanceof Type)
270             addType((Type) value);
271     }
272
273     public void visitEnum(String JavaDoc name, String JavaDoc desc, String JavaDoc value) {
274         addDesc(desc);
275     }
276
277     public AnnotationVisitor visitAnnotation(String JavaDoc name, String JavaDoc desc) {
278         addDesc(desc);
279         return this;
280     }
281
282     public AnnotationVisitor visitArray(String JavaDoc name) {
283         return this;
284     }
285
286     // SignatureVisitor
287

288     public void visitFormalTypeParameter(String JavaDoc name) {
289     }
290
291     public SignatureVisitor visitClassBound() {
292         return this;
293     }
294
295     public SignatureVisitor visitInterfaceBound() {
296         return this;
297     }
298
299     public SignatureVisitor visitSuperclass() {
300         return this;
301     }
302
303     public SignatureVisitor visitInterface() {
304         return this;
305     }
306
307     public SignatureVisitor visitParameterType() {
308         return this;
309     }
310
311     public SignatureVisitor visitReturnType() {
312         return this;
313     }
314
315     public SignatureVisitor visitExceptionType() {
316         return this;
317     }
318
319     public void visitBaseType(char descriptor) {
320     }
321
322     public void visitTypeVariable(String JavaDoc name) {
323         // TODO verify
324
}
325
326     public SignatureVisitor visitArrayType() {
327         return this;
328     }
329
330     public void visitClassType(String JavaDoc name) {
331         addName(name);
332     }
333
334     public void visitInnerClassType(String JavaDoc name) {
335         addName(name);
336     }
337
338     public void visitTypeArgument() {
339     }
340
341     public SignatureVisitor visitTypeArgument(char wildcard) {
342         return this;
343     }
344
345     // common
346

347     public void visitEnd() {
348     }
349
350     // ---------------------------------------------
351

352     private String JavaDoc getGroupKey(String JavaDoc name) {
353         int n = name.lastIndexOf('/');
354         if (n > -1)
355             name = name.substring(0, n);
356         packages.add(name);
357         return name;
358     }
359
360     private void addName(String JavaDoc name) {
361         if (name == null)
362             return;
363         String JavaDoc p = getGroupKey(name);
364         if (current.containsKey(p)) {
365             current.put(p, current.get(p) + 1);
366         } else {
367             current.put(p, 1);
368         }
369     }
370
371     private void addNames(String JavaDoc[] names) {
372         for (int i = 0; names != null && i < names.length; i++)
373             addName(names[i]);
374     }
375
376     private void addDesc(String JavaDoc desc) {
377         addType(Type.getType(desc));
378     }
379
380     private void addMethodDesc(String JavaDoc desc) {
381         addType(Type.getReturnType(desc));
382         Type[] types = Type.getArgumentTypes(desc);
383         for (int i = 0; i < types.length; i++)
384             addType(types[i]);
385     }
386
387     private void addType(Type t) {
388         switch (t.getSort()) {
389             case Type.ARRAY:
390                 addType(t.getElementType());
391                 break;
392             case Type.OBJECT:
393                 addName(t.getClassName().replace('.', '/'));
394                 break;
395         }
396     }
397
398     private void addSignature(String JavaDoc signature) {
399         if (signature != null)
400             new SignatureReader(signature).accept(this);
401     }
402
403     private void addTypeSignature(String JavaDoc signature) {
404         if (signature != null)
405             new SignatureReader(signature).acceptType(this);
406     }
407 }
408
Popular Tags