@@ -98,7 +98,11 @@ struct SwiftJavaBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
9898 /// Determine the set of Swift files that will be emitted by the swift-java tool.
9999 // TODO: this is not precise and won't work with more advanced Java files, e.g. lambdas etc.
100100 let outputDirectoryGenerated = self . outputDirectory ( context: context, generated: true )
101- let outputSwiftFiles = classes. map { ( javaClassName, swiftName) in
101+ let outputSwiftFiles = classes. compactMap { ( javaClassName, swiftName) -> URL ? in
102+ guard shouldImportJavaClass ( javaClassName, config: config) else {
103+ return nil
104+ }
105+
102106 let swiftNestedName = swiftName. replacingOccurrences ( of: " . " , with: " + " )
103107 return outputDirectoryGenerated. appending ( path: " \( swiftNestedName) .swift " )
104108 }
@@ -168,7 +172,9 @@ struct SwiftJavaBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
168172 // Add all the core Java stdlib modules as --depends-on
169173 let javaStdlibModules = getExtractedJavaStdlibModules ( )
170174 log ( " Include Java standard library SwiftJava modules: \( javaStdlibModules) " )
171- arguments += javaStdlibModules. flatMap { [ " --depends-on " , $0] }
175+ let dependsOnArguments = javaStdlibModules. flatMap { [ " --depends-on " , $0] }
176+ // TODO: make the dependsOnArguments unique, and preserve the ordering. We cannot use external dependencies. Make an extension on Array to achieve this
177+ arguments += dependsOnArguments
172178
173179 if !outputSwiftFiles. isEmpty {
174180 arguments += [ configFile. path ( percentEncoded: false ) ]
@@ -195,6 +201,39 @@ struct SwiftJavaBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
195201 }
196202}
197203
204+ extension SwiftJavaBuildToolPlugin {
205+ private func shouldImportJavaClass( _ javaClassName: String , config: Configuration ) -> Bool {
206+ // If we have an inclusive filter, import only types from it
207+ if let includes = config. filterInclude, !includes. isEmpty {
208+ let anyIncludeFilterMatched = includes. contains { include in
209+ if javaClassName. starts ( with: include) {
210+ // TODO: lower to trace level
211+ log ( " Skip Java type: \( javaClassName) (does not match any include filter) " )
212+ return true
213+ }
214+
215+ return false
216+ }
217+
218+ guard anyIncludeFilterMatched else {
219+ log ( " Skip Java type: \( javaClassName) (does not match any include filter) " )
220+ return false
221+ }
222+ }
223+
224+ // If we have an exclude filter, check for it as well
225+ for exclude in config. filterExclude ?? [ ] {
226+ if javaClassName. starts ( with: exclude) {
227+ log ( " Skip Java type: \( javaClassName) (does match exclude filter: \( exclude) ) " )
228+ return false
229+ }
230+ }
231+
232+ // The class matches import filters, if any, and was not excluded.
233+ return true
234+ }
235+ }
236+
198237extension SwiftJavaBuildToolPlugin {
199238 func argumentsSwiftModule( sourceModule: Target ) -> [ String ] {
200239 return [
0 commit comments