在Java 9中,自动模块可以在导出的包中读取命名模块的类吗?

时间:2017-09-18 15:01:11

标签: java weld java-9 java-module module-info

我试图了解Weld CDI容器如何与JPMS一起使用。所以我有以下配置。

我有自己的命名模块和焊接阴影作为自动模块。 在我的模块中我有

module my.module {
    requires weld.se.shaded;
    exports com.foo;
}

的NewClass

package com.foo;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class NewClass {

    @Inject
    private NewBean bean;

    public void doIt() {
        bean.doIt();
    }

}

NewBean类

package com.foo;

import javax.enterprise.context.Dependent;

@Dependent
public class NewBean {

    public void doIt() {
        System.out.println("I am doing it");
    }
}

这是结果

Sep 18, 2017 2:33:12 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.0.2 (2017-09-18 08:49)
Sep 18, 2017 2:33:12 PM org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy processAnnotatedDiscovery
INFO: WELD-ENV-000014: Falling back to Java Reflection for bean-discovery-mode="annotated" discovery. Add org.jboss:jandex to the classpath to speed-up startup.
Sep 18, 2017 2:33:12 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Exception in thread "main" java.lang.reflect.InaccessibleObjectException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:590)
    at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:996)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.checkForExceptions(AbstractExecutorServices.java:72)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:58)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:66)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.ConcurrentBeanDeployer.createClassBeans(ConcurrentBeanDeployer.java:65)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:256)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:422)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.environment.se.Weld.initialize(Weld.java:789)
    at my.module/com.foo.NewMain.main(NewMain.java:47)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private com.foo.NewBean com.foo.NewClass.bean accessible: module my.module does not "opens com.foo" to module weld.se.shaded
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.of(GetAccessibleCopyOfMember.java:38)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.run(GetAccessibleCopyOfMember.java:44)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.security.GetAccessibleCopyOfMember.run(GetAccessibleCopyOfMember.java:26)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at weld.se.shaded@3.0.2-SNAPSHOT/org.jboss.weld.injection.FieldInjectionPoint.<init>(FieldInjectionPoint.java:65)

如何解释此异常

  

引起:java.lang.reflect.InaccessibleObjectException:无法执行   make field private com.foo.NewBean com.foo.NewClass.bean accessible:   模块my.module没有&#34;打开com.foo&#34;模块weld.se.shaded

有一个表here enter image description here 从中我可以理解,自动模块可以读取命名(应用程序)模块。那为什么它没有?

1 个答案:

答案 0 :(得分:3)

您应该尝试替换

exports com.foo

module-info.java

opens com.foo 

模块声明中的opens指令声明打开包以允许包中的所有类型,并且所有成员,而不仅仅是公共类型及其公共成员,由支持私有的API反映出来访问或绕过或禁止默认Java语言访问控制检查的方法。