Display typical type parameters from compiled class files

Is there a javap-like tool that can display methods and fields with their original (not erased) types?

I know that type information is erased when compiling the source code. However, the compiler must somehow know, because it can determine if my calls correspond to the library methods and their signatures, even if the library is already compiled into class files. Therefore, at least in theory, it should be possible to obtain type information.

I searched further and I found this answer: Where are generic types stored in java class files? which pointed me to getGeneric methods ... () . Therefore, it seems to me that type information is stored in class files in some complicated way, and these methods allow it to be obtained. But so far I have not found a command line tool that would display this information.

+2
source share
4 answers

A decompiler, such as http://java.decompiler.free.fr/ , will give you approximate source code, including generic signatures.

+1
source

The old version of javap .

 $ javap -version 1.7.0 $ javap java.util.List Compiled from "List.java" public interface java.util.List<E> extends java.util.Collection<E> { public abstract int size(); public abstract boolean isEmpty(); public abstract boolean contains(java.lang.Object); public abstract java.util.Iterator<E> iterator(); public abstract java.lang.Object[] toArray(); public abstract <T extends java/lang/Object> T[] toArray(T[]); public abstract boolean add(E); public abstract boolean remove(java.lang.Object); public abstract boolean containsAll(java.util.Collection<?>); public abstract boolean addAll(java.util.Collection<? extends E>); public abstract boolean addAll(int, java.util.Collection<? extends E>); public abstract boolean removeAll(java.util.Collection<?>); public abstract boolean retainAll(java.util.Collection<?>); public abstract void clear(); public abstract boolean equals(java.lang.Object); public abstract int hashCode(); public abstract E get(int); public abstract E set(int, E); public abstract void add(int, E); public abstract E remove(int); public abstract int indexOf(java.lang.Object); public abstract int lastIndexOf(java.lang.Object); public abstract java.util.ListIterator<E> listIterator(); public abstract java.util.ListIterator<E> listIterator(int); public abstract java.util.List<E> subList(int, int); } 
+2
source

A very good search tool in .class is JClasslib . This is a much better alternative than javap . Example screenshot:

enter image description here

You can use it to view all the fields inside the .class file, all methods, byte code, everything. It can also open JAR files.

You can look in the section "Attributes" โ†’ Signature. There you can find such information.

For example: for the following class definition:

 public class BlaType<T extends Integer> 

I see something like this:

enter image description here

+1
source

You can try Method.toGenericString() , and that seems to all that you can get from the class at runtime. This method should print the signature of the method with common borders.

0
source

All Articles