gpt4 book ai didi

java - 告诉bytebuddy到 "not care"关于通用信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:01 24 4
gpt4 key购买 nike

所以我遇到了

Exception in thread "Thread-0" java.lang.IllegalArgumentException: Unknown type: null
at net.bytebuddy.description.type.TypeDefinition$Sort.describe(TypeDefinition.java:213)
at net.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType$ForLoadedType$ParameterArgumentTypeList.get(TypeDescription.java:4595)
at net.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType$ForLoadedType$ParameterArgumentTypeList.get(TypeDescription.java:4569)
at java.util.AbstractList$Itr.next(AbstractList.java:358)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor.onParameterizedType(TypeDescription.java:1556)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor$ForDetachment.onParameterizedType(TypeDescription.java:1709)
at net.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType.accept(TypeDescription.java:4407)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor.onParameterizedType(TypeDescription.java:1557)
at net.bytebuddy.description.type.TypeDescription$Generic$Visitor$Substitutor$ForDetachment.onParameterizedType(TypeDescription.java:1709)
at net.bytebuddy.description.type.TypeDescription$Generic$OfParameterizedType.accept(TypeDescription.java:4407)
at net.bytebuddy.description.type.TypeDescription$Generic$LazyProjection.accept(TypeDescription.java:5308)
at net.bytebuddy.description.field.FieldDescription$AbstractBase.asToken(FieldDescription.java:143)
at net.bytebuddy.description.field.FieldDescription$AbstractBase.asToken(FieldDescription.java:87)
at net.bytebuddy.description.field.FieldList$AbstractBase.asTokenList(FieldList.java:47)
at net.bytebuddy.dynamic.scaffold.InstrumentedType$Factory$Default$1.represent(InstrumentedType.java:222)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:698)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:676)
at parc.Foo.redefineClass(Foo.java:137)

当试图重新定义一个已经加载了 JVM 加载的字节码的类时。

代码处于初步阶段,已经被 soot-framework 转换,我们怀疑一些签名属性可能已经过时或在该过程中丢失,而 ByteBuddy 只是坚持它所没有的信息的正确性。没有。

严格来说,ByteBuddy 也不需要这些信息。 (很明显,看看 signature 属性是如何可选的,以及类是如何被 JVM 加载和运行的就好了。)因此,一种快速检查的方法是告诉 byteBuddy 根本不在乎,看看是否有任何改变。

有没有办法以这种方式配置ByteBuddy?

(ByteBuddy 版本为1.7.9)

(项目需要 Java 7)

(类重载完成

private void redefineClass(String classname, byte[] bytecode) {
ClassFileLocator cfl = ClassFileLocator.Simple.of(classname,bytecode);

Class clazz;
try{
clazz = Class.forName(classname);
}catch(ClassNotFoundException e){
throw new RuntimeException(e);
}

Debug._print("REDEFINING %s",clazz.getName());

new ByteBuddy()
.redefine(clazz,cfl)
.make()
.load(clazz.getClassLoader(),ByteBuddyConfig.reloadingStrategy)
;
}

public class ByteBuddyConfig {

static final ClassReloadingStrategy reloadingStrategy;
static {
try {
reloadingStrategy = new ClassReloadingStrategy(
(Instrumentation) ClassLoader.getSystemClassLoader()
.loadClass("net.bytebuddy.agent.Installer")
.getMethod("getInstrumentation")
.invoke(null),
ClassReloadingStrategy.Strategy.RETRANSFORMATION);
}catch(ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e){
throw new RuntimeException(e);
}
}
}

感谢来自 how to debug an internal error? 的@kutschkern)

最佳答案

我想无论 ByteBuddy前端在这里做的,是对所有其他操作的支持的一部分,您可以链接以执行另一个转换。如 the answer to your other question 中所述,如果已经有字节码,可以跳过这些操作:

ClassReloadingStrategy s = ClassReloadingStrategy.fromInstalledAgent();
s.load(clazz.getClassLoader(),
Collections.singletonMap(new TypeDescription.ForLoadedType(clazz), bytecode));

在 Java 8 之前,您需要 Collections.<TypeDescription,byte[]>singletonMap(…) .

当类加载策略基于ClassReloadingStrategy.Strategy.REDEFINITION时你也可以使用

ClassReloadingStrategy s = ClassReloadingStrategy.fromInstalledAgent();
s.reset(ClassFileLocator.Simple.of(classname, bytecode), clazz);

因为它将使用通过 ClassFileLocator 检索到的字节码作为基础。

我建议继续使用获取 ClassReloadingStrategy 的标准方式实现,正如您在其他问题中所做的那样,我无法识别您希望通过这种更复杂的反射操作获得什么。

关于java - 告诉bytebuddy到 "not care"关于通用信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48882178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com