[External] : Re: JEP 476: Clarification on Package Export Behavior in Java Modules
Alex Buckley
alex.buckley at oracle.com
Fri Sep 13 19:44:18 UTC 2024
You're not importing the my.nice.project.pkg.{a,b} packages at all, so
the names SomeRecord and SomeInterface are unknown.
`import module ...` will only import the packages exported from the module.
If the my.nice.project module doesn't export anything, then `import
module my.nice.project` is not a substitute for on-demand/single-type
imports.
If the my.nice.project module exports the two packages to itself, then
`import module my.nice.project` from within the module should work as a
substitute for on-demand/single-type imports.
Alex
On 9/13/2024 12:28 PM, Thiago Henrique Hupner wrote:
> Source structure:
>
> src
> | module-info.java
> \---my
> \---nice
> \---project
> | Main.java
> \---pkg
> +---a
> | SomeInterface.java
> \---b
> SomeRecord.java
>
> I've tried both commands with and without the module import and they
> both fail:
>
> java --enable-preview src/my/nice/project/Main.java
>
>
> javac --release 23 --enable-preview -d ./mod ./src/module-info.java ./
> src/my/nice/project/Main.java ./src/my/nice/project/pkg/a/
> SomeInterface.java ./src/my/nice/project/pkg/b/SomeRecord.java
>
> However, exporting a package to the my.nice.project module and including
> the module import both commands work.
>
>
> .\src\my\nice\project\pkg\a\SomeInterface.java:7: error: cannot find symbol
> public SomeRecord someMethod();
> ^
> symbol: class SomeRecord
> location: interface SomeInterface
> .\src\my\nice\project\Main.java:7: error: cannot find symbol
> SomeInterface someInterface = new SomeInterface() {
> ^
> symbol: class SomeInterface
> location: class Main
> .\src\my\nice\project\Main.java:7: error: cannot find symbol
> SomeInterface someInterface = new SomeInterface() {
> ^
> symbol: class SomeInterface
> location: class Main
> .\src\my\nice\project\Main.java:9: error: cannot find symbol
> public SomeRecord someMethod() {
> ^
> symbol: class SomeRecord
> .\src\my\nice\project\Main.java:8: error: method does not override or
> implement a method from a supertype
> @Override
> ^
> .\src\my\nice\project\Main.java:10: error: cannot find symbol
> return new SomeRecord();
> ^
> symbol: class SomeRecord
> 6 errors
>
>
> Best regards,
>
> Thiago
>
> Em sex., 13 de set. de 2024 às 15:53, Alex Buckley
> <alex.buckley at oracle.com <mailto:alex.buckley at oracle.com>> escreveu:
>
> If removing the imports doesn't fix the errors, then I'm suspicious of
> your compiler setup.
>
> Please describe on this mailing list the commands you're using and the
> actual javac errors that you see.
>
> Alex
>
> On 9/13/2024 9:41 AM, Thiago Henrique Hupner wrote:
> > Removing the imports still doesn't fix the errors.
> >
> > I've create a Github repo with the reproducer: https://
> github.com/ <https://urldefense.com/v3/__https://github.com/__;!!
> ACWV5N9M2RV99hQ!I4FDC_5bZkrDbMH-KZS-5Acu-
> JIodeUAiW82iZl2Ozjm04tx6e3VXy1ug49pGDiAOGAKVnPCOoj1tF9KrA$>
> > Thihup/module-import-bug
> >
> > Best regards,
> >
> > Thiago
> >
> >
> > Em sex., 13 de set. de 2024 às 12:41, Alex Buckley
> > <alex.buckley at oracle.com <mailto:alex.buckley at oracle.com>
> <mailto:alex.buckley at oracle.com <mailto:alex.buckley at oracle.com>>>
> escreveu:
> >
> > I assume that the package my.nice.project is colocated with
> the other
> > two packages -- my.nice.project.pkg.{a,b} -- in the module
> > my.nice.project.
> >
> > If so, then all three packages should be able to access each
> others'
> > public types (SomeInterface, SomeRecord) without error.
> >
> > The fact that you're getting an error looks like a javac bug.
> >
> > You don't need to `import module my.nice.project` in the source
> > files of
> > that module. Try removing those imports and see if
> my.nice.project.Main
> > compiles (it should).
> >
> > Alex
> >
> > On 9/13/2024 5:00 AM, Thiago Henrique Hupner wrote:
> > > Dear Amber Team,
> > >
> > > I hope you're doing well. I’ve encountered a behavior
> related to JEP
> > > 476: Module Import Declarations and wanted to ask for
> clarification.
> > >
> > > I’m working with a module setup where two packages are defined
> > but not
> > > initially exported:
> > >
> > > |module my.nice.project { //exports my.nice.project.pkg.a to
> > > my.nice.project; //exports my.nice.project.pkg.b to
> > my.nice.project; } |
> > >
> > > Here’s a simplified version of the code structure:
> > >
> > >
> > > |package my.nice.project;
> > > |
> > > |
> > > |import module my.nice.project;|
> > >
> > > |
> > > |public class Main { public static void main(String[] args)
> > > { SomeInterface someInterface = new SomeInterface()
> { @Override
> > public
> > > SomeRecord someMethod() { return new SomeRecord(); } }; } }|
> > >
> > > |
> > > |
> > > |package my.nice.project.pkg.a; import module
> my.nice.project;|
> > > |
> > > |
> > > |public interface SomeInterface { public SomeRecord
> someMethod(); }|
> > >
> > > |package my.nice.project.pkg.b; public record SomeRecord()
> {} |
> > >
> > > When attempting to compile this code with the exports
> commented
> > out, I
> > > received several "cannot find symbol" errors. However, after
> > > uncommenting the exports:
> > >
> > >
> > > |exports my.nice.project.pkg.a to my.nice.project; exports
> > > my.nice.project.pkg.b to my.nice.project; |
> > >
> > > The code compiled and ran as expected.
> > >
> > > Given this behavior, I wanted to confirm if this
> restriction on
> > package
> > > visibility within a module is the intended design under
> the Amber
> > > project and the module system, or if there’s another
> approach or
> > best
> > > practice I might be overlooking in terms of package exports.
> > >
> > > Thank you for your time and insight.
> > >
> > > Best regards,
> > >
> > > Thiago
> > >
> >
>
More information about the amber-dev
mailing list