What you are looking for is an erase type , not a type inference .
I do not know any option for javac that outputs type erase data.
Essentially, the compiler erases all type parameters to the smallest known type obtained from the type parameter:
<T> becomes Object<T extends Number> becomes Number<T extends Comparable<T>> becomes Comparable<T extends Cloneable & Comparable<T>> becomes Cloneable<T extends Object & Comparable<T>> becomes Object<S, T extends S> becomes Object,Object
You can always check your .class file by calling javap -c <class_name>.class to see what the resulting bytecode is.
* NB Keep in mind that the compiler has various keys to save debugging information. See javac -g .
So, in terms of the experiment below, your code is slightly reworked (note type parameter in the class declaration and sneakyThrow(T t) throws T
public class Scratch <T extends Throwable> { private static <T extends Throwable> void sneakyThrow(T t) throws T { throw (T) t; } public void foo() throws T { sneakyThrow((T) new Exception()); } }
... and below is part of the output of javap -v -c Scratch.class after compilation with javac -g:none Scratch.java :
Constant pool: #1 = Methodref #6.#19
As you can see, Throwable is what type erasure is allowed.
source share