Instrument problems using Byte Buddy

I have Byte Buddy and works as an agent, and it successfully intercepts the vast majority of my code base, which is quite large! Although there are several phenomena that I could not use, and which I described below, in the hope that you may know the answer!

1. Generated CGLIB classes

Spring generates some additional classes with the same name as my classes, but with the addition "$$EnhancerByCGLIB$$"to the end, and this causes errors. The error I am getting is:

java.lang.IllegalStateException: Cannot resolve type description for com.mycompany.MySpringBean$$EnhancerByCGLIB$$ee9d3c37_2
at net.bytebuddy.pool.TypePool$Resolution$Illegal.resolve(TypePool.java:152)
at net.bytebuddy.pool.TypePool$LazyFacade$LazyResolution$LazyTypeDescription.resolve(TypePool.java:3158)
at net.bytebuddy.pool.TypePool$LazyFacade$LazyResolution$LazyTypeDescription.getModifiers(TypePool.java:3238)
at net.bytebuddy.ByteBuddy.rebase(ByteBuddy.java:697)

In truth, I am not interested in the tools for creating CGLIB classes and therefore want to exclude them. What is the best approach? Right now I'm matching by name, but I think this is the best way.

.and(not(nameContains("EnhancerByCGLIB")))

2. Package Private and private classes

, , - package private private class.

package private :

abstract class BaseBean {
    Object methodA(final String customerNumber){}
}

, :

Caused by: java.lang.IllegalAccessException:
    Class net.bytebuddy.implementation.LoadedTypeInitializer$ForStaticField cannot 
    access a member of class com.mycompany.BaseBean with modifiers "public static"

private class :

public class Object A {
 //variables & methods...
 private class ObjectB {}
}

, :

Caused by: java.lang.IllegalAccessException:
    Class net.bytebuddy.implementation.LoadedTypeInitializer$ForStaticField cannot 
    access a member of class com.mycompany.ObjectA$ObjectB with modifiers "public static"

Byte Buddy package private private classes?

3.

, Byte Buddy ? , Byte Buddy .

4.

:

package com.mycompany;
public interface InterfaceA{
    Object provideAccess(final String id);
}

package com.mycompany;
public abstract class BaseBeanA implements InterfaceA { 
  //some general helper methods
}

package com.mycompany;
public abstract class BaseBeanB extends BaseBeanA { 
    //some specific helper methods
}

package com.mycompany;
public class BeanImpl extends BaseBeanB {   
    protected Object provideAccess(final String id) {
    }
}

BaseBeanB:

java.lang.IllegalArgumentException: [protected void java.lang.Object.finalize() java.lang.Throwable, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long, int) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException, public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll(), public java.lang.Object com.mycompany.MyInterceptor.intercept(java.util.concurrent.Callable, java.lang.Object [], java.lang.reflect.Method, java.lang.Class) throws java.lang.Exception] com.mycompany.InterfaceA.provideAccess(java.lang.String)

+4
1

1. CGLIB

. , . , , Byte Buddy , , .

, , cglib, , .

2.

. , , , . 0.7-rc7.

3.

, AgentBuilder.Listener. , , API ClassFileTransformer. - , ? , AgentBuilder .

4.

Callable, , , SuperCall. -, super, . , , , , , . .

, Object , Byte Buddy . .

+5

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


All Articles