You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have tried to add a new method to an record (Java 17), but javassist corrupts the class signature when CtNewMethod#make(CtClass, String, CtClass[], CtClass[], String, CtClass) is used.
The class signature / header in this example will get changed from "public record JavasisstTestClass(String testValue)" to "public record JavasisstTestClass(testValue Ljavassist/test/JavasisstTestClass;)", which is broken.
Record to enhance:
package javassist.test;
public record JavasisstTestClass(String testValue) {
}
Reproducer:
package javassist.test;
import javassist.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavassistTest {
public static void main(String[] args) throws Exception {
ClassPool cp = new ClassPool(true);
CtClass ctClass = cp.get("javassist.test.JavasisstTestClass");
// doesn't work, the class signature gets corrupted
// The class signature will be 'public record JavasisstTestClass(testValue Ljavassist/test/JavasisstTestClass;)'
// instead of 'public record JavasisstTestClass(String testValue)'.
CtMethod newMethod = CtNewMethod.make(CtClass.booleanType, "isTest", null, null, "{ return false; }", ctClass);
ctClass.addMethod(newMethod);
dump(ctClass);
}
private static void dump(CtClass ctClass) throws IOException, CannotCompileException, NotFoundException {
byte[] bytecode = ctClass.toBytecode();
dump(ctClass.getName(), bytecode);
}
private static void dump(String className, byte[] classBytes) throws IOException {
Path path = Paths.get("C:/Temp/bytecode/javassist");
Path file = path.resolve(className.replace('.', '/') + ".class");
Files.createDirectories(file.getParent());
Files.write(file, classBytes, new OpenOption[0]);
}
}
The text was updated successfully, but these errors were encountered:
I have tried to add a new method to an record (Java 17), but javassist corrupts the class signature when CtNewMethod#make(CtClass, String, CtClass[], CtClass[], String, CtClass) is used.
The class signature / header in this example will get changed from "public record JavasisstTestClass(String testValue)" to "public record JavasisstTestClass(testValue Ljavassist/test/JavasisstTestClass;)", which is broken.
Record to enhance:
Reproducer:
The text was updated successfully, but these errors were encountered: