我正在尝试使用基于Byte Buddy的Java代理来调用javax.servlet.http.HttpServlet的service()方法。我的代码中的premain函数被正确调用,但是使用堆栈跟踪检测失败:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
at ub.jagent.ServletInstrumentation.instrument(ServletInstrumentation.java:24)
at ub.jagent.StackTracerAgent.premain(StackTracerAgent.java:75)
... 6 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more
FATAL ERROR in native method: processing of -javaagent failed
我使用代理构建器如下:
AgentBuilder b = new AgentBuilder.Default().ignore(ElementMatchers.none())
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.type(ElementMatchers.isSubTypeOf(HttpServlet.class))
.transform((builder, type, classLoader) ->
builder.visit(Advice.to(ServletAdvice.class)
.on(ElementMatchers.named("service")
.and(ElementMatchers.takesArgument(0, HttpServletRequest.class))
.and(ElementMatchers.takesArgument(1, HttpServletResponse.class)))).installOn(inst);
我的建议实施如下:
public class ServletAdvice {
@Advice.OnMethodEnter
public static void before(@Advice.This Object obj,
@Advice.Argument(value = 0) HttpServletRequest a1,
@Advice.Argument(value = 1) HttpServletResponse a2,
@Advice.Origin Method method)
{
System.out.println("\nRunning pre-method logic for " + obj.getClass().getCanonicalName() + ":" + method.getName() + "()");
<...etc...>
}
}
}
奇怪的是,当我匹配特定的Tomcat实现类时:
.type(ElementMatchers.named("org.glassfish.jersey.servlet.ServletContainer"))
...然后代理设置工作(尽管Advice方法中对HttpServletRequest和HttpServletResponse的引用在检测本身期间触发ClassNotFoundException)。
这是否意味着我必须在代理.jar文件本身中包含所有代理依赖项(包括Java Servlet API)?
我正在使用ByteBuddy 1.6.2,Apache Tomcat v8.0。
修改 修改后的工作代码(有Rafael的帮助)看起来像:
.type(ElementMatchers.isSubTypeOf(new TypeDescription.Latent("javax.servlet.GenericServlet", Modifier.PUBLIC | Modifier.ABSTRACT, TypeDescription.Generic.OBJECT, null)))
.transform(
new AgentBuilder.Transformer.ForAdvice()
.include(getClass().getClassLoader())
.advice(
ElementMatchers.named("service")
.and(ElementMatchers.takesArgument(0, ElementMatchers.named("javax.servlet.http.HttpServletRequest"))
.and(ElementMatchers.takesArgument(1, ElementMatchers.named( "javax.servlet.http.HttpServletResponse")),
ServletAdvice.class.getName())
);
答案 0 :(得分:0)
我假设类型的servlet-family不可用于Byte Buddy用于解析建议的类加载器。为了解决这个问题,Byte Buddy提供了一个特定的转换器,它解析了用户和代理的类加载器的建议类:
new AgentBuilder.Transformation.ForAdvice()
.include(getClass().getClassLoader())
.advice("your.pkg.ServletAdvice")
对于元素匹配器,而不是匹配参数&#39;然后使用类型常量命名。