1 package jp.sourceforge.stigmata.birthmarks.uc;
3 import jp.sourceforge.stigmata.Birthmark;
4 import jp.sourceforge.stigmata.BirthmarkContext;
5 import jp.sourceforge.stigmata.BirthmarkElement;
6 import jp.sourceforge.stigmata.birthmarks.BirthmarkExtractVisitor;
8 import org.objectweb.asm.ClassVisitor;
9 import org.objectweb.asm.FieldVisitor;
10 import org.objectweb.asm.Label;
11 import org.objectweb.asm.MethodAdapter;
12 import org.objectweb.asm.MethodVisitor;
13 import org.objectweb.asm.Type;
14 import org.objectweb.asm.signature.SignatureReader;
15 import org.objectweb.asm.signature.SignatureWriter;
19 * @author Haruaki TAMADA
21 public class UsedClassesBirthmarkExtractVisitor extends BirthmarkExtractVisitor{
22 public UsedClassesBirthmarkExtractVisitor(ClassVisitor visitor, Birthmark birthmark,
23 BirthmarkContext context){
24 super(visitor, birthmark, context);
28 public void visit(int version, int access, String name, String signature,
29 String superName, String[] interfaces){
30 addSignatureClass(signature);
32 if(getEnvironment().getWellknownClassManager().isWellKnownClass(superName)){
35 for(String i: interfaces){
36 if(getEnvironment().getWellknownClassManager().isWellKnownClass(i)){
43 public FieldVisitor visitField(int access, String name, String desc,
44 String signature, Object value){
45 FieldVisitor visitor = super.visitField(access, name, desc, signature, value);
48 addSignatureClass(signature);
54 public MethodVisitor visitMethod(int access, String name, String desc,
55 String signature, String[] exceptions){
56 if(exceptions != null){
57 for(String exception: exceptions){
58 if(getEnvironment().getWellknownClassManager().isWellKnownClass(exception)){
63 addMethodDescriptor(desc);
64 addSignatureClass(signature);
66 MethodVisitor visitor = super.visitMethod(access, name, desc, signature, exceptions);
68 return new MethodAdapter(visitor){
70 public void visitTypeInsn(int opcode, String desc){
71 Type type = Type.getType("L" + desc + ";");
75 super.visitTypeInsn(opcode, desc);
79 public void visitTryCatchBlock(Label start, Label end, Label handle, String desc){
80 Type type = Type.getType("L" + desc + ";");
84 super.visitTryCatchBlock(start, end, handle, desc);
88 public void visitMultiANewArrayInsn(String desc, int dims){
89 Type type = Type.getType(desc);
93 super.visitMultiANewArrayInsn(desc, dims);
97 public void visitLocalVariable(String name, String desc, String signature,
98 Label start, Label end, int index){
99 if(checkType(Type.getType(desc))){
102 addSignatureClass(signature);
104 super.visitLocalVariable(name, desc, signature, start, end, index);
108 public void visitFieldInsn(int opcode, String owner, String name, String desc){
109 if(getEnvironment().getWellknownClassManager().isWellKnownClass(owner)){
113 super.visitFieldInsn(opcode, owner, name, desc);
116 public void visitMethodInsn(int opcode, String owner, String name, String desc){
117 String className = normalize(owner);
118 if(getEnvironment().getWellknownClassManager().isWellKnownClass(className)){
121 addMethodDescriptor(desc);
122 super.visitMethodInsn(opcode, owner, name, desc);
127 private void addSignatureClass(String signature){
128 if(signature != null){
129 SignatureReader in = new SignatureReader(signature);
130 SignatureWriter writer = new SignatureWriter(){
132 public void visitClassType(String classType){
133 if(getEnvironment().getWellknownClassManager().isWellKnownClass(classType)){
142 private void addMethodDescriptor(String desc){
143 Type returnType = Type.getReturnType(desc);
144 Type[] args = Type.getArgumentTypes(desc);
145 if(checkType(returnType)){
146 add(getType(returnType));
155 private void addDescriptor(String desc){
156 Type type = Type.getType(desc);
162 private String getType(Type type){
163 if(type.getSort() == Type.ARRAY){
164 while(type.getSort() != Type.ARRAY){
165 type = type.getElementType();
169 if(type.getSort() == Type.OBJECT){
170 return normalize(type.getClassName());
176 private boolean checkType(Type type){
177 if(type.getSort() == Type.ARRAY){
178 while(type.getSort() != Type.ARRAY){
179 type = type.getElementType();
183 if(type.getSort() == Type.OBJECT){
184 String className = type.getClassName();
185 if(getEnvironment().getWellknownClassManager().isWellKnownClass(className)){
192 private String normalize(String name){
193 if(name.startsWith("L") && name.endsWith(";")){
194 name = name.substring(1, name.length() - 1);
196 name = name.replace('/', '.');
201 private void add(String name){
202 addElement(new BirthmarkElement(normalize(name)));