Getting a list of referenced types in a .class file

Compile the following Java source file with java -g.

Test.java:

public class Test
{
   private int          privateMember = 10;
   protected float      protectedMember = 20;
   public double        publicMember = 30.0;
 
   public Test() {
   }
   
   private void privateMethod(String argument) {
      int localMember = 40;
   }
  
   protected String protectedMethod() {
      String localMember = "Hello, World!";
 
      return localMember;
   }
  
   public void publicMethod() {
   }
}

Make sure Test.class in on your classpath and run Main:
Main.java:

import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.*;
 
public class Main
{
   public static void main(String []args) {
      JavaClass clazz = Repository.lookupClass("Test");
      ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
 
      Field[] fields = clazz.getFields();
      for (int i=0; i<fields.length; i++) {
         System.out.println(fields[i].getSignature());
      }
 
      Method[] methods = clazz.getMethods();
      for (int i=0; i<methods.length; i++) {
         Code code = methods[i].getCode();
         LocalVariableTable localVariableTable = code.getLocalVariableTable();
         if (localVariableTable != null) {            
            LocalVariable[] localVars = localVariableTable.getLocalVariableTable();
            for (int j=0; j<localVars.length; j++) {
               System.out.println(localVars[j].getSignature());
            }
         }
      }
   }
}

output:

I
F
D
LTest;
Ljava/lang/String;
Ljava/lang/String;
LTest;
LTest;
LTest;