What is the best way to do a static send bytecode generated proxy in java at present?

I have ue cglib in th epast, but to be honest, I have to believe that there is a more convenient way than the callback and callback method in cglib. I know there used to be aspectwerkz proxy. but he seems to be wandering somewhere.

+3
source share
2 answers

If you just need simple proxies with minimal bytes of hits, try janino ( http://docs.codehaus.org/display/JANINO/Home ):

final String bodyText=
"public Object get(Object obj) {return null;}\n"+
"public void set(Object obj, Object val) {}\n"+
"public Class getPropertyType() {return Void.class;}\n"+
"public boolean isPrimitive() {return true;}\n";

return (PropHandle)
   ClassBodyEvaluator.createFastClassBodyEvaluator(
   new Scanner(target+"__"+property, new StringReader(bodyText)),
   PropHandle.class, // Base type to extend/implement
   (ClassLoader)null); // Use current thread context class loader

This is a snippet from my ORM that generates accessors.

If you really want to work at the bytecode level, try Javassist - it has a pretty nice interface.

+2

Javassist - Java:

CtClass point = ClassPool.getDefault().get("Point");
CtMethod m = CtNewMethod.make(
             "public int xmove(int dx) { x += dx; }",
             point);
point.addMethod(m);
+3

Source: https://habr.com/ru/post/1793402/


All Articles