[英]ByteBuddy how to create a subclass of another ByteBuddy created Class in Android?
我正在使用byte-buddy:1.10.17
和byte-buddy-android:1.10.17
為 android 編寫 java 代碼來動態創建類。 我想動態創建一個類,它將是另一個動態創建的類的子類。 這是我想要做的示例代碼
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"
在創建子類時Caused by: java.lang.ClassNotFoundException: Didn't find class "TestParentClass"
。
我也檢查了這個問題,但它根本不起作用。
不要使用 Test 的類加載器。 這將在您的類路徑中尋找一個類文件。 動態類 TestParentClass 將沒有。 相反,從 TestParentClass 獲取類加載器:
Class testChildClass = builder.make().load(testParentClass.getClassLoader(), loadingStrategy).getLoaded();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.