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

Can automatic module read classes of named module in exported package in Java 9?

我正在尝试了解 Weld CDI 容器如何与 JPMS 一起工作。所以我有以下配置。

我有我的命名模块和 weld-se-shaded 作为自动模块。 在我的模块中我有

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

新班级

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 class

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)

如何解释这个异常

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

有个table here 从中我可以理解自动模块可以读取命名(应用程序)模块。那为什么不呢?

您或许应该尝试更换

exports com.foo

在你的 module-info.java

opens com.foo 

模块声明中的 opens 指令声明一个包是开放的,以允许包中的所有类型及其所有成员,而不仅仅是 public 类型及其 public成员将被支持私有访问的 API 或绕过或抑制默认 Java 语言访问控制检查的方式反映出来。