我正在为android编写一个java代码,使用byte-buddy:1.10.17和byte-buddy-android:1.10.17来动态创建类。我想动态创建一个类,它将是另一个动态创建的类的子类。下面是我想要做的事情的示例代码
AndroidClassLoadingStrategy loadingStrategy = new AndroidClassLoadingStrategy.Wrapping(context.getCacheDir());
DynamicType.Builder builder = new ByteBuddy().subclass(Object.class).name("TestParentClass");
Class testParentClass = builder.make().load(Test.class.getClassLoader(), loadingStrategy).getLoaded();
builder = new ByteBuddy().subclass(testParentClass).name("TestChildClass");
Class testChildClass = builder.make().load(Test.class.getClassLoader(), loadingStrategy).getLoaded();但是我在创建子类时得到了Caused by: java.lang.ClassNotFoundException: Didn't find class "TestParentClass"。
我也检查过this question,但它根本不起作用。
发布于 2020-10-27 04:29:21
不要使用Test的类加载器。它将在类路径中查找类文件。动态类TestParentClass将没有。相反,可以从TestParentClass获取类加载器:
Class testChildClass = builder.make().load(testParentClass.getClassLoader(), loadingStrategy).getLoaded();https://stackoverflow.com/questions/64544355
复制相似问题