Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassFile.renames fails in presence of generics #298

Open
pietrobraione opened this issue Jan 21, 2020 · 0 comments
Open

ClassFile.renames fails in presence of generics #298

pietrobraione opened this issue Jan 21, 2020 · 0 comments

Comments

@pietrobraione
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant