javassist icon indicating copy to clipboard operation
javassist copied to clipboard

ClassFile.renames fails in presence of generics

Open pietrobraione opened this issue 5 years ago • 0 comments

The method javassist.bytecode.ClassFile.renameClass(Map) fails when the classes to be renamed are generic. Apparently the problem is due to the fact that Javassist does not modify the constant pool entries that contain generic type signatures.

Let us consider the following program:

public class Main {
  public static void main(String[] s) {
    final Path pathOfClass = Paths.get("/some/where/A.class");
    ClassFile cf = new ClassFile(new DataInputStream(new ByteArrayInputStream(Files.readAllBytes(pathOfClass))));
    final HashMap<String, String> renames = new HashMap<>();
    renames.put("A", "B");
    cf.renameClass(renames);
    cf.compact();
    cf.write(new DataOutputStream(new FileOutputStream("/some/where/B.class")));
  }
}

First case:

$ cat A.java
public class A {
  private int x;
  private A(int x) { this.x = x; } 
  public static A make(int x) {
      return new A(x);
  }
}

$ java Main
$ javap B.class
Compiled from "A.java"
public class B {
  public static B make(int);
}

All the occurrences of A are correctly renamed to B.

Second case:

$ cat A.java
public class A<K> {
  private K x;  
  private A(K x) { this.x = x; }   
  public static <W> A<W> make(W x) {
    return new A<W>(x);
  }
}

$ java Main
$ javap B.class
Compiled from "A.java"
public class B<K> {
  public static <W> A<W> make(W);
}

The static make method incorrectly returns an object of class A.

Tested with Javassist 3.26.0-GA and AdoptOpenJDK version 1.8.0_232.

pietrobraione avatar Jan 21 '20 14:01 pietrobraione