Skip to content

[JEP 511] Module Import Declarations#4910

Merged
jlerbsc merged 11 commits intojavaparser:masterfrom
johannescoetzee:johannes/module-imports
Dec 4, 2025
Merged

[JEP 511] Module Import Declarations#4910
jlerbsc merged 11 commits intojavaparser:masterfrom
johannescoetzee:johannes/module-imports

Conversation

@johannescoetzee
Copy link
Copy Markdown
Collaborator

I tried to re-open #4905, but since I force-pushed changes to restructure the commits as requested this option was no longer available.

Fixes #4848

This PR adds parser support for module import statements, as well as support for resolving types from modules.

Changes to ImportDeclaration/CompilationUnit

Constructors and an addImport method with an option to specify whether this is a module import or not (indicated by the value of the new isModule field in ImportDeclaration) were added. The old versions of these constructors and this method without the isModule parameter were kept to avoid breaking user code.

JarTypeSolver

Javassist does not provide much support for handling modules, so I ended up parsing the module attribute manually. This is a bit cumbersome, but does work and I hope the comments explain what is happening sufficiently well.

ClassLoaderTypeSolver

I couldn't figure out a way to do this without using Java 9+ types and methods, so I used a lot of reflection for the implementation to maintain compatibility with Java 8 (although this will always fail if JavaParser is run with Java 8). I also couldn't figure out a good way to get to a list of modules loaded by a classloader (the mechanisms I could find that looked promising were all JDK-internal or blocked for security reasons). The solution as it is presented here requires users to provide a list of module layers that the typesolver will use to find modules. This is done automatically for the ReflectionTypeSolver.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 2, 2025

Codecov Report

❌ Patch coverage is 64.86486% with 78 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.417%. Comparing base (2dc4812) to head (701d0dd).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...ver/resolution/typesolvers/CombinedTypeSolver.java 29.166% 14 Missing and 3 partials ⚠️
...r/resolution/typesolvers/JavaParserTypeSolver.java 64.516% 6 Missing and 5 partials ⚠️
.../resolution/typesolvers/ClassLoaderTypeSolver.java 47.368% 6 Missing and 4 partials ⚠️
...olsolver/resolution/typesolvers/JarTypeSolver.java 60.000% 4 Missing and 4 partials ⚠️
...vaparser/symbolsolver/utils/ModuleLayerHelper.java 75.000% 5 Missing and 2 partials ⚠️
...a/com/github/javaparser/resolution/TypeSolver.java 0.000% 5 Missing ⚠️
...a/com/github/javaparser/ast/ImportDeclaration.java 77.777% 4 Missing ⚠️
...rser/symbolsolver/utils/JavassistModuleHelper.java 89.285% 2 Missing and 1 partial ⚠️
...ava/com/github/javaparser/ast/CompilationUnit.java 60.000% 1 Missing and 1 partial ⚠️
...om/github/javaparser/ast/visitor/CloneVisitor.java 0.000% 2 Missing ⚠️
... and 8 more
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##              master     #4910   +/-   ##
===========================================
  Coverage     58.417%   58.417%           
  Complexity      2559      2559           
===========================================
  Files            689       689           
  Lines          39526     39526           
  Branches        7168      7168           
===========================================
  Hits           23090     23090           
  Misses         13499     13499           
  Partials        2937      2937           
Flag Coverage Δ
AlsoSlowTests 58.417% <64.864%> (ø)
javaparser-core 58.417% <64.864%> (ø)
javaparser-symbol-solver 58.417% <64.864%> (ø)
jdk-10 57.981% <63.963%> (ø)
jdk-11 57.982% <63.963%> (ø)
jdk-12 57.982% <63.963%> (+0.002%) ⬆️
jdk-13 57.979% <63.963%> (-0.003%) ⬇️
jdk-14 58.217% <63.963%> (ø)
jdk-15 58.217% <63.963%> (ø)
jdk-16 58.192% <63.963%> (ø)
jdk-17 58.344% <63.963%> (+0.002%) ⬆️
jdk-18 58.344% <63.963%> (ø)
jdk-8 57.828% <36.936%> (ø)
jdk-9 57.981% <63.963%> (ø)
macos-latest 58.409% <64.864%> (ø)
ubuntu-latest 58.404% <64.864%> (ø)
windows-latest 58.399% <64.864%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ava/com/github/javaparser/ParserConfiguration.java 87.417% <100.000%> (ø)
...ub/javaparser/ast/observer/ObservableProperty.java 87.027% <100.000%> (ø)
...r/language_level_validations/Java1_0Validator.java 91.935% <100.000%> (ø)
...or/language_level_validations/Java25Validator.java 100.000% <100.000%> (ø)
.../validator/postprocessors/Java25PostProcessor.java 100.000% <100.000%> (ø)
...github/javaparser/ast/visitor/HashCodeVisitor.java 61.246% <100.000%> (ø)
...vaparser/metamodel/ImportDeclarationMetaModel.java 100.000% <ø> (ø)
...thub/javaparser/metamodel/JavaParserMetaModel.java 99.708% <100.000%> (ø)
...github/javaparser/printer/ConcreteSyntaxModel.java 99.260% <100.000%> (ø)
...avaparser/printer/DefaultPrettyPrinterVisitor.java 92.916% <100.000%> (ø)
... and 18 more

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d7e06dd...701d0dd. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

printer.print("static ");
}
if (n.isModule()) {
printer.print("static ");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the PR finished? If so, as soon as you have fixed this small issue, I will validate this PR. Thank you for your contribution.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that was a copy-paste issue. I would've thought that the tests I added would catch this, but apparently this visitor isn't being tested correctly. Tomorrow morning I'll update the test to ensure this is actually covered and fix this typo, but after that this PR is finished if you're satisfied with the rest

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but after that this PR is finished if you're satisfied with the rest

The solution of creating a new method for resolving types does not satisfy me intellectually, but I have no other solution to propose that would be as effective.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the static/module issue. I saw this is in the deprecated PrettyPrintVisitor and it looks like this visitor either isn't being tested at all, or only in few specific testcases. Most testcases use the DefaultPrettyPrinter instead. If we want to keep the PrettyPrintVisitor, ideally we'd refactor it so that it just uses the DefaultPrettyPrinter methods, removing the need to maintain two largely identical implementations. I haven't looked into the configuration options enough to get an idea of how hard porting the configuring aspect will be, but the printing methods themselves should be simple. Regardless, I think refactoring the printer and fixing the testing situation is probably best handled separately.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be time to do some cleaning up and remove this part of the code. Could you create an issue to address this topic? However, I would prefer to postpone resolving this new issue until later, as I do not want to include it in the next version of JavaParser to avoid too much impact on users' code.

@johannescoetzee johannescoetzee force-pushed the johannes/module-imports branch from 9bf84ba to 2dc4812 Compare December 3, 2025 14:20
@jlerbsc jlerbsc merged commit ba8e966 into javaparser:master Dec 4, 2025
35 checks passed
@jlerbsc jlerbsc added this to the next release milestone Dec 4, 2025
@jlerbsc jlerbsc added the PR: Added A PR that introduces new behaviour (e.g. functionality, tests) label Dec 4, 2025
@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Dec 4, 2025

Thank you for this contribution. It is very readable and understandable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Added A PR that introduces new behaviour (e.g. functionality, tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JEP 511 : Module Import Declarations

2 participants