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 1073671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:01:17+00:00 2026-05-16T21:01:17+00:00

In a Java Project of mine, I would like to find out programmatically which

  • 0

In a Java Project of mine, I would like to find out programmatically which classes from a given API are used. Is there a good way to do that? Through source code parsing or bytecode parsing maybe? Because Reflection won’t be of any use, I’m afraid.

To make things simpler: there are no wildcard imports (import com.mycompany.api.*;) anywhere in my project, no fully qualified field or variable definitions (private com.mycompany.api.MyThingy thingy;) nor any Class.forName(...) constructs. Given these limitations, it boils down to parsing import statements, I guess. Is there a preferred approach to do this?

  • 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-16T21:01:17+00:00Added an answer on May 16, 2026 at 9:01 pm

    You can discover the classes using ASM‘s Remapper class (believe it or not). This class is actually meant to replace all occurrences of a class name within bytecode. For your purposes, however, it doesn’t need to replace anything.

    This probably doesn’t make a whole lot of sense, so here is an example…

    First, you create a subclass of Remapper whose only purpose in life is to intercept all calls to the mapType(String) method, recording its argument for later use.

    public class ClassNameRecordingRemapper extends Remapper {
    
        private final Set<? super String> classNames;
    
        public ClassNameRecordingRemapper(Set<? super String> classNames) {
            this.classNames = classNames;
        }
    
        @Override
        public String mapType(String type) {
            classNames.add(type);
            return type;
        }
    
    }
    

    Now you can write a method like this:

    public Set<String> findClassNames(byte[] bytecode) {
        Set<String> classNames = new HashSet<String>();
    
        ClassReader classReader = new ClassReader(bytecode);
        ClassWriter classWriter = new ClassWriter(classReader, 0);
    
        ClassNameRecordingRemapper remapper = new ClassNameRecordingRemapper(classNames);
        classReader.accept(remapper, 0);
    
        return classNames;
    }
    

    It’s your responsibility to actually obtain all classes’ bytecode.


    EDIT by seanizer (OP)

    I am accepting this answer, but as the above code is not quite correct, I will insert the way I used this:

    public static class Collector extends Remapper{
    
        private final Set<Class<?>> classNames;
        private final String prefix;
    
        public Collector(final Set<Class<?>> classNames, final String prefix){
            this.classNames = classNames;
            this.prefix = prefix;
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public String mapDesc(final String desc){
            if(desc.startsWith("L")){
                this.addType(desc.substring(1, desc.length() - 1));
            }
            return super.mapDesc(desc);
        }
    
        /**
         * {@inheritDoc}
         */
        @Override
        public String[] mapTypes(final String[] types){
            for(final String type : types){
                this.addType(type);
            }
            return super.mapTypes(types);
        }
    
        private void addType(final String type){
            final String className = type.replace('/', '.');
            if(className.startsWith(this.prefix)){
                try{
                    this.classNames.add(Class.forName(className));
                } catch(final ClassNotFoundException e){
                    throw new IllegalStateException(e);
                }
            }
        }
    
        @Override
        public String mapType(final String type){
            this.addType(type);
            return type;
        }
    
    }
    
    public static Set<Class<?>> getClassesUsedBy(
        final String name,   // class name
        final String prefix  // common prefix for all classes
                             // that will be retrieved
        ) throws IOException{
        final ClassReader reader = new ClassReader(name);
        final Set<Class<?>> classes =
            new TreeSet<Class<?>>(new Comparator<Class<?>>(){
    
                @Override
                public int compare(final Class<?> o1, final Class<?> o2){
                    return o1.getName().compareTo(o2.getName());
                }
            });
        final Remapper remapper = new Collector(classes, prefix);
        final ClassVisitor inner = new EmptyVisitor();
        final RemappingClassAdapter visitor =
            new RemappingClassAdapter(inner, remapper);
        reader.accept(visitor, 0);
        return classes;
    }
    

    Here’s a main class to test it using:

    public static void main(final String[] args) throws Exception{
        final Collection<Class<?>> classes =
            getClassesUsedBy(Collections.class.getName(), "java.util");
        System.out.println("Used classes:");
        for(final Class<?> cls : classes){
            System.out.println(" - " + cls.getName());
        }
    
    }
    

    And here’s the Output:

    Used classes:
     - java.util.ArrayList
     - java.util.Arrays
     - java.util.Collection
     - java.util.Collections
     - java.util.Collections$1
     - java.util.Collections$AsLIFOQueue
     - java.util.Collections$CheckedCollection
     - java.util.Collections$CheckedList
     - java.util.Collections$CheckedMap
     - java.util.Collections$CheckedRandomAccessList
     - java.util.Collections$CheckedSet
     - java.util.Collections$CheckedSortedMap
     - java.util.Collections$CheckedSortedSet
     - java.util.Collections$CopiesList
     - java.util.Collections$EmptyList
     - java.util.Collections$EmptyMap
     - java.util.Collections$EmptySet
     - java.util.Collections$ReverseComparator
     - java.util.Collections$ReverseComparator2
     - java.util.Collections$SelfComparable
     - java.util.Collections$SetFromMap
     - java.util.Collections$SingletonList
     - java.util.Collections$SingletonMap
     - java.util.Collections$SingletonSet
     - java.util.Collections$SynchronizedCollection
     - java.util.Collections$SynchronizedList
     - java.util.Collections$SynchronizedMap
     - java.util.Collections$SynchronizedRandomAccessList
     - java.util.Collections$SynchronizedSet
     - java.util.Collections$SynchronizedSortedMap
     - java.util.Collections$SynchronizedSortedSet
     - java.util.Collections$UnmodifiableCollection
     - java.util.Collections$UnmodifiableList
     - java.util.Collections$UnmodifiableMap
     - java.util.Collections$UnmodifiableRandomAccessList
     - java.util.Collections$UnmodifiableSet
     - java.util.Collections$UnmodifiableSortedMap
     - java.util.Collections$UnmodifiableSortedSet
     - java.util.Comparator
     - java.util.Deque
     - java.util.Enumeration
     - java.util.Iterator
     - java.util.List
     - java.util.ListIterator
     - java.util.Map
     - java.util.Queue
     - java.util.Random
     - java.util.RandomAccess
     - java.util.Set
     - java.util.SortedMap
     - java.util.SortedSet
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this general project folder of mine which includes a variety of stuff:
I'm having trouble deciding if I want a project of mine to be web-based
We normally use Eclipse for a particular Java project, but recently I imported the
I've got a Java application that I'm writing an installer for. We're using the
I am an undergraduate student. I was exposed to basic programming couple of years
I have a JWindow and a JFrame both I have made runnable and both
I've heavily edited this question because responses indicated I wasn't being clear problem: UI
At work I am parsing large XML files using the DefaultHandler class. Doing that,
I have a number of rather large binary files (fixed length records, the layout
I bought a new Vista PC recently but was having lots of problems getting

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.