@@ -56,8 +56,6 @@ extension JNISwift2JavaGenerator {
5656 self . expectedOutputSwiftFiles. remove ( moduleFilename)
5757 }
5858
59- try self . writeJNICacheSource ( & printer)
60-
6159 for (_, ty) in self . analysis. importedTypes. sorted ( by: { ( lhs, rhs) in lhs. key < rhs. key } ) {
6260 let fileNameBase = " \( ty. swiftNominal. qualifiedName) +SwiftJava "
6361 let filename = " \( fileNameBase) .swift "
@@ -82,38 +80,13 @@ extension JNISwift2JavaGenerator {
8280 }
8381 }
8482
85- private func writeJNICacheSource( _ printer: inout CodePrinter ) throws {
86- printer. print ( " import JavaKit " )
87- printer. println ( )
88- printer. printBraceBlock ( " enum JNICaches " ) { printer in
89- let enumCases = self . analysis. importedTypes. values. filter { $0. swiftNominal. kind == . enum } . flatMap ( \. cases)
90- for enumCase in enumCases {
91- printer. print ( " static var \( JNICaching . cacheName ( for: enumCase) ) : _JNICache! " )
92- }
93- printer. println ( )
94- printer. printBraceBlock ( " static func cleanup() " ) { printer in
95- for enumCase in enumCases {
96- printer. print ( " JNICaches. \( JNICaching . cacheName ( for: enumCase) ) = nil " )
97- }
83+ private func printJNICache( _ printer: inout CodePrinter , _ type: ImportedNominalType ) {
84+ printer. printBraceBlock ( " enum \( JNICaching . cacheName ( for: type) ) " ) { printer in
85+ for enumCase in type. cases {
86+ guard let translatedCase = translatedEnumCase ( for: enumCase) else { continue }
87+ printer. print ( " static let \( JNICaching . cacheMemberName ( for: enumCase) ) = \( renderEnumCaseCacheInit ( translatedCase) ) " )
9888 }
9989 }
100-
101- printer. println ( )
102- printer. print ( #"@_cdecl("JNI_OnUnload")"# )
103- printer. printBraceBlock ( " func JNI_OnUnload(javaVM: UnsafeMutablePointer<JavaVM?>!, reserved: UnsafeMutableRawPointer!) " ) { printer in
104- printer. print ( " JNICaches.cleanup() " )
105- }
106-
107- let fileName = " \( self . swiftModuleName) +JNICaches.swift "
108-
109- if let outputFile = try printer. writeContents (
110- outputDirectory: self . swiftOutputDirectory,
111- javaPackagePath: nil ,
112- filename: fileName
113- ) {
114- print ( " [swift-java] Generated: \( fileName. bold) (at \( outputFile) ) " )
115- self . expectedOutputSwiftFiles. remove ( fileName)
116- }
11790 }
11891
11992 private func printGlobalSwiftThunkSources( _ printer: inout CodePrinter ) throws {
@@ -133,6 +106,9 @@ extension JNISwift2JavaGenerator {
133106 private func printNominalTypeThunks( _ printer: inout CodePrinter , _ type: ImportedNominalType ) throws {
134107 printHeader ( & printer)
135108
109+ printJNICache ( & printer, type)
110+ printer. println ( )
111+
136112 for initializer in type. initializers {
137113 printSwiftFunctionThunk ( & printer, initializer)
138114 printer. println ( )
@@ -192,30 +168,18 @@ extension JNISwift2JavaGenerator {
192168 printSwiftFunctionThunk ( & printer, enumCase. caseFunction)
193169 printer. println ( )
194170
195- // Print enum case native init
196- printEnumNativeInit ( & printer, translatedCase)
197-
198171 // Print getAsCase method
199172 if !translatedCase. translatedValues. isEmpty {
200173 printEnumGetAsCaseThunk ( & printer, translatedCase)
201174 }
202175 }
203176
204- private func printEnumNativeInit( _ printer: inout CodePrinter , _ enumCase: TranslatedEnumCase ) {
205- printCDecl (
206- & printer,
207- javaMethodName: " $nativeInit " ,
208- parentName: " \( enumCase. original. enumType. nominalTypeDecl. name) $ \( enumCase. name) $$JNI " ,
209- parameters: [ ] ,
210- resultType: . void
211- ) { printer in
212- // Setup caching
177+ private func renderEnumCaseCacheInit( _ enumCase: TranslatedEnumCase ) -> String {
213178 let nativeParametersClassName = " \( javaPackagePath) / \( enumCase. enumName) $ \( enumCase. name) $$NativeParameters "
214179 let methodSignature = MethodSignature ( resultType: . void, parameterTypes: enumCase. parameterConversions. map ( \. native. javaType) )
215180 let methods = #"[.init(name: "<init>", signature: " \#( methodSignature. mangledName) ")]"#
216181
217- printer. print ( #"JNICaches. \#( JNICaching . cacheName ( for: enumCase. original) ) = _JNICache(environment: environment, className: " \#( nativeParametersClassName) ", methods: \#( methods) )"# )
218- }
182+ return #"_JNIMethodIDCache(environment: try! JavaVirtualMachine.shared().environment(), className: " \#( nativeParametersClassName) ", methods: \#( methods) )"#
219183 }
220184
221185 private func printEnumGetAsCaseThunk(
@@ -237,9 +201,9 @@ extension JNISwift2JavaGenerator {
237201 guard case . \( enumCase. original. name) ( \( caseNamesWithLet. joined ( separator: " , " ) ) ) = \( selfPointer) .pointee else {
238202 fatalError( " Expected enum case ' \( enumCase. original. name) ', but was ' \\ ( \( selfPointer) .pointee)'! " )
239203 }
240- let cache$ = JNICaches. \( JNICaching . cacheName ( for: enumCase. original) ) !
204+ let cache$ = \( JNICaching . cacheName ( for: enumCase. original. enumType ) ) . \( JNICaching . cacheMemberName ( for : enumCase . original ) )
241205 let class$ = cache$.javaClass
242- let method$ = _JNICache .Method(name: " <init> " , signature: " \( methodSignature. mangledName) " )
206+ let method$ = _JNIMethodIDCache .Method(name: " <init> " , signature: " \( methodSignature. mangledName) " )
243207 let constructorID$ = cache$[method$]
244208 """
245209 )
0 commit comments