diff --git a/Sources/JavaStdlib/JavaIO/swift-java.config b/Sources/JavaStdlib/JavaIO/swift-java.config index 7b40b2da..571e0216 100644 --- a/Sources/JavaStdlib/JavaIO/swift-java.config +++ b/Sources/JavaStdlib/JavaIO/swift-java.config @@ -10,10 +10,10 @@ "java.lang.Readable" : "Readable", "java.io.Writer" : "Writer", "java.io.File" : "File", + "java.io.Closeable" : "Closeable", "java.nio.file.Path" : "Path", "java.io.FileDescriptor" : "FileDescriptor", "java.nio.charset.Charset" : "Charset", - "java.io.Closeable" : "Closeable", "java.io.Flushable" : "Flushable", "java.io.Flushable" : "ByteBuffer", "java.nio.file.WatchService" : "WatchService", diff --git a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift index 837d5e2f..455cb962 100644 --- a/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift +++ b/Sources/SwiftJavaTool/Commands/ConfigureCommand.swift @@ -104,11 +104,11 @@ extension SwiftJava.ConfigureCommand { log.logLevel = .init(rawValue: self.logLevel.rawValue)! log.info("Run: emit configuration...") - var (amendExistingConfig, configuration) = try getBaseConfigurationForWrite() + var (amendExistingConfig, config) = try getBaseConfigurationForWrite() if !self.commonOptions.filterInclude.isEmpty { log.debug("Generate Java->Swift type mappings. Active include filters: \(self.commonOptions.filterInclude)") - } else if let filters = configuration.filterInclude, !filters.isEmpty { + } else if let filters = config.filterInclude, !filters.isEmpty { // take the package filter from the configuration file self.commonOptions.filterInclude = filters } else { @@ -124,7 +124,7 @@ extension SwiftJava.ConfigureCommand { if amendExistingConfig { log.info("Amend existing swift-java.config file...") } - configuration.classpath = classpathEntries.joined(separator: ":") // TODO: is this correct? + config.classpath = classpathEntries.joined(separator: ":") // TODO: is this correct? // Import types from all the classpath entries; // Note that we use the package level filtering, so users have some control over what gets imported. @@ -139,7 +139,7 @@ extension SwiftJava.ConfigureCommand { if entry.hasSuffix(".jar") { let jarFile = try JarFile(entry, false, environment: environment) try addJavaToSwiftMappings( - to: &configuration, + to: &config, forJar: jarFile, environment: environment ) @@ -151,7 +151,7 @@ extension SwiftJava.ConfigureCommand { } // Encode the configuration. - let contents = try configuration.renderJSON() + let contents = try config.renderJSON() // Write the file. try writeContents( diff --git a/Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift b/Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift index 0af1e75b..0e70ad00 100644 --- a/Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift +++ b/Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift @@ -66,6 +66,7 @@ extension SwiftJava { extension SwiftJava.WrapJavaCommand { mutating func runSwiftJavaCommand(config: inout Configuration) async throws { + print("self.commonOptions.filterInclude = \(self.commonOptions.filterInclude)") configure(&config.filterInclude, append: self.commonOptions.filterInclude) configure(&config.filterExclude, append: self.commonOptions.filterExclude) @@ -114,6 +115,7 @@ extension SwiftJava.WrapJavaCommand { } extension SwiftJava.WrapJavaCommand { + mutating func generateWrappers( config: Configuration, dependentConfigs: [(String, Configuration)], @@ -148,21 +150,9 @@ extension SwiftJava.WrapJavaCommand { let classLoader = try! JavaClass(environment: environment) .getSystemClassLoader()! var javaClasses: [JavaClass] = [] - eachClass: for (javaClassName, _) in config.classes ?? [:] { - - // If we have an inclusive filter, import only types from it - for include in config.filterInclude ?? [] { - guard javaClassName.starts(with: include) else { - log.info("Skip Java type: \(javaClassName) (does not match filter)") - continue - } - } - // If we have an exclude filter, check for it as well - for exclude in config.filterExclude ?? [] { - if javaClassName.starts(with: exclude) { - log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))") - continue eachClass - } + for (javaClassName, _) in config.classes ?? [:] { + guard shouldImportJavaClass(javaClassName, config: config) else { + continue } log.info("Wrapping java type: \(javaClassName)") @@ -213,19 +203,8 @@ extension SwiftJava.WrapJavaCommand { return nil } - // If we have an inclusive filter, import only types from it - for include in config.filterInclude ?? [] { - guard javaClassName.starts(with: include) else { - log.info("Skip Java type: \(javaClassName) (does not match filter)") - return nil - } - } - // If we have an exclude filter, check for it as well - for exclude in config.filterExclude ?? [] { - if javaClassName.starts(with: exclude) { - log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))") - return nil - } + guard shouldImportJavaClass(javaClassName, config: config) else { + return nil } // If this class has been explicitly mentioned, we're done. @@ -285,4 +264,24 @@ extension SwiftJava.WrapJavaCommand { ) } } + + private func shouldImportJavaClass(_ javaClassName: String, config: Configuration) -> Bool { + // If we have an inclusive filter, import only types from it + for include in config.filterInclude ?? [] { + guard javaClassName.starts(with: include) else { + log.info("Skip Java type: \(javaClassName) (does not match include filter: \(include))") + return false + } + } + // If we have an exclude filter, check for it as well + for exclude in config.filterExclude ?? [] { + if javaClassName.starts(with: exclude) { + log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))") + return false + } + } + + // The class matches import filters, if any, and was not excluded. + return true + } } diff --git a/Sources/SwiftJavaTool/CommonOptions.swift b/Sources/SwiftJavaTool/CommonOptions.swift index ebdfdbea..c313202b 100644 --- a/Sources/SwiftJavaTool/CommonOptions.swift +++ b/Sources/SwiftJavaTool/CommonOptions.swift @@ -38,7 +38,11 @@ extension HasCommonOptions { func configure(_ setting: inout [T]?, append value: [T]?) { if let value { - setting?.append(contentsOf: value) + if setting == nil { + setting = value + } else { + setting?.append(contentsOf: value) + } } }