1 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 ; 27 import java.util.zip.ZipFile ; 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 source; 37 private String curName; 38 private DepHandler handler; 39 private PathClass curPathClass; 40 41 private final SignatureVisitor SIG = new EmptySignatureVisitor() { 42 public void visitTypeVariable(String name) { 43 checkName(name); 44 } 45 public void visitClassType(String name) { 46 checkName(name); 47 } 48 public void visitInnerClassType(String name) { 49 checkName(name); 50 } 51 }; 52 53 public DepFindVisitor(Map classes, Object 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 name, String signature, String superName, String [] 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 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 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 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 getSourceName(Object source) throws IOException { 92 if (source instanceof ZipFile ) { 93 return ((ZipFile )source).getName(); 94 } else { 95 return ((File )source).getCanonicalPath(); 96 } 97 } 98 99 private void checkName(String name) { 100 try { 101 if (classes.containsKey(name)) { 102 String otherSource = getSourceName(classes.get(name)); 103 if (!source.equals(otherSource)) { 104 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 [] 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 value) 122 { 123 if (value instanceof Type) 124 checkDesc(((Type)value).getDescriptor()); 125 } 126 127 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String [] exceptions) { 128 checkMethodDesc(desc); 129 checkSignature(signature, false); 130 checkNames(exceptions); 131 return this; 132 } 133 134 public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { 135 checkDesc(desc); 136 checkSignature(signature, true); 137 checkType(value); 138 return this; 139 } 140 141 public void visitTypeInsn(int opcode, String desc) { 142 if (desc.charAt(0) == '[') { 143 checkDesc(desc); 144 } else { 145 checkName(desc); 146 } 147 } 148 149 public void visitFieldInsn(int opcode, String owner, String name, String desc) { 150 checkName(owner); 151 checkDesc(desc); 152 } 153 154 public void visitLdcInsn(Object cst) { 155 checkType(cst); 156 } 157 158 public void visitMethodInsn(int opcode, String owner, String name, String desc) { 159 checkName(owner); 160 checkMethodDesc(desc); 161 } 162 163 public void visitMultiANewArrayInsn(String desc, int dims) { 164 checkDesc(desc); 165 } 166 167 public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { 168 checkName(type); 169 } 170 171 public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { 172 checkDesc(desc); 173 checkSignature(signature, true); 174 } 175 176 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 177 checkDesc(desc); 178 return this; 179 } 180 181 public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { 182 checkDesc(desc); 183 return this; 184 } 185 186 public void visit(String name, Object value) { 187 checkType(value); 188 } 189 190 public void visitClassType(String name) { 191 checkName(name); 192 } 193 194 public void visitInnerClassType(String name) { 195 checkName(name); 196 } 197 198 public void visitTypeVariable(String name) { 199 } 201 202 public AnnotationVisitor visitAnnotation(String name, String desc) { 203 checkDesc(desc); 204 return this; 205 } 206 207 public void visitEnum(String name, String desc, String value) { 208 checkDesc(desc); 209 } 210 } 211 | Popular Tags |