1 32 33 package com.jeantessier.classreader; 34 35 import java.util.*; 36 37 import org.apache.oro.text.perl.*; 38 39 public class FeatureDependencyCollector extends CollectorBase { 40 private static final Perl5Util perl = new Perl5Util(); 41 42 private Class_info thisClass; 43 44 public void visitClassfile(Classfile classfile) { 45 thisClass = classfile.getRawClass(); 46 47 classfile.getConstantPool().accept(this); 48 } 49 50 public void visitFieldRef_info(FieldRef_info entry) { 51 if (entry.getRawClass() != thisClass) { 52 add(entry.getClassName() + "." + entry.getRawNameAndType().getName()); 53 } 54 } 55 56 public void visitMethodRef_info(MethodRef_info entry) { 57 if ((entry.getRawClass() != thisClass) && !perl.match("/<.*init>/", entry.getRawNameAndType().getName())) { 58 add(entry.getClassName() + "." + entry.getRawNameAndType().getName()); 59 } 60 } 61 62 public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) { 63 if (entry.getRawClass() != thisClass) { 64 add(entry.getClassName() + "." + entry.getRawNameAndType().getName()); 65 } 66 } 67 68 public void visitMethod_info(Method_info entry) { 69 processSignature(entry.getDescriptor()); 70 71 super.visitMethod_info(entry); 72 } 73 74 public void visitCode_attribute(Code_attribute attribute) { 75 76 byte[] code = attribute.getCode(); 77 78 Iterator ci = attribute.iterator(); 79 while (ci.hasNext()) { 80 Instruction instr = (Instruction) ci.next(); 81 switch (instr.getOpcode()) { 82 case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7: case 0xb8: case 0xb9: int start = instr.getStart(); 91 int index = (code[start+1] << 8) | code[start+2]; 92 ((Visitable) attribute.getClassfile().getConstantPool().get(index)).accept(this); 93 break; 94 default: 95 break; 97 } 98 } 99 100 Iterator i = attribute.getAttributes().iterator(); 101 while (i.hasNext()) { 102 ((Visitable) i.next()).accept(this); 103 } 104 } 105 106 private void processSignature(String str) { 107 int currentPos = 0; 108 int startPos; 109 int endPos; 110 111 while ((startPos = str.indexOf('L', currentPos)) != -1) { 112 if ((endPos = str.indexOf(';', startPos)) != -1) { 113 String candidate = str.substring(startPos + 1, endPos); 114 if (!thisClass.getName().equals(candidate)) { 115 add(SignatureHelper.path2ClassName(candidate)); 116 } 117 currentPos = endPos + 1; 118 } else { 119 currentPos = startPos + 1; 120 } 121 } 122 } 123 } 124 | Popular Tags |