Skip to content

Commit 5f7ac58

Browse files
committed
Fix how we handle appending to empty config
Also cleanup handling filtering in wrap-java a bit
1 parent a9add7f commit 5f7ac58

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

Sources/JavaStdlib/JavaIO/swift-java.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"java.lang.Readable" : "Readable",
1111
"java.io.Writer" : "Writer",
1212
"java.io.File" : "File",
13+
"java.io.Closeable" : "Closeable",
1314
"java.nio.file.Path" : "Path",
1415
"java.io.FileDescriptor" : "FileDescriptor",
1516
"java.nio.charset.Charset" : "Charset",
16-
"java.io.Closeable" : "Closeable",
1717
"java.io.Flushable" : "Flushable",
1818
"java.io.Flushable" : "ByteBuffer",
1919
"java.nio.file.WatchService" : "WatchService",

Sources/SwiftJavaTool/Commands/ConfigureCommand.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ extension SwiftJava.ConfigureCommand {
104104
log.logLevel = .init(rawValue: self.logLevel.rawValue)!
105105

106106
log.info("Run: emit configuration...")
107-
var (amendExistingConfig, configuration) = try getBaseConfigurationForWrite()
107+
var (amendExistingConfig, config) = try getBaseConfigurationForWrite()
108108

109109
if !self.commonOptions.filterInclude.isEmpty {
110110
log.debug("Generate Java->Swift type mappings. Active include filters: \(self.commonOptions.filterInclude)")
111-
} else if let filters = configuration.filterInclude, !filters.isEmpty {
111+
} else if let filters = config.filterInclude, !filters.isEmpty {
112112
// take the package filter from the configuration file
113113
self.commonOptions.filterInclude = filters
114114
} else {
@@ -124,7 +124,7 @@ extension SwiftJava.ConfigureCommand {
124124
if amendExistingConfig {
125125
log.info("Amend existing swift-java.config file...")
126126
}
127-
configuration.classpath = classpathEntries.joined(separator: ":") // TODO: is this correct?
127+
config.classpath = classpathEntries.joined(separator: ":") // TODO: is this correct?
128128

129129
// Import types from all the classpath entries;
130130
// Note that we use the package level filtering, so users have some control over what gets imported.
@@ -139,7 +139,7 @@ extension SwiftJava.ConfigureCommand {
139139
if entry.hasSuffix(".jar") {
140140
let jarFile = try JarFile(entry, false, environment: environment)
141141
try addJavaToSwiftMappings(
142-
to: &configuration,
142+
to: &config,
143143
forJar: jarFile,
144144
environment: environment
145145
)
@@ -151,7 +151,7 @@ extension SwiftJava.ConfigureCommand {
151151
}
152152

153153
// Encode the configuration.
154-
let contents = try configuration.renderJSON()
154+
let contents = try config.renderJSON()
155155

156156
// Write the file.
157157
try writeContents(

Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extension SwiftJava {
6666
extension SwiftJava.WrapJavaCommand {
6767

6868
mutating func runSwiftJavaCommand(config: inout Configuration) async throws {
69+
print("self.commonOptions.filterInclude = \(self.commonOptions.filterInclude)")
6970
configure(&config.filterInclude, append: self.commonOptions.filterInclude)
7071
configure(&config.filterExclude, append: self.commonOptions.filterExclude)
7172

@@ -114,6 +115,7 @@ extension SwiftJava.WrapJavaCommand {
114115
}
115116

116117
extension SwiftJava.WrapJavaCommand {
118+
117119
mutating func generateWrappers(
118120
config: Configuration,
119121
dependentConfigs: [(String, Configuration)],
@@ -148,21 +150,9 @@ extension SwiftJava.WrapJavaCommand {
148150
let classLoader = try! JavaClass<ClassLoader>(environment: environment)
149151
.getSystemClassLoader()!
150152
var javaClasses: [JavaClass<JavaObject>] = []
151-
eachClass: for (javaClassName, _) in config.classes ?? [:] {
152-
153-
// If we have an inclusive filter, import only types from it
154-
for include in config.filterInclude ?? [] {
155-
guard javaClassName.starts(with: include) else {
156-
log.info("Skip Java type: \(javaClassName) (does not match filter)")
157-
continue
158-
}
159-
}
160-
// If we have an exclude filter, check for it as well
161-
for exclude in config.filterExclude ?? [] {
162-
if javaClassName.starts(with: exclude) {
163-
log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))")
164-
continue eachClass
165-
}
153+
for (javaClassName, _) in config.classes ?? [:] {
154+
guard shouldImportJavaClass(javaClassName, config: config) else {
155+
continue
166156
}
167157

168158
log.info("Wrapping java type: \(javaClassName)")
@@ -213,19 +203,8 @@ extension SwiftJava.WrapJavaCommand {
213203
return nil
214204
}
215205

216-
// If we have an inclusive filter, import only types from it
217-
for include in config.filterInclude ?? [] {
218-
guard javaClassName.starts(with: include) else {
219-
log.info("Skip Java type: \(javaClassName) (does not match filter)")
220-
return nil
221-
}
222-
}
223-
// If we have an exclude filter, check for it as well
224-
for exclude in config.filterExclude ?? [] {
225-
if javaClassName.starts(with: exclude) {
226-
log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))")
227-
return nil
228-
}
206+
guard shouldImportJavaClass(javaClassName, config: config) else {
207+
return nil
229208
}
230209

231210
// If this class has been explicitly mentioned, we're done.
@@ -285,4 +264,24 @@ extension SwiftJava.WrapJavaCommand {
285264
)
286265
}
287266
}
267+
268+
private func shouldImportJavaClass(_ javaClassName: String, config: Configuration) -> Bool {
269+
// If we have an inclusive filter, import only types from it
270+
for include in config.filterInclude ?? [] {
271+
guard javaClassName.starts(with: include) else {
272+
log.info("Skip Java type: \(javaClassName) (does not match include filter: \(include))")
273+
return false
274+
}
275+
}
276+
// If we have an exclude filter, check for it as well
277+
for exclude in config.filterExclude ?? [] {
278+
if javaClassName.starts(with: exclude) {
279+
log.info("Skip Java type: \(javaClassName) (does match exclude filter: \(exclude))")
280+
return false
281+
}
282+
}
283+
284+
// The class matches import filters, if any, and was not excluded.
285+
return true
286+
}
288287
}

Sources/SwiftJavaTool/CommonOptions.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ extension HasCommonOptions {
3838

3939
func configure<T>(_ setting: inout [T]?, append value: [T]?) {
4040
if let value {
41-
setting?.append(contentsOf: value)
41+
if setting == nil {
42+
setting = value
43+
} else {
44+
setting?.append(contentsOf: value)
45+
}
4246
}
4347
}
4448

0 commit comments

Comments
 (0)