1 | /* |
2 | * Copyright (c) 2001-2009, 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 | public class ClassDependencyCollector extends CollectorBase { |
36 | private Class_info thisClass; |
37 | private boolean top = true; |
38 | |
39 | public void visitClassfile(Classfile classfile) { |
40 | thisClass = classfile.getRawClass(); |
41 | |
42 | classfile.getConstantPool().accept(this); |
43 | |
44 | classfile.getRawSuperclass().accept(this); |
45 | |
46 | for (Class_info class_info : classfile.getAllInterfaces()) { |
47 | class_info.accept(this); |
48 | } |
49 | |
50 | for (Field_info field : classfile.getAllFields()) { |
51 | field.accept(this); |
52 | } |
53 | |
54 | for (Method_info method : classfile.getAllMethods()) { |
55 | method.accept(this); |
56 | } |
57 | } |
58 | |
59 | public void visitClass_info(Class_info entry) { |
60 | String classname = entry.getName(); |
61 | |
62 | if (entry != thisClass) { |
63 | if (classname.startsWith("[") ) { |
64 | top = false; |
65 | entry.getRawName().accept(this); |
66 | top = true; |
67 | } else { |
68 | add(classname); |
69 | } |
70 | } |
71 | } |
72 | |
73 | public void visitFieldRef_info(FieldRef_info entry) { |
74 | if (top) { |
75 | if (entry.getRawClass() == thisClass) { |
76 | top = false; |
77 | entry.getRawNameAndType().accept(this); |
78 | top = true; |
79 | } |
80 | } else { |
81 | entry.getRawNameAndType().accept(this); |
82 | } |
83 | } |
84 | |
85 | public void visitMethodRef_info(MethodRef_info entry) { |
86 | if (top) { |
87 | if (entry.getRawClass() == thisClass) { |
88 | top = false; |
89 | entry.getRawNameAndType().accept(this); |
90 | top = true; |
91 | } |
92 | } else { |
93 | entry.getRawNameAndType().accept(this); |
94 | } |
95 | } |
96 | |
97 | public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) { |
98 | if (top) { |
99 | if (entry.getRawClass() == thisClass) { |
100 | top = false; |
101 | entry.getRawNameAndType().accept(this); |
102 | top = true; |
103 | } |
104 | } else { |
105 | entry.getRawNameAndType().accept(this); |
106 | } |
107 | } |
108 | |
109 | public void visitString_info(String_info entry) { |
110 | if (!top) { |
111 | entry.getRawValue().accept(this); |
112 | } |
113 | } |
114 | |
115 | public void visitNameAndType_info(NameAndType_info entry) { |
116 | if (!top) { |
117 | entry.getRawType().accept(this); |
118 | } |
119 | } |
120 | |
121 | public void visitUTF8_info(UTF8_info entry) { |
122 | if (!top) { |
123 | processSignature(entry.getValue()); |
124 | } |
125 | } |
126 | |
127 | public void visitField_info(Field_info entry) { |
128 | processSignature(entry.getDescriptor()); |
129 | |
130 | super.visitField_info(entry); |
131 | } |
132 | |
133 | public void visitMethod_info(Method_info entry) { |
134 | processSignature(entry.getDescriptor()); |
135 | |
136 | super.visitMethod_info(entry); |
137 | } |
138 | |
139 | public void visitLocalVariable(LocalVariable helper) { |
140 | processSignature(helper.getDescriptor()); |
141 | |
142 | super.visitLocalVariable(helper); |
143 | } |
144 | |
145 | private void processSignature(String str) { |
146 | int currentPos = 0; |
147 | int startPos; |
148 | int endPos; |
149 | |
150 | while ((startPos = str.indexOf('L', currentPos)) != -1) { |
151 | if ((endPos = str.indexOf(';', startPos)) != -1) { |
152 | String candidate = str.substring(startPos + 1, endPos); |
153 | if (!thisClass.getName().equals(candidate)) { |
154 | add(ClassNameHelper.path2ClassName(candidate)); |
155 | } |
156 | currentPos = endPos + 1; |
157 | } else { |
158 | currentPos = startPos + 1; |
159 | } |
160 | } |
161 | } |
162 | } |