Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/JavaStdlib/JavaIO/swift-java.config
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftJavaTool/Commands/ConfigureCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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
)
Expand All @@ -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(
Expand Down
55 changes: 27 additions & 28 deletions Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -114,6 +115,7 @@ extension SwiftJava.WrapJavaCommand {
}

extension SwiftJava.WrapJavaCommand {

mutating func generateWrappers(
config: Configuration,
dependentConfigs: [(String, Configuration)],
Expand Down Expand Up @@ -148,21 +150,9 @@ extension SwiftJava.WrapJavaCommand {
let classLoader = try! JavaClass<ClassLoader>(environment: environment)
.getSystemClassLoader()!
var javaClasses: [JavaClass<JavaObject>] = []
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)")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
6 changes: 5 additions & 1 deletion Sources/SwiftJavaTool/CommonOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ extension HasCommonOptions {

func configure<T>(_ setting: inout [T]?, append value: [T]?) {
if let value {
setting?.append(contentsOf: value)
if setting == nil {
setting = value
} else {
setting?.append(contentsOf: value)
}
}
}

Expand Down