Skip to content

Conversation

@MartinWitt
Copy link
Collaborator

@MartinWitt MartinWitt commented Jan 3, 2026

needed for java25

Copilot AI review requested due to automatic review settings January 3, 2026 23:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for Java module imports (e.g., import module java.base;) to the Spoon framework. Module imports are a Java 9+ feature that allows importing entire modules.

Key changes:

  • Added MODULE enum value to CtImportKind to represent module imports
  • Extended CtImportVisitor interface with visitModuleImport method to handle module import traversal
  • Implemented module import parsing in JDTImportBuilder to detect and process module imports from source code
  • Updated all visitor implementations and switch statements to handle the new MODULE import kind
  • Added module import support to the pretty printer to output module imports correctly

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/test/resources/imports/ModuleImport.java Test resource demonstrating module import usage (missing necessary type imports)
src/test/java/spoon/test/imports/ImportTest.java Added test case for module imports and fixed trailing whitespace
src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java Added test to verify module imports are correctly pretty-printed
src/main/java/spoon/support/reflect/declaration/CtImportImpl.java Updated to recognize MODULE import kind and migrated switch to expression syntax
src/main/java/spoon/support/compiler/jdt/JDTImportBuilder.java Added logic to parse and create module imports from JDT AST
src/main/java/spoon/reflect/visitor/TypeNameScope.java Added empty visitModuleImport implementation (modules don't affect type resolution)
src/main/java/spoon/reflect/visitor/ImportCleaner.java Ensures module imports are never removed during cleanup
src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java Added visitModuleImport to print module imports correctly
src/main/java/spoon/reflect/visitor/CtImportVisitor.java Extended interface with visitModuleImport method
src/main/java/spoon/reflect/visitor/CtAbstractImportVisitor.java Added empty default implementation of visitModuleImport
src/main/java/spoon/reflect/declaration/CtImportKind.java Added MODULE enum value for module imports

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

*/
package spoon.support.compiler.jdt;

import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

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

The import for StringUtils is added but not used anywhere in the file. This unused import should be removed.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +14
import module java.base;

public class ModuleImport {
public static void main(String[] args) {
String[] fruits = new String[] { "apple", "berry", "citrus" };
Map<String, String> m = Stream
.of(fruits)
.collect(Collectors.toMap(
s -> s.toUpperCase().substring(0,1),
Function.identity()));
m.forEach((k, v) ->
System.out.println(k + " " + v));
}
}
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

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

The test file is missing necessary import statements for Map, Stream, Collectors, and Function. These types are used in the code but not imported. This will cause compilation errors. The missing imports should be added after the module import statement.

Copilot uses AI. Check for mistakes.
@MartinWitt MartinWitt changed the title feat: add support for module imports review: feat: add support for module imports Jan 4, 2026
@MartinWitt MartinWitt requested a review from SirYwell January 4, 2026 00:14
Copy link
Collaborator

@SirYwell SirYwell left a comment

Choose a reason for hiding this comment

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

A few minor things

Comment on lines 267 to 270
if (oldImport.getImportKind() == CtImportKind.MODULE) {
//never remove module imports
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume this is a conservative solution, i.e., removing such an import would be legal/might make sense in some cases (e.g., if we're in the same module?).

In that case, I think the comment should reflect that a bit better, e.g., "be conservative: do not remove any module imports". This should help if anyone ever tries to do more here.

}

@ModelTest(value = {"src/test/resources/imports/ModuleImport.java"}, autoImport = true, complianceLevel = 25)
void moduleImportsArePrinted(@BySimpleName("ModuleImport") CtType<?> type, Launcher launcher) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I already forgot about the @BySimpleName annotation again, but it definitely makes the test cleaner. Can you add a // contract: comment though?

@MartinWitt MartinWitt requested a review from SirYwell January 4, 2026 12:28
Copy link
Collaborator

@SirYwell SirYwell left a comment

Choose a reason for hiding this comment

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

A few more things

@seintur
Copy link
Contributor

seintur commented Jan 5, 2026

Hi @MartinWitt

Great.

Just wondering: no case MODULE nor setModuleImports in ElementPrinterHelper#writeImports

public void writeImports(Collection<CtImport> imports) {
?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants