javajaxbbyte-buddypackage-info

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

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:

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?

P.S. In real task i need to generate package-info with JAXB annotation @XmlSchema, that specify xml namespace. Without it, classes have naming collisions


Solution

  • Packages are class loader respobsibility and not defined by a package-info class. You can define them using the loader DSL.