@@ -53,20 +53,18 @@ extension Swift2JavaTranslator {
5353 }
5454
5555 public func writeSwiftThunkSources( outputDirectory: String , printer: inout CodePrinter ) throws {
56- // ==== Globals
57- for decl in self . importedGlobalFuncs {
58- printSwiftThunkSources ( & printer, decl: decl)
59- }
60-
6156 let moduleFilenameBase = " \( self . swiftModuleName) Module+SwiftJava "
6257 let moduleFilename = " \( moduleFilenameBase) .swift "
63- log. info ( " Printing contents: \( moduleFilename) " )
6458 do {
59+ log. info ( " Printing contents: \( moduleFilename) " )
60+
61+ try printGlobalSwiftThunkSources ( & printer)
62+
6563 if let outputFile = try printer. writeContents (
6664 outputDirectory: outputDirectory,
6765 javaPackagePath: nil ,
6866 filename: moduleFilename) {
69- print ( " [swift-java] Generated: \( moduleFilenameBase. bold) .java (at \( outputFile) " )
67+ print ( " [swift-java] Generated: \( moduleFilenameBase. bold) .swift (at \( outputFile) " )
7068 }
7169 } catch {
7270 log. warning ( " Failed to write to Swift thunks: \( moduleFilename) " )
@@ -93,6 +91,15 @@ extension Swift2JavaTranslator {
9391 }
9492 }
9593
94+ public func printGlobalSwiftThunkSources( _ printer: inout CodePrinter ) throws {
95+ let stt = SwiftThunkTranslator ( self )
96+
97+ for thunk in stt. renderGlobalThunks ( ) {
98+ printer. print ( thunk)
99+ printer. println ( )
100+ }
101+ }
102+
96103 public func printSwiftThunkSources( _ printer: inout CodePrinter , decl: ImportedFunc ) {
97104 let stt = SwiftThunkTranslator ( self )
98105
@@ -145,9 +152,6 @@ extension Swift2JavaTranslator {
145152 printImports ( & printer)
146153
147154 printModuleClass ( & printer) { printer in
148-
149- printStaticLibraryLoad ( & printer)
150-
151155 // TODO: print all "static" methods
152156 for decl in importedGlobalFuncs {
153157 printFunctionDowncallMethods ( & printer, decl)
@@ -161,9 +165,6 @@ extension Swift2JavaTranslator {
161165 printImports ( & printer)
162166
163167 printClass ( & printer, decl) { printer in
164- // Ensure we have loaded the library where the Swift type was declared before we attempt to resolve types in Swift
165- printStaticLibraryLoad ( & printer)
166-
167168 // Prepare type metadata, we're going to need these when invoking e.g. initializers so cache them in a static.
168169 // We call into source swift-java source generated accessors which give us the type of the Swift object:
169170 // TODO: seems we no longer need the mangled name per se, so avoiding such constant and downcall
@@ -308,6 +309,10 @@ extension Swift2JavaTranslator {
308309 """
309310 static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup();
310311 private static SymbolLookup getSymbolLookup() {
312+ // Ensure Swift and our Lib are loaded during static initialization of the class.
313+ System.loadLibrary( " swiftCore " );
314+ System.loadLibrary(LIB_NAME);
315+
311316 if (PlatformUtils.isMacOS()) {
312317 return SymbolLookup.libraryLookup(System.mapLibraryName(LIB_NAME), LIBRARY_ARENA)
313318 .or(SymbolLookup.loaderLookup())
@@ -339,6 +344,11 @@ extension Swift2JavaTranslator {
339344 private \( typeName) () {
340345 // Should not be called directly
341346 }
347+
348+ // Static enum to force initialization
349+ private static enum Initializer {
350+ FORCE; // Refer to this to force outer Class initialization (and static{} blocks to trigger)
351+ }
342352 """
343353 )
344354 }
@@ -508,17 +518,6 @@ extension Swift2JavaTranslator {
508518 )
509519 }
510520
511- public func printStaticLibraryLoad( _ printer: inout CodePrinter ) {
512- printer. print (
513- """
514- static {
515- System.loadLibrary( " swiftCore " );
516- System.loadLibrary(LIB_NAME);
517- }
518- """
519- )
520- }
521-
522521 public func printFunctionDowncallMethods( _ printer: inout CodePrinter , _ decl: ImportedFunc ) {
523522 printer. printSeparator ( decl. identifier)
524523
0 commit comments