- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我遇到了
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/
我有一个无法修改的大型第 3 方代码库,但我需要在许多不同的地方进行微小但重要的更改。我希望使用基于 ByteBuddy 的代理,但我不知道如何使用。我需要替换的调用的形式为: SomeSystemC
我正在尝试在 bytebuddy 中创建一个抽象类的子类,并想用我自己的函数覆盖构造函数。我不能让它与 defineConstructor 一起工作。 父类(super class): public
如何将 byte-buddy 生成的类与“org.reflections”一起使用? 例子: Class dynamicType = new ByteBuddy() .
我正在尝试更改已加载类的方法的返回值。 从 ByteBuddy 的文档 (http://bytebuddy.net/#/tutorial) 看来,只要我不添加任何字段/方法,这似乎可以使用 Java
我正在尝试使用 ByteBuddy 附加到我的计算机上运行的正在运行的进程。我希望在附加到正在运行的程序时,我的代理将导致重新加载已加载的类并显示我的 Transformer 的打印语句。 相反,当我
我正在尝试使用 ByteBuddy 重新定义 2 个方法,如下所示: ClassLoader classLoader = ClassLoader.getSystemClassLoader(); Cla
如何使用 AgentBuilder 将子类与名称匹配? 我可以轻松匹配类型本身 new AgentBuilder.Default() .type(named("SomeClass"))
我正在尝试与字节伙伴一起模仿模拟创建。是否可以一次性拦截所有方法? 我尝试了以下方法: new ByteBuddy() .subclass(Object.class) .method(na
我正在尝试编写一个如下所示的通用方法: private static Class immutableVersionOfClass(Class clazz) { return new Byte
我有一个带有方法 m() 的类 A 和带有方法 n() 的类 B。在 A 中,我有一个 B 类型的字段 b。在 m() 中的某个时刻会调用 b.n()。我需要使用 ByteBuddy 更改 A 的代码
我有一堆在普通 JDK 上运行的 Web 服务,我需要拦截所有公共(public)方法才能执行某些操作。一些方法使用@WebParam 注释。使用 ByteBuddy 对 WebService 进行子
如果我有以下内容: public abstract class Parameterized { protected abstract String foo(); } 是否可以这样做: Dynami
语境 我正在使用 ByteBuddy 实现字节码转换,操作过程是一个多步骤过程。 因此,操作必须能够: 扩充原有方法 完全创建新方法 扩充通过 2 引入的方法。 对于 1. 我使用了 @OnMetho
我目前正在尝试制作一个记录器代理,我目前正在拦截 PrepareStatement 类。在 PrepareStatement 中,有多种我希望跟踪的方法,但我感觉我做错了。 通常我现在所做的是拦截我希
我已经使用 ByteBuddy 库有一段时间了,但我发现自己陷入了困境。当测试的类位于同一个文件中(作为静态内部类)时,我可以使用此方法,但现在我已将逻辑分离到一个单独的文件中,它不再起作用。 如果有
如何定义一个方法,然后用 ByteBuddy 装饰它(多次)?这是我的例子 Builder builder = new ByteBuddy().subclass(Object.class).name(
有没有办法使用 ByteBuddy 为没有空构造函数的类创建代理? 这个想法是为给定的具体类型创建一个代理,然后将所有方法重定向到处理程序。 此测试展示了为没有空构造函数的类创建代理的场景,它会抛出
我正在尝试使用 bytebuddy 为进程附加代理。我发现我们可以使用 ByteBuddyAgent.attach(file,"18467");为了这。但是当我尝试执行此操作时,发生了以下错误。 这是
我正在尝试开发一个需要使用注释的工具。一个重要的功能是定位带有注释的元素并更改其值,即 // from this @Annotation(value = "foo") class SomeClass
我正在使用 Byte Buddy 1.9.0 作为基于 Xtext 的编程语言的代码生成器,并且我正在努力为实例化“匿名”类的方法生成字节码。出于所有意图和目的,我试图让 Byte Buddy 创建一
我是一名优秀的程序员,十分优秀!