KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > DepFindVisitor


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

20
21 package com.tonicsystems.jarjar;
22
23 import com.tonicsystems.jarjar.util.*;
24 import java.io.*;
25 import java.util.*;
26 import java.util.zip.ZipEntry JavaDoc;
27 import java.util.zip.ZipFile JavaDoc;
28 import org.objectweb.asm.*;
29 import org.objectweb.asm.Type;
30 import org.objectweb.asm.commons.EmptyVisitor;
31 import org.objectweb.asm.signature.*;
32
33 class DepFindVisitor extends EmptyVisitor
34 {
35     private Map classes;
36     private String JavaDoc source;
37     private String JavaDoc curName;
38     private DepHandler handler;
39     private PathClass curPathClass;
40     
41     private final SignatureVisitor SIG = new EmptySignatureVisitor() {
42         public void visitTypeVariable(String JavaDoc name) {
43             checkName(name);
44         }
45         public void visitClassType(String JavaDoc name) {
46             checkName(name);
47         }
48         public void visitInnerClassType(String JavaDoc name) {
49             checkName(name);
50         }
51     };
52
53     public DepFindVisitor(Map classes, Object JavaDoc source, DepHandler handler) throws IOException {
54         this.classes = classes;
55         this.source = getSourceName(source);
56         this.handler = handler;
57     }
58
59     public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
60         curName = name;
61         curPathClass = new PathClass(source, curName);
62         checkSignature(signature, false);
63         checkName(superName);
64         checkNames(interfaces);
65     }
66
67     private void checkSignature(String JavaDoc signature, boolean type) {
68         if (signature != null) {
69             SignatureReader reader = new SignatureReader(signature);
70             if (type) {
71                 reader.acceptType(SIG);
72             } else {
73                 reader.accept(SIG);
74             }
75         }
76     }
77     
78     private void checkDesc(String JavaDoc desc) {
79         int index = desc.indexOf('L');
80         if (index >= 0)
81             checkName(desc.substring(index + 1, desc.length() - 1));
82     }
83
84     private void checkMethodDesc(String JavaDoc methodDesc) {
85         checkDesc(Type.getReturnType(methodDesc).getDescriptor());
86         Type[] args = Type.getArgumentTypes(methodDesc);
87         for (int i = 0; i < args.length; i++)
88             checkDesc(args[i].getDescriptor());
89     }
90
91     private String JavaDoc getSourceName(Object JavaDoc source) throws IOException {
92         if (source instanceof ZipFile JavaDoc) {
93             return ((ZipFile JavaDoc)source).getName();
94         } else {
95             return ((File JavaDoc)source).getCanonicalPath();
96         }
97     }
98
99     private void checkName(String JavaDoc name) {
100         try {
101             if (classes.containsKey(name)) {
102                 String JavaDoc otherSource = getSourceName(classes.get(name));
103                 if (!source.equals(otherSource)) {
104                     // TODO: some escape mechanism?
105
handler.handle(curPathClass, new PathClass(otherSource, name));
106                 }
107             }
108         } catch (IOException e) {
109             throw new RuntimeIOException(e);
110         }
111     }
112
113     private void checkNames(String JavaDoc[] names)
114     {
115         if (names != null) {
116             for (int i = 0; i < names.length; i++)
117                 checkName(names[i]);
118         }
119     }
120
121     private void checkType(Object JavaDoc value)
122     {
123         if (value instanceof Type)
124             checkDesc(((Type)value).getDescriptor());
125     }
126
127     public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
128         checkMethodDesc(desc);
129         checkSignature(signature, false);
130         checkNames(exceptions);
131         return this;
132     }
133
134     public FieldVisitor visitField(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, Object JavaDoc value) {
135         checkDesc(desc);
136         checkSignature(signature, true);
137         checkType(value);
138         return this;
139     }
140
141     public void visitTypeInsn(int opcode, String JavaDoc desc) {
142         if (desc.charAt(0) == '[') {
143             checkDesc(desc);
144         } else {
145             checkName(desc);
146         }
147     }
148
149     public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
150         checkName(owner);
151         checkDesc(desc);
152     }
153
154     public void visitLdcInsn(Object JavaDoc cst) {
155         checkType(cst);
156     }
157
158     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
159         checkName(owner);
160         checkMethodDesc(desc);
161     }
162
163     public void visitMultiANewArrayInsn(String JavaDoc desc, int dims) {
164         checkDesc(desc);
165     }
166
167     public void visitTryCatchBlock(Label start, Label end, Label handler, String JavaDoc type) {
168         checkName(type);
169     }
170
171     public void visitLocalVariable(String JavaDoc name, String JavaDoc desc, String JavaDoc signature, Label start, Label end, int index) {
172         checkDesc(desc);
173         checkSignature(signature, true);
174     }
175
176     public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
177         checkDesc(desc);
178         return this;
179     }
180
181     public AnnotationVisitor visitParameterAnnotation(int parameter, String JavaDoc desc, boolean visible) {
182         checkDesc(desc);
183         return this;
184     }
185
186     public void visit(String JavaDoc name, Object JavaDoc value) {
187         checkType(value);
188     }
189     
190     public void visitClassType(String JavaDoc name) {
191         checkName(name);
192     }
193
194     public void visitInnerClassType(String JavaDoc name) {
195         checkName(name);
196     }
197
198     public void visitTypeVariable(String JavaDoc name) {
199         // TODO
200
}
201     
202     public AnnotationVisitor visitAnnotation(String JavaDoc name, String JavaDoc desc) {
203         checkDesc(desc);
204         return this;
205     }
206     
207     public void visitEnum(String JavaDoc name, String JavaDoc desc, String JavaDoc value) {
208         checkDesc(desc);
209     }
210 }
211
Popular Tags