ByteBuddy变基,合成类型和OSGi [英] ByteBuddy rebasing, synthetic types and OSGi

查看:177
本文介绍了ByteBuddy变基,合成类型和OSGi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为字节伙伴开发了以下拦截器:

I have the following interceptor developed for byte-buddy:

public class SecurityInterceptor() {

    @RuntimeType
    public static Object intercept(
        @SuperCall Callable<Object> supercall, 
        @This Object target, 
        @Origin Method method, 
        @AllArguments Object[] args) {  

        // Check args and annotations ...       

        Object obj = supercall.call();

        // Post-process obj content ...
    }
}

拦截器的注册如下:

Unloaded<Object> unloaded = new ByteBuddy()
    .rebase(type, classFileLocator)
    .method(ElementMatchers.isAnnotatedWith(Secured.class))
    .intercept(MethodDelegation.to(SecurityInterceptor.class))
    .make();
wovenClass.setBytes(unloaded.getBytes());

,这发生在OSGi中的WeavingHook内部.问题在于@SuperCall的重新设置会更改原始代码,

and this happens inside a WeavingHook in OSGi. The problem is that rebasing with @SuperCall alters the original code as such

public User getUser(final String s) throws Exception {
    return SecurityInterceptor.intercept((Callable)new UsersServiceImpl$auxiliary$xhbBRSr4(this, s), 
        (Object)this, UsersServiceImpl.cachedValue$nlgHrwy3$sn5qca3, new Object[] { s });
}

其中UsersServiceImpl$auxiliary$xhbBRSr4是由字节伙伴生成的综合类:

where UsersServiceImpl$auxiliary$xhbBRSr4 is a synthetic class that is generated by byte-buddy:

class UsersServiceImpl$auxiliary$xhbBRSr4 implements Runnable, Callable
{
    private UsersServiceImpl argument0;
    private String argument1;

    @Override
    public Object call() throws Exception {
        return this.argument0.getUser$original$6ve6X5gN$accessor$nlgHrwy3(this.argument1);
    }

    @Override
    public void run() {
        this.argument0.getUser$original$6ve6X5gN$accessor$nlgHrwy3(this.argument1);
    }

    UsersServiceImpl$auxiliary$xhbBRSr4(final UsersServiceImpl argument0, final String argument2) {
        this.argument0 = argument0;
        this.argument1 = argument2;
    }
}

其中UsersServiceImpl是要编织的类.

因此,我需要在UsersServiceImpl包的类空间中添加所有这些综合类(或通常使该类中的可访问"综合类).这可能吗?

So what I need is to add all these synthetic classes in the class space of the UsersServiceImpl's bundle (or in general make synthetic classes "accessible" from that bundle). Is this possible?

推荐答案

最后,我使用了另一种方法:

In the end I used a different approach:

Unloaded<Object> unloaded = new ByteBuddy()
    .redefine(type, classFileLocator)
    .visit(Advice.to(SecurityAdvice.class)
            .on(ElementMatchers.isAnnotatedWith(Secured.class)))
    .make();

public class SecurityAdvice {
    @Advice.OnMethodEnter
    private static void enter(@Advice.AllArguments Object[] args) {
        //...
    }

    @Advice.OnMethodExit
    private static void exit(@Advice.Return(typing = Typing.DYNAMIC) Object value) {
       //...
    }
}

这只会更改原始类的字节码,而不会引入其他合成类型.

this only alters the original class's bytecode without introducing additional synthetic types.

这篇关于ByteBuddy变基,合成类型和OSGi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆