Re: Can ASM method-visitors be used with interfaces?
Peter Veentjer <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
Perhaps I'm missing something, but the instructions of a method can't be used to find (all) the callers of that method. If you want to find all usages of an interfaces, you need to go through all methods that have instructions and see of a INVOKE_INTERFACE on your desired interface is being executed. PS: with the INVOKE_INTERFACE you will only find direct interface calls and not implementing classes. On Thu, May 20, 2010 at 11:11 AM, <[email protected]> wrote: > I need to write a tool that lists the classes that call methods of > specified > interfaces. It will be used as part of the build process of a large java > application consisting of many modules. The goal is to automatically > document > the dependencies between certain java modules. > > I found several tools for dependency analysis, but they don't work on the > method level, just for packages or jars. Finally I found ASM, that seems to > do > what I need. > > The following code prints the method dependencies of all class files in a > given > directory: > > -------------- > import java.io.*; > import java.util.*; > > import org.objectweb.asm.ClassReader; > > public class Test { > > public static void main(String[] args) throws Exception { > > File dir = new File(args[0]); > > List<File> classFiles = new LinkedList<File>(); > findClassFiles(classFiles, dir); > > for (File classFile : classFiles) { > InputStream input = new FileInputStream(classFile); > new ClassReader(input).accept(new MyClassVisitor(), 0); > input.close(); > } > } > > private static void findClassFiles(List<File> list, File dir) { > for (File file : dir.listFiles()) { > if (file.isDirectory()) { > findClassFiles(list, file); > } else if (file.getName().endsWith(".class")) { > list.add(file); > } > } > } > } > -------------------- > import org.objectweb.asm.MethodVisitor; > import org.objectweb.asm.commons.EmptyVisitor; > > public class MyClassVisitor extends EmptyVisitor { > > private String className; > > @Override > public void visit(int version, int access, String name, String > signature, > String superName, String[] interfaces) { > this.className = name; > } > > @Override > public MethodVisitor visitMethod(int access, String name, String desc, > String signature, String[] exceptions) { > > System.out.println(className + "." + name); > return new MyMethodVisitor(); > } > } > ------------------------ > import org.objectweb.asm.commons.EmptyVisitor; > > public class MyMethodVisitor extends EmptyVisitor { > > @Override > public void visitMethodInsn(int opcode, String owner, String name, > String desc) { > > String key = owner + "." + name; > System.out.println(" " + key); > } > } > ----------------------- > > The Problem: > The code works for regular classes only! If the class file contains an > interface, visitMethod() is called, but not visitMethodInsn(). I don't get > any > info about the callers of interface methods. > > Any ideas? > > > -- > You receive this message as a subscriber of the [email protected] mailing list. > To unsubscribe: mailto:[email protected] > For general help: mailto:[email protected]?subject=help > OW2 mailing lists service home page: http://www.ow2.org/wws > >
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws