Skip to content

Commit eb85ff2

Browse files
committed
wrap-java add basic docs with java signature to @JavaMethods
Right now these are not super exciting but having the java signature next to the wrapped Swift signature is helpful in detecting subtle issues or understanding hwo types were mapped over Further down the line I'd like to figure out including the original javadoc, by fetching and resolving the javadoc jars etc...
1 parent dab4736 commit eb85ff2

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

Sources/SwiftJavaToolLib/JavaClassTranslator.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,20 @@ extension JavaClassTranslator {
695695
let swiftMethodName = javaMethod.getName().escapedSwiftName
696696
let swiftOptionalMethodName = "\(javaMethod.getName())Optional".escapedSwiftName
697697

698+
// --- Handle docs for the generated method.
699+
// Include the original Java signature
700+
let docsString =
701+
"""
702+
/**
703+
* Java method `\(javaMethod.getName())`.
704+
*
705+
* ### Java method signature
706+
* ```java
707+
* \(javaMethod.toGenericString())
708+
* ```
709+
*/
710+
"""
711+
698712
// Compute the parameters for '@...JavaMethod(...)'
699713
let methodAttribute: AttributeSyntax
700714
if implementedInSwift {
@@ -755,9 +769,9 @@ extension JavaClassTranslator {
755769
baseBody
756770
}
757771

758-
759772
return
760773
"""
774+
\(raw: docsString)
761775
\(methodAttribute)\(raw: accessModifier)\(raw: overrideOpt)func \(raw: swiftMethodName)\(raw: genericParameterClauseStr)(\(raw: parametersStr))\(raw: throwsStr)\(raw: resultTypeStr)\(raw: whereClause)
762776
763777
\(raw: accessModifier)\(raw: overrideOpt)func \(raw: swiftOptionalMethodName)\(raw: genericParameterClauseStr)(\(raw: parameters.map(\.clause.description).joined(separator: ", ")))\(raw: throwsStr) -> \(raw: resultOptional)\(raw: whereClause) {
@@ -767,6 +781,7 @@ extension JavaClassTranslator {
767781
} else {
768782
return
769783
"""
784+
\(raw: docsString)
770785
\(methodAttribute)\(raw: accessModifier)\(raw: overrideOpt)func \(raw: swiftMethodName)\(raw: genericParameterClauseStr)(\(raw: parametersStr))\(raw: throwsStr)\(raw: resultTypeStr)\(raw: whereClause)
771786
"""
772787
}

Tests/SwiftJavaToolLibTests/WrapJavaTests/BasicWrapJavaTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ final class BasicWrapJavaTests: XCTestCase {
4949
)
5050
}
5151

52+
func testWrapJava_docs_signature() async throws {
53+
let classpathURL = try await compileJava(
54+
"""
55+
package com.example;
56+
57+
class ExampleSimpleClass {
58+
public void example(String name, int age) { }
59+
}
60+
""")
61+
62+
try assertWrapJavaOutput(
63+
javaClassNames: [
64+
"com.example.ExampleSimpleClass"
65+
],
66+
classpath: [classpathURL],
67+
expectedChunks: [
68+
"""
69+
import CSwiftJavaJNI
70+
import SwiftJava
71+
""",
72+
"""
73+
/**
74+
* Java method `example`.
75+
*
76+
* ### Java method signature
77+
* ```java
78+
* public void com.example.ExampleSimpleClass.example(java.lang.String,int)
79+
* ```
80+
*/
81+
@JavaMethod
82+
open func example(_ arg0: String, _ arg1: Int32)
83+
"""
84+
]
85+
)
86+
}
87+
5288
func test_wrapJava_doNotDupeImportNestedClassesFromSuperclassAutomatically() async throws {
5389
let classpathURL = try await compileJava(
5490
"""

Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,6 @@ final class GenericsWrapJavaTests: XCTestCase {
257257
@JavaClass("com.example.ByteArray")
258258
open class ByteArray: JavaObject {
259259
""",
260-
// """
261-
// @JavaInterface("com.example.Store")
262-
// public struct Store<K: AnyJavaObject, V: AnyJavaObject, T: AnyJavaObject> {
263-
// """,
264260
"""
265261
@JavaClass("com.example.CompressingStore")
266262
open class CompressingStore: AbstractStore<ByteArray, [UInt8], [UInt8]> {
@@ -292,6 +288,8 @@ final class GenericsWrapJavaTests: XCTestCase {
292288
"""
293289
@JavaClass("com.example.Kappa")
294290
open class Kappa<T: AnyJavaObject>: JavaObject {
291+
""",
292+
"""
295293
@JavaMethod(typeErasedResult: "T!")
296294
open func get() -> T!
297295
}

0 commit comments

Comments
 (0)