0

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

1 Answers1

1

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

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • 1
    Thanks, i use `load(Main2.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION.with(PackageDefinitionStrategy.Trivial.INSTANCE));` and this solve the problem! – vladimir vyatkin Jun 01 '20 at 04:19
  • @rafael-winterhalter, I am having a similar problem, but I am getting a `Illegal type name` when I hit `make` for a class with an as-yet-unknown package. I am not even getting to the load where I can use this technique. I get the same error if I call `.makePackage(newPkgName).make()` – egeorge Feb 24 '23 at 23:30
  • @rafael-winterhalter, nevermind. I was actually using a reserved word in my package name. Without that it works as expected. (thanks for an amazing tool, btw) – egeorge Feb 24 '23 at 23:50