1

Cannot resolve type description在为. AgentBuilder.Listener.onError(...)_AgentBuilderInstrumentation

代理代码为:

  public static void premain(final String agentArgs, final Instrumentation inst) {
    System.out.println("from bytebuddy: agent premain");
    new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED))
      .disableClassFormatChanges()
      .ignore(none())
      .with(RedefinitionStrategy.RETRANSFORMATION)
      .with(InitializationStrategy.NoOp.INSTANCE)
      .with(TypeStrategy.Default.REDEFINE)
      .with(new AgentBuilder.Listener() {
        public void onDiscovery(final String typeName, final ClassLoader classLoader, final JavaModule module, final boolean loaded) {
          log("Event::onDiscovery(" + typeName + ", " + getNameId(classLoader) + ", " + module + ", " + loaded + ")");
        }

        public void onTransformation(final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module, final boolean loaded, final DynamicType dynamicType) {
          log("Event::onTransformation(" + typeDescription.getName() + ", " + getNameId(classLoader) + ", " + module + ", " + loaded + ", " + dynamicType + ")");
        }

        public void onIgnored(final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module, final boolean loaded) {
          log("Event::onIgnored(" + typeDescription.getName() + ", " + getNameId(classLoader) + ", " + module + ", " + loaded + ")");
        }

        public void onError(final String typeName, final ClassLoader classLoader, final JavaModule module, final boolean loaded, final Throwable throwable) {
          log("Event::onError(" + typeName + ", " + getNameId(classLoader) + ", " + module + ", " + loaded + ")", throwable);
        }

        public void onComplete(final String typeName, final ClassLoader classLoader, final JavaModule module, final boolean loaded) {
          log("Event::onComplete(" + typeName + ", " + getNameId(classLoader) + ", " + module + ", " + loaded + ")");
        }
      })
      .type(hasSuperType(named("com.ning.http.client.AsyncHttpClientConfig$Builder")))
      .transform(new Transformer() {
        public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder, final     
          System.out.println("from bytebuddy: transform");
          TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
          return builder.visit(Advice.to(AHCBuilderExit.class).on(isDefaultConstructor()));
        }})
      .installOn(inst);
  }

这是使用 ByteBuddy v1.10.2 和 Mule 4 (jdk1.8) 作为目标应用程序。

这里有一篇关于同样问题的类似帖子,但回复并没有让我找到解决方案。

输出显示“来自 bytebuddy:agent premain”,但未显示“来自 bytebuddy:transform”。相反,日志显示属于引导类加载器的许多类的异常,例如:

Event::onError(org.mule.weave.v2.el.WeaveExpressionLanguage, org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader@6d4b1d14, null, false)
java.lang.IllegalStateException: Cannot resolve type description for java.lang.Exception
    at net.bytebuddy.pool.TypePool$Resolution$Illegal.resolve(TypePool.java:159)
    at net.bytebuddy.pool.TypePool$Default$WithLazyResolution$LazyTypeDescription.delegate(TypePool.java:914)
    at net.bytebuddy.description.type.TypeDescription$AbstractBase$OfSimpleType$WithDelegation.getSuperClass(TypeDescription.java:8031)
    at net.bytebuddy.description.type.TypeDescription$Generic$OfNonGenericType.getSuperClass(TypeDescription.java:3619)
    at net.bytebuddy.description.type.TypeDefinition$SuperClassIterator.next(TypeDefinition.java:314)
    at net.bytebuddy.description.type.TypeDefinition$SuperClassIterator.next(TypeDefinition.java:281)
    at net.bytebuddy.matcher.HasSuperTypeMatcher.matches(HasSuperTypeMatcher.java:53)
...

似乎正在查询类型描述的类加载器没有提供所需的类。ClassFileLocator有没有办法为这些查找指定替代方案?

导致错误的特定类加载器是: org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader

有趣的是,当使用 Byteman 作为代理时,该工具按预期工作,具有以下规则文件:

RULE AsyncHttpClientConfig.Builder.<init>
CLASS com.ning.http.client.AsyncHttpClientConfig$Builder
METHOD <init>
AT EXIT
IF TRUE
DO
  traceln("from byteman: AsyncHttpClientConfig$Builder.<init> triggered: " + getClass().getClassLoader());
ENDRULE

任何意见,将不胜感激!谢谢您的帮助!

4

1 回答 1

1

堆栈跟踪显示您正在对 Byte Buddy 应用超类型检查,而您不与 Byteman 一起使用。如果你hasSuperType(named(...))用 just 替换你的匹配器named(...),它可能会在没有错误的情况下工作,因为这似乎是错误发生的地方。Byte Buddy 需要解析类层次结构中的所有类,以便能够应用此匹配器,这在您的情况下似乎是不可能的。

然而,aClassLoader无法解析类文件java.lang.Exception似乎很奇怪。您正在使用的类加载器可能是一个不委托给引导加载器的自定义实现。

您可以通过在您的代理中定义一个LocationStrategy始终查询引导加载程序作为替代选项的自定义来规避此问题:

class BootFallbackLocationStrategy implements AgentBuilder.LocationStrategy {

  @Override
  public ClassFileLocator classFileLocator(ClassLoader classLoader, JavaModule module) {
     return new ClassFileLocator.Compound(
       ClassFileLocator.ForClassLoader.of(classLoader),  
       ClassFileLocator.ForClassLoader.ofBootLoader())
  }
}

正如我在评论中提到的,如果您可以命名无法解析类文件的类加载器的类型,我也许可以提供更详细的答案。

我将在 Byte Buddy 的未来版本中添加这种定位策略的标准实现,以便更轻松地解决这个问题。在这张票中跟踪进度:https ://github.com/raphw/byte-buddy/issues/794

于 2019-12-17T09:16:22.170 回答