@@ -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