http://prismoskills.appspot.com/lessons/Super_Java/Insert_code_at_runtime.jsp
Javassist is a Java byte-code editor which allows to:
{
static String pkgName = "com.prismoskills.javassist.examples.annotation";
public static void main(String[] args) throws Exception
{
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(pkgName + ".FooCodeInsertion");
// Without the call to "makePackage()", package information is lost
cp.makePackage(cp.getClassLoader(), pkgName);
CtMethod m = cc.getDeclaredMethod("helloWorld");
m.insertBefore("{System.out.print(\"Oh, say no to \");}");
m.insertAfter("{System.out.print(\"And say - Hi World\");}");
// Changes are not persisted without a call to "toClass()"
cc.toClass();
(new FooCodeInsertion()).helloWorld();
}
}
http://prismoskills.appspot.com/lessons/Super_Java/Dynamically_adding_annotations.jsp
public static void main(String[] args) throws Exception
{
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(pkgName + ".SomeClass");
// Without the call to "makePackage()", package information is lost
cp.makePackage(cp.getClassLoader(), pkgName);
addAnnotation(cc, "someField", "java.lang.Deprecated");
// Changes are not persisted without a call to "toClass()"
Class<?> c = cc.toClass();
Field fld = SomeClass.class.getDeclaredField("someField");
System.out.println(c.getPackage());
System.out.println(fld.getAnnotation(Deprecated.class));
}
public static void addAnnotation(
CtClass clazz,
String fieldName,
String annotationName) throws Exception
{
ClassFile cfile = clazz.getClassFile();
ConstPool cpool = cfile.getConstPool();
CtField cfield = clazz.getField(fieldName);
AnnotationsAttribute attr =
new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation(annotationName, cpool);
attr.addAnnotation(annot);
cfield.getFieldInfo().addAttribute(attr);
}
Javassist is a Java byte-code editor which allows to:
- Define a new class at runtime
- Modify a class file when the JVM loads it
- Insert byte-code in the form of source text, which is compiled on the fly by Javassist
- Add new methods to classes
{
static String pkgName = "com.prismoskills.javassist.examples.annotation";
public static void main(String[] args) throws Exception
{
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(pkgName + ".FooCodeInsertion");
// Without the call to "makePackage()", package information is lost
cp.makePackage(cp.getClassLoader(), pkgName);
CtMethod m = cc.getDeclaredMethod("helloWorld");
m.insertBefore("{System.out.print(\"Oh, say no to \");}");
m.insertAfter("{System.out.print(\"And say - Hi World\");}");
// Changes are not persisted without a call to "toClass()"
cc.toClass();
(new FooCodeInsertion()).helloWorld();
}
}
http://prismoskills.appspot.com/lessons/Super_Java/Dynamically_adding_annotations.jsp
public static void main(String[] args) throws Exception
{
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(pkgName + ".SomeClass");
// Without the call to "makePackage()", package information is lost
cp.makePackage(cp.getClassLoader(), pkgName);
addAnnotation(cc, "someField", "java.lang.Deprecated");
// Changes are not persisted without a call to "toClass()"
Class<?> c = cc.toClass();
Field fld = SomeClass.class.getDeclaredField("someField");
System.out.println(c.getPackage());
System.out.println(fld.getAnnotation(Deprecated.class));
}
public static void addAnnotation(
CtClass clazz,
String fieldName,
String annotationName) throws Exception
{
ClassFile cfile = clazz.getClassFile();
ConstPool cpool = cfile.getConstPool();
CtField cfield = clazz.getField(fieldName);
AnnotationsAttribute attr =
new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation(annotationName, cpool);
attr.addAnnotation(annot);
cfield.getFieldInfo().addAttribute(attr);
}