できなかった。
Stringクラスの拡張をしたかった。
ExtendSample.java
package javassist.test;
import javassist.ClassPool;
import javassist.CtClass;
public class ExtendSample {
public static void main(String[] args) throws Exception{
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass("dynamic.clazz.Sample");
cc.setSuperclass(pool.get("java.lang.String"));
Class<?> clazz = cc.toClass();
Object obj = clazz.newInstance();
}
}
Exception in thread "main" javassist.CannotCompileException: by java.lang.VerifyError: Cannot inherit from final class
at javassist.ClassPool.toClass(ClassPool.java:1099)
at javassist.ClassPool.toClass(ClassPool.java:1042)
at javassist.ClassPool.toClass(ClassPool.java:1000)
at javassist.CtClass.toClass(CtClass.java:1224)
at javassist.test.ExtendSample.main(ExtendSample.java:11)
Caused by: java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javassist.ClassPool.toClass2(ClassPool.java:1112)
at javassist.ClassPool.toClass(ClassPool.java:1093)
... 4 more