Skip to content
Advertisement

Is there a way to see what my code looks like after type erasure?

I would like to know what tool can be used to view code after type erasure. For example, I’d like to see what the following code looks like after type erasure. I know that generic type information is stripped prior to final compilation, replacing type references with Object or boundary types, creating bridge methods, etc. I want to see the source code after the erasure process takes place.

  public static void main(String[] args) {
    Predicate<Object> objPredicate = (Object c) -> c.toString().length() > 2 ;
    Predicate<? super Integer> predicate = objPredicate;
    Number n = Integer.valueOf(22);
    System.out.println(predicate.test(n)); //this line does not compile
  }

[If compiling code is required to see an erasure inspection tool work on the example, the non-compiling line can be omitted or n can be cast to Integer]

Advertisement

Answer

On github, I found the java-based open source gui tool bytecode viewer which did the job as follows:

 public static void main(String[] var0) {
      Predicate var1 = (var0x) -> {
         return var0x.toString().length() > 2;
      };
      Integer var3 = 22;
      System.out.println(var1.test(var3));
   }

To get this, I set the view menu to Pane 2/Fernflower/Java.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement