KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > ClassDependencyCollector


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

32
33 package com.jeantessier.classreader;
34
35 import java.util.*;
36
37 public class ClassDependencyCollector extends CollectorBase {
38     private Class_info thisClass;
39     private boolean top = true;
40
41     public void visitClassfile(Classfile classfile) {
42         thisClass = classfile.getRawClass();
43
44         classfile.getConstantPool().accept(this);
45
46         classfile.getRawSuperclass().accept(this);
47
48         Iterator i;
49
50         i = classfile.getAllInterfaces().iterator();
51         while (i.hasNext()) {
52             ((Visitable) i.next()).accept(this);
53         }
54
55         i = classfile.getAllFields().iterator();
56         while (i.hasNext()) {
57             ((Visitable) i.next()).accept(this);
58         }
59
60         i = classfile.getAllMethods().iterator();
61         while (i.hasNext()) {
62             ((Visitable) i.next()).accept(this);
63         }
64     }
65
66     public void visitClass_info(Class_info entry) {
67         String JavaDoc classname = entry.getName();
68     
69         if (entry != thisClass) {
70             if (classname.startsWith("[") ) {
71                 top = false;
72                 entry.getRawName().accept(this);
73                 top = true;
74             } else {
75                 add(classname);
76             }
77         }
78     }
79
80     public void visitFieldRef_info(FieldRef_info entry) {
81         if (top) {
82             if (entry.getRawClass() == thisClass) {
83                 top = false;
84                 entry.getRawNameAndType().accept(this);
85                 top = true;
86             }
87         } else {
88             entry.getRawNameAndType().accept(this);
89         }
90     }
91
92     public void visitMethodRef_info(MethodRef_info entry) {
93         if (top) {
94             if (entry.getRawClass() == thisClass) {
95                 top = false;
96                 entry.getRawNameAndType().accept(this);
97                 top = true;
98             }
99         } else {
100             entry.getRawNameAndType().accept(this);
101         }
102     }
103
104     public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) {
105         if (top) {
106             if (entry.getRawClass() == thisClass) {
107                 top = false;
108                 entry.getRawNameAndType().accept(this);
109                 top = true;
110             }
111         } else {
112             entry.getRawNameAndType().accept(this);
113         }
114     }
115
116     public void visitString_info(String_info entry) {
117         if (!top) {
118             entry.getRawValue().accept(this);
119         }
120     }
121
122     public void visitNameAndType_info(NameAndType_info entry) {
123         if (!top) {
124             entry.getRawType().accept(this);
125         }
126     }
127
128     public void visitUTF8_info(UTF8_info entry) {
129         if (!top) {
130             processSignature(entry.getValue());
131         }
132     }
133
134     public void visitField_info(Field_info entry) {
135         processSignature(entry.getDescriptor());
136     
137         super.visitField_info(entry);
138     }
139
140     public void visitMethod_info(Method_info entry) {
141         processSignature(entry.getDescriptor());
142     
143         super.visitMethod_info(entry);
144     }
145
146     public void visitCode_attribute(Code_attribute attribute) {
147         Iterator i = attribute.getAttributes().iterator();
148         while (i.hasNext()) {
149             ((Visitable) i.next()).accept(this);
150         }
151     }
152
153     public void visitExceptions_attribute(Exceptions_attribute attribute) {
154         Iterator i = attribute.getExceptions().iterator();
155         while (i.hasNext()) {
156             ((Visitable) i.next()).accept(this);
157         }
158     }
159
160     public void visitInnerClasses_attribute(InnerClasses_attribute attribute) {
161         Iterator i = attribute.getClasses().iterator();
162         while (i.hasNext()) {
163             ((Visitable) i.next()).accept(this);
164         }
165     }
166
167     public void visitLineNumberTable_attribute(LineNumberTable_attribute attribute) {
168         Iterator i = attribute.getLineNumbers().iterator();
169         while (i.hasNext()) {
170             ((Visitable) i.next()).accept(this);
171         }
172     }
173
174     public void visitLocalVariableTable_attribute(LocalVariableTable_attribute attribute) {
175         Iterator i = attribute.getLocalVariables().iterator();
176         while (i.hasNext()) {
177             ((Visitable) i.next()).accept(this);
178         }
179     }
180
181     public void visitLocalVariable(LocalVariable helper) {
182         processSignature(helper.getDescriptor());
183     }
184
185     private void processSignature(String JavaDoc str) {
186         int currentPos = 0;
187         int startPos;
188         int endPos;
189
190         while ((startPos = str.indexOf('L', currentPos)) != -1) {
191             if ((endPos = str.indexOf(';', startPos)) != -1) {
192                 String JavaDoc candidate = str.substring(startPos + 1, endPos);
193                 if (!thisClass.getName().equals(candidate)) {
194                     add(SignatureHelper.path2ClassName(candidate));
195                 }
196                 currentPos = endPos + 1;
197             } else {
198                 currentPos = startPos + 1;
199             }
200         }
201     }
202 }
203
Popular Tags