KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.asm.*;
25 import org.objectweb.asm.signature.*;
26
27 class PackageTransformer extends ClassAdapter implements ClassTransformer
28 {
29     private Rules rules;
30     private String JavaDoc className;
31     
32     public PackageTransformer(Rules rules) {
33         super(null);
34         this.rules = rules;
35     }
36     
37     public void setTarget(ClassVisitor target) {
38         cv = target;
39     }
40
41     private String JavaDoc[] fixNames(String JavaDoc[] names) {
42         if (names == null)
43             return null;
44         String JavaDoc[] fixed = new String JavaDoc[names.length];
45         for (int i = 0; i < names.length; i++) {
46             fixed[i] = rules.fixName(names[i]);
47         }
48         return fixed;
49     }
50
51     private Object JavaDoc fixValue(Object JavaDoc value) {
52         if (value instanceof String JavaDoc) {
53             return rules.fixString(className, (String JavaDoc)value);
54         } else if (value instanceof Type) {
55             return Type.getType(rules.fixDesc(((Type)value).getDescriptor()));
56         } else {
57             return value;
58         }
59     }
60
61     public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
62         className = name.replace('/', '.');
63         cv.visit(version, access, rules.fixName(name), fixSignature(signature, false), rules.fixName(superName), fixNames(interfaces));
64     }
65
66     public void visitAttribute(Attribute attr) {
67         cv.visitAttribute(rules.fixAttribute(attr));
68     }
69
70     public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
71         return new AnnotationFixer(cv.visitAnnotation(rules.fixDesc(desc), visible));
72     }
73     
74     public FieldVisitor visitField(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, Object JavaDoc value) {
75         
76         FieldVisitor fv = new FieldFixer(cv.visitField(access, name, rules.fixDesc(desc), fixSignature(signature, true), fixValue(value)));
77         return (fv != null) ? new FieldFixer(fv) : null;
78     }
79
80     public void visitInnerClass(String JavaDoc name, String JavaDoc outerName, String JavaDoc innerName, int access) {
81         cv.visitInnerClass(rules.fixName(name), rules.fixName(outerName), innerName, access);
82     }
83
84     public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
85         MethodVisitor mv = cv.visitMethod(access, name, rules.fixMethodDesc(desc), fixSignature(signature, false), fixNames(exceptions));
86         return (mv != null) ? new MethodFixer(mv) : null;
87     }
88
89     private class FieldFixer implements FieldVisitor
90     {
91         private FieldVisitor fv;
92
93         public FieldFixer(FieldVisitor fv) {
94             this.fv = fv;
95         }
96
97         public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
98             return new AnnotationFixer(fv.visitAnnotation(rules.fixDesc(desc), visible));
99         }
100         
101         public void visitAttribute(Attribute attr) {
102             fv.visitAttribute(rules.fixAttribute(attr));
103         }
104         
105         public void visitEnd() {
106             fv.visitEnd();
107         }
108     }
109
110     private class MethodFixer extends MethodAdapter
111     {
112         public MethodFixer(MethodVisitor mv) {
113             super(mv);
114         }
115
116         public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
117             return fixAnnotation(mv.visitAnnotation(rules.fixDesc(desc), visible));
118         }
119
120         public AnnotationVisitor visitAnnotationDefault() {
121             return fixAnnotation(mv.visitAnnotationDefault());
122         }
123
124         public void visitTypeInsn(int opcode, String JavaDoc desc) {
125             mv.visitTypeInsn(opcode, (desc.charAt(0) == '[') ? rules.fixDesc(desc) : rules.fixName(desc));
126         }
127
128         public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
129             mv.visitFieldInsn(opcode, rules.fixName(owner), name, rules.fixDesc(desc));
130         }
131
132         public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
133             mv.visitMethodInsn(opcode, rules.fixName(owner), name, rules.fixMethodDesc(desc));
134         }
135
136         public void visitLdcInsn(Object JavaDoc cst) {
137             mv.visitLdcInsn(fixValue(cst));
138         }
139
140         public void visitMultiANewArrayInsn(String JavaDoc desc, int dims) {
141             mv.visitMultiANewArrayInsn(rules.fixDesc(desc), dims);
142         }
143
144         public void visitTryCatchBlock(Label start, Label end, Label handler, String JavaDoc type) {
145             mv.visitTryCatchBlock(start, end, handler, rules.fixName(type));
146         }
147
148         public void visitLocalVariable(String JavaDoc name, String JavaDoc desc, String JavaDoc signature, Label start, Label end, int index) {
149             mv.visitLocalVariable(name, rules.fixDesc(desc), fixSignature(signature, true), start, end, index);
150         }
151
152         public void visitAttribute(Attribute attr) {
153             mv.visitAttribute(rules.fixAttribute(attr));
154         }
155     }
156
157     private class AnnotationFixer implements AnnotationVisitor
158     {
159         private AnnotationVisitor av;
160         
161         public AnnotationFixer(AnnotationVisitor av) {
162             this.av = av;
163         }
164         
165         public void visit(String JavaDoc name, Object JavaDoc value) {
166             av.visit(name, fixValue(value));
167         }
168
169         public AnnotationVisitor visitAnnotation(String JavaDoc name, String JavaDoc desc) {
170             return fixAnnotation(av.visitAnnotation(name, rules.fixDesc(desc)));
171         }
172
173         public AnnotationVisitor visitArray(String JavaDoc name) {
174             return fixAnnotation(av.visitArray(name));
175         }
176
177         public void visitEnum(String JavaDoc name, String JavaDoc desc, String JavaDoc value) {
178             av.visitEnum(name, rules.fixDesc(desc), (String JavaDoc)fixValue(value));
179         }
180
181         public void visitEnd() {
182             av.visitEnd();
183         }
184     }
185
186     private AnnotationVisitor fixAnnotation(AnnotationVisitor av) {
187         return (av != null) ? new AnnotationFixer(av) : null;
188     }
189
190     private String JavaDoc fixSignature(String JavaDoc signature, boolean type) {
191         if (signature == null)
192             return null;
193         SignatureReader reader = new SignatureReader(signature);
194         SignatureWriter writer = new SignatureWriter();
195         SignatureFixer fixer = new SignatureFixer(writer);
196         if (type) {
197             reader.acceptType(fixer);
198         } else {
199             reader.accept(fixer);
200         }
201         return writer.toString();
202     }
203     
204     private class SignatureFixer extends SignatureAdapter
205     {
206         public SignatureFixer(SignatureWriter sw) {
207             super(sw);
208         }
209
210         public void visitTypeVariable(String JavaDoc name) {
211             sw.visitTypeVariable(rules.fixName(name));
212         }
213
214         public void visitClassType(String JavaDoc name) {
215             sw.visitClassType(rules.fixName(name));
216         }
217     
218         public void visitInnerClassType(String JavaDoc name) {
219             sw.visitInnerClassType(rules.fixName(name));
220         }
221     }
222 }
223
Popular Tags