java - CGLib Enhancer with anonymous class -


I am using CGLib for proxy classes. I have turned down a problem where I can not increase anonymous classes because they do not have any default constructor.

My code looks like this:

  Enhancer enhancer = new enhance (); Enhancer.setCallback (new method interceptor () {....}); Enhancer.setSuperclass (type); // Type is the class that I am trying to increase .create ();   

However this throws the following exception if the enhanced category is anonymous (no default constructor).

  java.lang.IllegalArgumentException: There is no empty constructor in Superclass   

>

The validator of a JVM assures you that you call a valid Constructor Series. The only way around this is to disable the verifier while starting the JVM with the command line and it is not something you usually want to do because it presents many insecurities for your application.

In this way, you have to call a legitimate constructor of your anonymous class which has been emulated by the subclass created by cglib the creator of this class has the form of this kind of logic Takes an example of its attached class in If you use cglib to create a pure proxy which never calls the real method , then you assign it to null You can still consider it, but you still need it. Before you argue, select the right creator.

You can get it by calling:

  enhancer.create (new class   

Where the call above selects the right constructor and initializes the anonymous class's external instance field with the null value.

Alternatively, you can use the internal Open ZK class ReflectionFactory to create an instance without calling any constructor. This is possible after calling enhancer.createClass () . For this, though additional preparation is required because you need to specify a different callback.

However, you may only want to leave cglib allgotglander as a disclaimer, I am the author of an alternative library which I would recommend to you. Using, you have more freedom in creating classes and it is easy to define your logic using it. Using Byte Buddy, you can write

  new bytebird () .subclass (type) .method (any). Blocking exceptions (MethodDelegation.to (New Object) (@RuntimeType Public Object Blocking (@SuperCall Callbill & lt ;? & gt; c, @gang method m, @ All Arguments Objects) A) Exceptions {// your interception logic Apply}}).);   

And get the same logic. Remove the (annotated) arguments for which you do not need to call your blockage

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -