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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension JNISwift2JavaGenerator {
return // no need to write any empty files, yay
}

logger.info("[swift-java] Write empty [\(self.expectedOutputSwiftFileNames.count)] 'expected' files in: \(swiftOutputDirectory)/")
logger.info("Write empty [\(self.expectedOutputSwiftFileNames.count)] 'expected' files in: \(swiftOutputDirectory)/")

for expectedFileName in self.expectedOutputSwiftFileNames {
logger.info("Write SwiftPM-'expected' empty file: \(expectedFileName.bold)")
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftJavaTool/Commands/WrapJavaCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ extension SwiftJava.WrapJavaCommand {
let anyIncludeFilterMatched = includes.contains { include in
if javaClassName.starts(with: include) {
// TODO: lower to trace level
log.info("Skip Java type: \(javaClassName) (does not match any include filter)")
return true
}

log.info("Skip Java type: \(javaClassName) (does not match any include filter)")
return false
}

Expand All @@ -362,4 +362,5 @@ extension SwiftJava.WrapJavaCommand {
// The class matches import filters, if any, and was not excluded.
return true
}

}
2 changes: 1 addition & 1 deletion Sources/SwiftJavaTool/CommonOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension SwiftJava {
@Option(name: .long, help: "While scanning a classpath, inspect ONLY types included in these packages")
var filterInclude: [String] = []

@Option(name: .long, help: "While scanning a classpath, skip types which match the filter prefix")
@Option(name: .long, help: "While scanning a classpath, skip types which match the filter prefix. You can exclude specific methods by using the `com.example.MyClass#method` format.")
var filterExclude: [String] = []

@Option(help: "A path to a custom swift-java.config to use")
Expand Down
36 changes: 32 additions & 4 deletions Sources/SwiftJavaToolLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ struct JavaClassTranslator {
for method in methods {
guard let method else { continue }

guard shouldExtract(method: method) else {
guard shouldExtract(method: method, config: translator.config) else {
continue
}

Expand Down Expand Up @@ -257,7 +257,34 @@ extension JavaClassTranslator {

/// Determines whether a method should be extracted for translation.
/// Only look at public and protected methods here.
private func shouldExtract(method: Method) -> Bool {
private func shouldExtract(method: Method, config: Configuration) -> Bool {
// Check exclude filters, if they're applicable to methods:
for exclude in config.filterExclude ?? [] where exclude.contains("#") {
let split = exclude.split(separator: "#")
guard split.count == 2 else {
self.log.warning("Malformed method exclude filter, must have only one '#' marker: \(exclude)")
continue // cannot use this filter, malformed
}

let javaClassName = method.getDeclaringClass().getName()
let javaMemberName = method.getName()

let className = split.first!
let excludedName = split.dropFirst().first!

self.log.warning("Exclude filter: \(exclude) ||| \(javaClassName) / \(javaMemberName)")

if javaClassName.starts(with: className) {
if excludedName.hasSuffix("*"), javaMemberName.starts(with: excludedName.dropLast()) {
log.info("Skip Java member '\(javaClassName)#\(javaMemberName)', prefix exclude matched: \(exclude)")
return false
} else if javaMemberName == excludedName {
log.info("Skip Java member '\(javaClassName)#\(javaMemberName)', exact exclude matched: \(exclude)")
return false
}
}
}

switch self.translator.config.effectiveMinimumInputAccessLevelMode {
case .internal:
return method.isPublic || method.isProtected || method.isPackage
Expand Down Expand Up @@ -579,7 +606,8 @@ extension JavaClassTranslator {
package func renderConstructor(
_ javaConstructor: Constructor<some AnyJavaObject>
) throws -> DeclSyntax {
let parameters = try translateJavaParameters(javaConstructor.getParameters()) + ["environment: JNIEnvironment? = nil"]
let parameters: [FunctionParameterSyntax]
parameters = try translateJavaParameters(javaConstructor.getParameters()) + ["environment: JNIEnvironment? = nil"]
let parametersStr = parameters.map { $0.description }.joined(separator: ", ")
let throwsStr = javaConstructor.throwsCheckedException ? "throws" : ""
let accessModifier = javaConstructor.isPublic ? "public " : ""
Expand Down Expand Up @@ -987,7 +1015,7 @@ extension JavaClassTranslator {
continue
}

guard shouldExtract(method: overriddenMethod) else {
guard shouldExtract(method: overriddenMethod, config: translator.config) else {
continue
}

Expand Down
Loading