对于一些特殊的用例,我有一个小实用程序可以使用动态类加载器从 jar 中加载 Java 类DynamicClassLoader
。这适用于 jar 中包含的 Java 类。从 jar 加载 Scala 类也没有问题。但是,实例化加载的 Scala 类会导致以下异常。看起来 Scala 类有私有默认构造函数?注意编译后的 Scala 类名以$
java.lang.IllegalAccessException: Class XXX can not access a member of class ScalaClassYYY$ with modifiers "private"
下面的代码片段说明了我想要实现的目标,并提供了更多背景信息。异常发生在注释行:
// deploy and register the new code
byte[] jarBytes = (byte[]) ((Object) message.getAttachment("jar"));
String registerClassName = message.getAttachment("register");
logger.debug("the register is '" + registerClassName + "'");
DynamicClassLoader loader = new DynamicClassLoader(jarBytes);
Class<?> registerClass = loader.lookUp(registerClassName);
// ===> this is where the java.lang.IllegalAccessException happens
IRegisterExecutor registerExecutor = (IRegisterExecutor) registerClass.newInstance();
registerExecutor.register();
任何想法如何解决?