@@ -58,52 +58,20 @@ extension JNISwift2JavaGenerator {
5858 // Each parent type goes into its own file
5959 // any nested types are printed inside the body as `static class`
6060 for ( _, ty ) in typesToExport . filter ( { _ , type in type . parent == nil } ) {
61- let filename = " \( ty . effectiveJavaSimpleName ) .java "
62- logger . debug ( " Printing contents: \( filename ) " )
63- printImportedNominal ( & printer , ty )
64-
65- if let outputFile = try printer . writeContents (
66- outputDirectory : javaOutputDirectory ,
67- javaPackagePath : javaPackagePath ,
68- filename : filename ,
69- ) {
70- exportedFileNames . append ( outputFile . path ( percentEncoded : false ) )
71- logger . info ( " [swift-java] Generated: \( ty . effectiveJavaSimpleName . bold ) .java (at \( outputFile ) ) " )
72- }
61+ let filename = printExtractedNominalFile ( & printer , ty )
62+ try writeGeneratedFile ( & printer , filename : filename , displayName : ty . effectiveJavaSimpleName , into : & exportedFileNames )
7363 }
7464
7565 // Skip the module-level .swift file when generating for a single type
7666 if config . singleType == nil {
77- let filename = " \( self . swiftModuleName ) .java "
78- logger . trace ( " Printing module class: \( filename ) " )
79- printModule ( & printer )
80-
81- if let outputFile = try printer . writeContents (
82- outputDirectory : javaOutputDirectory ,
83- javaPackagePath : javaPackagePath ,
84- filename : filename ,
85- ) {
86- exportedFileNames . append ( outputFile . path ( percentEncoded : false ) )
87- logger . info ( " [swift-java] Generated: \( self . swiftModuleName ) .java (at \( outputFile ) ) " )
88- }
67+ let filename = printExtractedModuleFile ( & printer )
68+ try writeGeneratedFile ( & printer , filename : filename , displayName : self . swiftModuleName , into : & exportedFileNames )
8969 }
9070
91- // One box class per protocol returned as `any P` / `some P` from at
92- // least one extracted function.
71+ // Print "`PBox`" Java class for any protocol returned as `any P` / `some P`.
9372 for protocolType in self . existentialProtocolBoxes {
94- let boxName = protocolType . swiftNominal . javaExistentialBoxName
95- let filename = " \( boxName ) .java "
96- logger . debug ( " Printing contents: \( filename ) " )
97- printExistentialBoxFile ( & printer , protocolType )
98-
99- if let outputFile = try printer . writeContents (
100- outputDirectory : javaOutputDirectory ,
101- javaPackagePath : javaPackagePath ,
102- filename : filename ,
103- ) {
104- exportedFileNames . append ( outputFile . path ( percentEncoded : false ) )
105- logger . info ( " [swift-java] Generated: \( boxName . bold ) .java (at \( outputFile ) ) " )
106- }
73+ let filename = printExistentialBoxFile ( & printer , protocolType )
74+ try writeGeneratedFile ( & printer , filename : filename , displayName : protocolType . swiftNominal . javaExistentialBoxName , into : & exportedFileNames )
10775 }
10876
10977 // Write java sources list file
@@ -118,15 +86,43 @@ extension JNISwift2JavaGenerator {
11886 }
11987 }
12088
121- private func printModule ( _ printer : inout JavaPrinter ) {
89+ /// Write the printer's accumulated contents to `<filename>`, append the
90+ /// resulting path to `exportedFileNames`, and emit the "Generated: ..."
91+ /// info log. Shared by every top-level `printXFile(...)` call site.
92+ private func writeGeneratedFile (
93+ _ printer : inout JavaPrinter ,
94+ filename : String ,
95+ displayName : String ,
96+ into exportedFileNames : inout OrderedSet < String >
97+ ) throws {
98+ if let outputFile = try printer . writeContents (
99+ outputDirectory : javaOutputDirectory ,
100+ javaPackagePath : javaPackagePath ,
101+ filename : filename ,
102+ ) {
103+ exportedFileNames . append ( outputFile . path ( percentEncoded : false ) )
104+ logger . info ( " [swift-java] Generated: \( displayName . bold ) .java (at \( outputFile ) ) " )
105+ }
106+ }
107+
108+ /// Reset the Java-identifier disambiguation factory for a new Java output
109+ /// file. `JavaIdentifierFactory` scopes overload disambiguation per Java
110+ /// class file, so every top-level `printXFile(&printer, ...)` method must
111+ /// call this once before it emits anything the translator will name.
112+ private func resetForNewOutputFile ( _ methods : [ ExtractedFunc ] ) {
113+ self . currentJavaIdentifiers = JavaIdentifierFactory ( methods )
114+ }
115+
116+ private func printExtractedModuleFile ( _ printer : inout JavaPrinter ) -> String {
117+ let filename = " \( self . swiftModuleName ) .java "
118+ logger . debug ( " Printing contents: \( filename ) " )
119+
120+ resetForNewOutputFile ( self . analysis . extractedGlobalFuncs + self . analysis . extractedGlobalVariables )
121+
122122 printHeader ( & printer )
123123 printPackage ( & printer )
124124 printImports ( & printer )
125125
126- self . currentJavaIdentifiers = JavaIdentifierFactory (
127- self . analysis . extractedGlobalFuncs + self . analysis . extractedGlobalVariables
128- )
129-
130126 printModuleClass ( & printer ) { printer in
131127 printer . print (
132128 """
@@ -170,23 +166,26 @@ extension JNISwift2JavaGenerator {
170166 printer . println ( )
171167 }
172168 }
169+ return filename
173170 }
174171
175- private func printImportedNominal ( _ printer : inout JavaPrinter , _ decl : ExtractedNominalType ) {
172+ private func printExtractedNominalFile ( _ printer : inout JavaPrinter , _ decl : ExtractedNominalType ) -> String {
173+ let filename = " \( decl . effectiveJavaSimpleName ) .java "
174+ logger . debug ( " Printing contents: \( filename ) " )
175+
176+ resetForNewOutputFile ( decl . initializers + decl . variables + decl . methods )
177+
176178 printHeader ( & printer )
177179 printPackage ( & printer )
178180 printImports ( & printer )
179181
180- self . currentJavaIdentifiers = JavaIdentifierFactory (
181- decl . initializers + decl . variables + decl . methods
182- )
183-
184182 switch decl . swiftNominal . kind {
185183 case . actor , . class , . enum , . struct :
186184 printConcreteType ( & printer , decl )
187185 case . protocol :
188186 printProtocol ( & printer , decl )
189187 }
188+ return filename
190189 }
191190
192191 private func printProtocol ( _ printer : inout JavaPrinter , _ decl : ExtractedNominalType ) {
@@ -219,16 +218,19 @@ extension JNISwift2JavaGenerator {
219218 /// Prints the full `.java` file for a protocol's existential box — the
220219 /// class used to represent a value returned as `any P` / `some P` from an
221220 /// extracted Swift function.
222- private func printExistentialBoxFile ( _ printer : inout JavaPrinter , _ decl : ExtractedNominalType ) {
223- assert ( decl . swiftNominal . kind == . protocol , " printExistentialBoxFile must only be called for a protocol, but was called with: \( decl ) " )
221+ private func printExistentialBoxFile ( _ printer : inout JavaPrinter , _ decl : ExtractedNominalType ) -> String {
222+ assert ( decl . swiftNominal . kind == . protocol , " Expected protocol, got \( decl . swiftNominal . kind ) : \( decl . qualifiedName ) " )
223+ let filename = " \( decl . swiftNominal . javaExistentialBoxName ) .java "
224+ logger . debug ( " Printing contents: \( filename ) " )
225+
226+ resetForNewOutputFile ( self . allProtocolRequirementMethods ( of : decl ) )
224227
225228 printHeader ( & printer )
226229 printPackage ( & printer )
227230 printImports ( & printer )
228231
229- self . currentJavaIdentifiers = JavaIdentifierFactory ( self . allProtocolRequirementMethods ( of : decl ) )
230-
231232 printExistentialBox ( & printer , decl )
233+ return filename
232234 }
233235
234236 /// Prints `public final class <P>Box implements JNISwiftInstance, <P> { ... }`.
0 commit comments