public static BeanInterface createJavassistBean() {
try {
ClassPool classPool = new ClassPool();
classPool.appendClassPath(new LoaderClassPath(BeanInterface.class
.getClassLoader()));
CtClass theClass = classPool
.makeClass(ClassFabUtils.generateClassName( BeanInterface.class ) );
theClass.addInterface(classPool.get(BeanInterface.class.getName()));
CtMethod theMethod = new CtMethod(
classPool.get("java.lang.String"), "interfaceMethod",
new CtClass[0], theClass);
theMethod.setBody("return \"Hello, World!\";");
theClass.addMethod(theMethod);
Class clazz = theClass.toClass();
return ( BeanInterface )clazz.newInstance();
} catch (Exception e) {
throw new ApplicationRuntimeException("Cannot construct instance.",
e);
}