Skip to content

Commit 7f4c375

Browse files
authored
Merge pull request #9 from dmlloyd/imports
Allow imports to be added from nested creators
2 parents 75a2c2f + fb425c0 commit 7f4c375

4 files changed

Lines changed: 232 additions & 9 deletions

File tree

‎src/main/java/io/smallrye/jdeparser/creator/SourceFileCreator.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
* within a source file. The source file's package declaration is set
1414
* when the file is created via
1515
* {@link Sources#createSourceFile(String, String, Consumer)}.
16+
* <p>
17+
* Import methods ({@link #import_(Type)}, {@link #import_(Class)},
18+
* {@link #importStatic(Type, String)}, {@link #importModule(String)})
19+
* may be called at any time before the source file is written, including
20+
* from within nested creator callbacks (e.g., inside a
21+
* {@link ClassCreator} or {@link MethodCreator} body).
1622
*/
1723
public sealed interface SourceFileCreator permits SourceFileCreatorImpl {
1824

‎src/main/java/io/smallrye/jdeparser/impl/AbstractCreator.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.smallrye.jdeparser.impl;
22

3+
import io.smallrye.common.constraint.Assert;
34
import io.smallrye.jdeparser.SourceVersion;
45
import io.smallrye.jdeparser.Type;
56

@@ -59,17 +60,33 @@ protected SourceVersion version() {
5960
return version;
6061
}
6162

63+
/**
64+
* Verifies that this creator has not been finalized.
65+
* <p>
66+
* Unlike {@link #checkActive()}, this permits calls while in the
67+
* NESTED state. Use this for operations that are safe to perform
68+
* concurrently with a child callback (e.g., adding imports).
69+
*
70+
* @throws IllegalStateException if the creator is DONE
71+
*/
72+
protected void checkNotDone() {
73+
if (state == ST_DONE) {
74+
throw new IllegalStateException("Creator has already been completed");
75+
}
76+
}
77+
6278
/**
6379
* Verifies that this creator is in the ACTIVE state.
6480
*
6581
* @throws IllegalStateException if the creator is NESTED or DONE
6682
*/
6783
protected void checkActive() {
68-
if (state != ST_ACTIVE) {
69-
throw new IllegalStateException(
70-
state == ST_NESTED
71-
? "Cannot modify creator while a nested callback is active"
72-
: "Creator has already been completed");
84+
switch (state) {
85+
case ST_ACTIVE -> {
86+
}
87+
case ST_NESTED -> throw new IllegalStateException("Cannot modify creator while a nested callback is active");
88+
case ST_DONE -> throw new IllegalStateException("Creator has already been completed");
89+
default -> throw Assert.impossibleSwitchCase(state);
7390
}
7491
}
7592

‎src/main/java/io/smallrye/jdeparser/impl/SourceFileCreatorImpl.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected void registerUsedType(final Type type) {
122122
/** {@inheritDoc} */
123123
@Override
124124
public void import_(final Type type) {
125-
checkActive();
125+
checkNotDone();
126126
Assert.checkNotNullParam("type", type);
127127
if (type instanceof ReferenceType ref) {
128128
imports.add(ref.qualifiedName());
@@ -132,15 +132,15 @@ public void import_(final Type type) {
132132
/** {@inheritDoc} */
133133
@Override
134134
public void import_(final Class<?> clazz) {
135-
checkActive();
135+
checkNotDone();
136136
Assert.checkNotNullParam("clazz", clazz);
137137
imports.add(clazz.getCanonicalName());
138138
}
139139

140140
/** {@inheritDoc} */
141141
@Override
142142
public void importStatic(final Type type, final String member) {
143-
checkActive();
143+
checkNotDone();
144144
Assert.checkNotNullParam("type", type);
145145
Assert.checkNotNullParam("member", member);
146146
Assert.checkNotEmptyParam("member", member);
@@ -152,7 +152,7 @@ public void importStatic(final Type type, final String member) {
152152
/** {@inheritDoc} */
153153
@Override
154154
public void importModule(final String moduleName) {
155-
checkActive();
155+
checkNotDone();
156156
Assert.checkNotNullParam("moduleName", moduleName);
157157
Assert.checkNotEmptyParam("moduleName", moduleName);
158158
version().require(LanguageFeature.MODULE_IMPORTS);

‎src/test/java/io/smallrye/jdeparser/test/ImportTest.java‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,206 @@ void explicitJavaLangImportResolvesAmbiguityWithSamePackage() throws IOException
618618
"Shadowed same-package type must be fully qualified, got:\n" + source);
619619
}
620620

621+
// ── Imports from nested creators ─────────────────────────────────────
622+
623+
/**
624+
* Verifies that {@link io.smallrye.jdeparser.creator.SourceFileCreator#import_(Type)}
625+
* can be called from within a nested class creator callback and produces
626+
* a correct import statement with simple name resolution.
627+
*
628+
* @throws IOException if source generation fails
629+
*/
630+
@Test
631+
void importFromNestedClassCreator() throws IOException {
632+
final Type listType = Type.named("java.util.List");
633+
final Sources sources = createSources(SourceVersion.JAVA_17);
634+
sources.createSourceFile("com.example", "NestedImport", sf -> {
635+
sf.class_("NestedImport", cc -> {
636+
cc.public_();
637+
// import added from within the class creator callback
638+
sf.import_(listType);
639+
cc.field("items", fc -> {
640+
fc.private_();
641+
fc.type(listType);
642+
});
643+
});
644+
});
645+
sources.writeSources();
646+
final String source = getSource("com.example", "NestedImport");
647+
assertTrue(source.contains("import java.util.List;"),
648+
"Import added from nested class creator should produce import statement, got:\n" + source);
649+
assertTrue(source.contains("private List items;"),
650+
"Type imported from nested creator should use simple name, got:\n" + source);
651+
}
652+
653+
/**
654+
* Verifies that {@link io.smallrye.jdeparser.creator.SourceFileCreator#import_(Type)}
655+
* can be called from a deeply nested creator callback (inside a method
656+
* body within a class) and still produces a correct import.
657+
*
658+
* @throws IOException if source generation fails
659+
*/
660+
@Test
661+
void importFromDeeplyNestedCreator() throws IOException {
662+
final Type listType = Type.named("java.util.List");
663+
final Sources sources = createSources(SourceVersion.JAVA_17);
664+
sources.createSourceFile("com.example", "DeepNested", sf -> {
665+
sf.class_("DeepNested", cc -> {
666+
cc.public_();
667+
cc.method("getItems", mc -> {
668+
mc.public_();
669+
mc.returning(listType);
670+
mc.body(b -> {
671+
// import added from within the method body callback
672+
sf.import_(listType);
673+
b.return_(Expr.NULL);
674+
});
675+
});
676+
});
677+
});
678+
sources.writeSources();
679+
final String source = getSource("com.example", "DeepNested");
680+
assertTrue(source.contains("import java.util.List;"),
681+
"Import added from deeply nested creator should produce import statement, got:\n" + source);
682+
assertTrue(source.contains("public List getItems()"),
683+
"Type imported from deeply nested creator should use simple name, got:\n" + source);
684+
}
685+
686+
/**
687+
* Verifies that {@link io.smallrye.jdeparser.creator.SourceFileCreator#import_(Class)}
688+
* can be called from within a nested creator callback.
689+
*
690+
* @throws IOException if source generation fails
691+
*/
692+
@Test
693+
void importClassFromNestedCreator() throws IOException {
694+
final Type listType = Type.of(java.util.List.class);
695+
final Sources sources = createSources(SourceVersion.JAVA_17);
696+
sources.createSourceFile("com.example", "NestedClassImport", sf -> {
697+
sf.class_("NestedClassImport", cc -> {
698+
cc.public_();
699+
sf.import_(java.util.List.class);
700+
cc.field("items", fc -> {
701+
fc.private_();
702+
fc.type(listType);
703+
});
704+
});
705+
});
706+
sources.writeSources();
707+
final String source = getSource("com.example", "NestedClassImport");
708+
assertTrue(source.contains("import java.util.List;"),
709+
"Class import from nested creator should produce import statement, got:\n" + source);
710+
assertTrue(source.contains("private List items;"),
711+
"Type imported via Class from nested creator should use simple name, got:\n" + source);
712+
}
713+
714+
/**
715+
* Verifies that {@link io.smallrye.jdeparser.creator.SourceFileCreator#importStatic(Type, String)}
716+
* can be called from within a nested creator callback.
717+
*
718+
* @throws IOException if source generation fails
719+
*/
720+
@Test
721+
void staticImportFromNestedCreator() throws IOException {
722+
final Type collectorsType = Type.named("java.util.stream.Collectors");
723+
final Sources sources = createSources(SourceVersion.JAVA_17);
724+
sources.createSourceFile("com.example", "NestedStaticImport", sf -> {
725+
sf.class_("NestedStaticImport", cc -> {
726+
cc.public_();
727+
sf.importStatic(collectorsType, "toList");
728+
cc.field("dummy", fc -> {
729+
fc.private_();
730+
fc.type(Type.INT);
731+
});
732+
});
733+
});
734+
sources.writeSources();
735+
final String source = getSource("com.example", "NestedStaticImport");
736+
assertTrue(source.contains("import static java.util.stream.Collectors.toList;"),
737+
"Static import from nested creator should produce import statement, got:\n" + source);
738+
}
739+
740+
/**
741+
* Verifies that {@link io.smallrye.jdeparser.creator.SourceFileCreator#importModule(String)}
742+
* can be called from within a nested creator callback.
743+
*
744+
* @throws IOException if source generation fails
745+
*/
746+
@Test
747+
void moduleImportFromNestedCreator() throws IOException {
748+
final Sources sources = createSources(SourceVersion.JAVA_25);
749+
sources.createSourceFile("com.example", "NestedModuleImport", sf -> {
750+
sf.class_("NestedModuleImport", cc -> {
751+
cc.public_();
752+
sf.importModule("java.base");
753+
cc.field("dummy", fc -> {
754+
fc.private_();
755+
fc.type(Type.INT);
756+
});
757+
});
758+
});
759+
sources.writeSources();
760+
final String source = getSource("com.example", "NestedModuleImport");
761+
assertTrue(source.contains("import module java.base;"),
762+
"Module import from nested creator should produce import statement, got:\n" + source);
763+
}
764+
765+
/**
766+
* Verifies that imports added both before and during a nested creator
767+
* callback are all present and resolve correctly in the output.
768+
*
769+
* @throws IOException if source generation fails
770+
*/
771+
@Test
772+
void importsFromMixedTopLevelAndNested() throws IOException {
773+
final Type listType = Type.named("java.util.List");
774+
final Type mapType = Type.named("java.util.Map");
775+
final Type setType = Type.named("java.util.Set");
776+
final Sources sources = createSources(SourceVersion.JAVA_17);
777+
sources.createSourceFile("com.example", "MixedNesting", sf -> {
778+
// import added before nesting
779+
sf.import_(listType);
780+
sf.class_("MixedNesting", cc -> {
781+
cc.public_();
782+
// import added during nesting
783+
sf.import_(mapType);
784+
cc.field("list", fc -> {
785+
fc.private_();
786+
fc.type(listType);
787+
});
788+
cc.field("map", fc -> {
789+
fc.private_();
790+
fc.type(mapType);
791+
});
792+
cc.method("process", mc -> {
793+
mc.public_();
794+
mc.body(b -> {
795+
// import added from deeply nested callback
796+
sf.import_(setType);
797+
});
798+
});
799+
cc.field("set", fc -> {
800+
fc.private_();
801+
fc.type(setType);
802+
});
803+
});
804+
});
805+
sources.writeSources();
806+
final String source = getSource("com.example", "MixedNesting");
807+
assertTrue(source.contains("import java.util.List;"),
808+
"Top-level import should be present, got:\n" + source);
809+
assertTrue(source.contains("import java.util.Map;"),
810+
"Import from nested class creator should be present, got:\n" + source);
811+
assertTrue(source.contains("import java.util.Set;"),
812+
"Import from deeply nested method body should be present, got:\n" + source);
813+
assertTrue(source.contains("private List list;"),
814+
"List should use simple name, got:\n" + source);
815+
assertTrue(source.contains("private Map map;"),
816+
"Map should use simple name, got:\n" + source);
817+
assertTrue(source.contains("private Set set;"),
818+
"Set should use simple name, got:\n" + source);
819+
}
820+
621821
// ── Mixed scenarios ─────────────────────────────────────────────────
622822

623823
/**

0 commit comments

Comments
 (0)