Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:13+00:00 2026-05-15T06:31:13+00:00

I need to write a tool that lists the classes that call methods of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:31:14+00:00Added an answer on May 15, 2026 at 6:31 am

    I have to admit, I was confused…

    I thought that asm visitors do some magic to give me the list of all callers of
    a given method, like a stacktrace. Instead they justs parse classes and method
    bodies. Fortunatly, this is totally sufficent for my needs as I can build the
    call tree by myself.

    The following code lists all methods that are called by other methods, checking class files in given directory (and subdirectories) only:


    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]);
    
            Map<String, Set<String>> callMap = new HashMap<String, Set<String>>();
    
            List<File> classFiles = new LinkedList<File>();
            findClassFiles(classFiles, dir);
    
            for (File classFile : classFiles) {
                InputStream input = new FileInputStream(classFile);
                new ClassReader(input).accept(new MyClassVisitor(callMap), 0);
                input.close();
            }
    
            for (Map.Entry<String, Set<String>> entry : callMap.entrySet()) {
                String method = entry.getKey();
                Set<String> callers = entry.getValue();
    
                if (callers != null && !callers.isEmpty()) {
                    System.out.println(method);
                    for (String caller : callers) {
                        System.out.println("    " + caller);
                    }
                }
            }
        }
    
        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 java.util.*;
    
    import org.objectweb.asm.MethodVisitor;
    import org.objectweb.asm.commons.EmptyVisitor;
    
    public class MyClassVisitor extends EmptyVisitor {
    
        private String className;
        private Map<String, Set<String>> callMap;
    
        public MyClassVisitor(Map<String, Set<String>> callMap) {
            this.callMap = callMap;
        }
    
        @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) {
    
            return new MyMethodVisitor(className + "." + name, callMap);
        }
    }
    

    import java.util.*;
    
    import org.objectweb.asm.commons.EmptyVisitor;
    
    public class MyMethodVisitor extends EmptyVisitor {
    
        private String currentMethod;
        private Map<String, Set<String>> callMap;
    
        public MyMethodVisitor(String currentMethod,
                Map<String, Set<String>> callMap) {
            this.currentMethod = currentMethod;
            this.callMap = callMap;
        }
    
        @Override
        public void visitMethodInsn(int opcode, String owner, String name,
                String desc) {
    
            String calledMethod = owner + "." + name;
    
            Set<String> callers = callMap.get(calledMethod);
            if (callers == null) {
                callers = new TreeSet<String>();
                callMap.put(calledMethod, callers);
            }
    
            callers.add(currentMethod);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure about your actual sourcecode, but in your example… May 17, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer Read this post on painless threading, particularly the Activity.runOnUIThread May 17, 2026 at 2:04 am
  • Editorial Team
    Editorial Team added an answer Turns out I was using the image "Android 2.2" instead… May 17, 2026 at 2:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm required to write documentation for my current project that lists all .c files
I am thinking of writing a tool that will list all the tables in
Im looking for a tool that will watch directory(with sub-dirs) and give me the
I'm building a little tool that will download files using wget, reading the urls
I am looking for a tool that will allow me to monitor and control
I'm trying to write a program in Scala, that will accept SOAP-requests, get the
I need to write on-line help (Eclipse help format) for an Eclipse plugin. I
I need a tool/script to fetch network card configurations from multiple Linux machines, mostly
I am trying to write a module for a Java application that accesses a
We have a c# app that uses a number of libraries (that we wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.