Below snippet return new instance from CtClass.
CtClass demoClass = .....;
Class<?> clazz = classPool.toClass(demoClass);
Object obj = clazz.newInstance();
Find the below working application.
App.java
package com.sample.app;
import javassist.ClassPool;
import javassist.CtClass;
public class App {
public static void main(String args[]) throws Exception {
ClassPool classPool = ClassPool.getDefault();
CtClass demoClass = classPool.makeClass("com.sample.app.model.Demo");
Class<?> clazz = classPool.toClass(demoClass);
Object obj = clazz.newInstance();
System.out.println(obj.getClass());
}
}
Run App.java, you will see below messages in console.
class com.sample.app.model.Demo
No comments:
Post a Comment