所以原来的解决方案是使用 Play 的默认类加载器加载库类,将构造函数设置为可访问,然后用于newInstance()
实例化库类。一些代码,以防其他人遇到类似问题:
// Use the default application classloader to load the library class
Class artistClazz = Play.application().classloader().loadClass("com.echonest.api.v4.Artist");
// Get the package private constructor that we need
Constructor<Artist> constructor = artistClazz.getDeclaredConstructor(EchoNestAPI.class, Map.class);
// Make sure it's accessible to this class
constructor.setAccessible(true);
return constructor.newInstance(this, (Map) mq.getObject("artist"));
我不相信这是解决这个问题的最好或最干净的解决方案,但它对这个修改后的子类之外的代码的影响最小,所以我现在可能会坚持下去。