6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javassistを使ってJavaのfinalクラスの拡張は・・・

Posted at

できなかった。
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
6
6
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?