简体   繁体   English

如何在 Byte-buddy 中将 package-info 与 class 关联?

[英]How to associate package-info with class in Byte-buddy?

I create package-info of package foo.bar and class foo.bar.BarCl in next code我在下一个代码中创建 package foo.bar和 class foo.bar.BarCl的包信息

public static void main(String[] args) throws ClassNotFoundException, IOException {
        DynamicType.Unloaded<?> make = new ByteBuddy().makePackage("foo.bar").make();
        DynamicType.Loaded<Object> load = new ByteBuddy()
                .subclass(Object.class)
                .name("foo.bar.BarCl")
                .make()
                .include(make)
                .load(Main2.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION);
        load.saveIn(new File("folder"));
        Class<?> loaded = load.getLoaded();
        System.out.println(loaded.getPackage());
    }

Class and package info correctly writing in folder: Class 和 package 信息正确写入文件夹:

package foo.bar;

interface $$package-info /* Real name is 'package-info' */ {
}


package foo.bar;

public class BarCl {
    public BarCl() {
    }
}

But in runtime after injecting this classes i get loaded.getPackage()==null How i can associate package-info with generated class?但是在运行时注入这些类后,我得到loaded.getPackage()==null我如何将包信息与生成的 class 关联?

PS In real task i need to generate package-info with JAXB annotation @XmlSchema , that specify xml namespace. PS 在实际任务中,我需要使用 JAXB 注释@XmlSchema生成包信息,指定 xml 命名空间。 Without it, classes have naming collisions没有它,类就会发生命名冲突

Packages are class loader respobsibility and not defined by a package-info class.包是 class 加载程序责任,而不是由package-info class 定义。 You can define them using the loader DSL .您可以使用加载程序 DSL定义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM