2017-04-19 78 views
1

试图写一个简单的java代理,基于bytebuddy主页上的示例。我让代理工作,但是当我用OSGI运行时运行它时,它会抛出java.lang.NoClassDefFoundError。bytebuddy与osgi容器

任何指针?

java.lang.ClassNotFoundException: com.foo.javaagent.TimingInterceptor cannot be found by .. 

import net.bytebuddy.agent.builder.AgentBuilder; 
import net.bytebuddy.implementation.MethodDelegation; 
import net.bytebuddy.matcher.ElementMatchers; 
import java.lang.instrument.Instrumentation; 


    public class TimerAgent { 
     public static void premain(String arguments, 
            Instrumentation instrumentation) { 
      new AgentBuilder.Default() 
        .type(ElementMatchers.nameEndsWith("World")) 
        .transform((builder, type, classLoader, module) -> 
          builder.method(ElementMatchers.any()) 
            .intercept(MethodDelegation.to(TimingInterceptor.class)) 
        ).installOn(instrumentation); 
     } 
    } 

回答

0

TimingInterceptor类是由你的仪表类引用,因此必须是可见的。 OSGi按类加载器隔离类,并且不会将系统类加载器设置为加载代理程序的父类。为了避免这种情况,你需要将你的类注入到它们普遍可见的引导类加载器中。为此,请将截取逻辑隔离在单独的jar中,并通过用于安装代理的实例将此jar添加到引导程序类加载器搜索路径。您需要在安装代理之前执行此操作。

+0

拉斐尔 - 非常感谢您的快速回复。你已经创作了一个很棒的图书馆。我能够得到它的工作。有没有关于如何修改方法输入参数,HTTP拦截器与字节好友的例子? – basu76