diff --git a/Sources/JavaStdlib/JavaLangReflect/JavaClass+Reflection.swift b/Sources/JavaStdlib/JavaLangReflect/JavaClass+Reflection.swift index 49e93ee3..11061f85 100644 --- a/Sources/JavaStdlib/JavaLangReflect/JavaClass+Reflection.swift +++ b/Sources/JavaStdlib/JavaLangReflect/JavaClass+Reflection.swift @@ -49,4 +49,8 @@ extension JavaClass { @JavaMethod public func getAnnotations() -> [Annotation?] + + public func `as`(_: Type.Type) -> Type { + Type(javaHolder: javaHolder) + } } diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/AccessibleObject.swift b/Sources/JavaStdlib/JavaLangReflect/generated/AccessibleObject.swift index 7527bc1e..5b54b1df 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/AccessibleObject.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/AccessibleObject.swift @@ -28,7 +28,7 @@ open class AccessibleObject: JavaObject { /// ```java /// public T java.lang.reflect.AccessibleObject.getAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open func getAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getAnnotationsByType`. @@ -55,7 +55,7 @@ open class AccessibleObject: JavaObject { /// ```java /// public T java.lang.reflect.AccessibleObject.getDeclaredAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open func getDeclaredAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getDeclaredAnnotationsByType`. diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/AnnotatedType.swift b/Sources/JavaStdlib/JavaLangReflect/generated/AnnotatedType.swift index 95407466..13ce2d90 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/AnnotatedType.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/AnnotatedType.swift @@ -4,30 +4,84 @@ import SwiftJavaJNICore @JavaInterface("java.lang.reflect.AnnotatedType") public struct AnnotatedType { + /// Java method `getAnnotatedOwnerType`. + /// + /// ### Java method signature + /// ```java + /// public default java.lang.reflect.AnnotatedType java.lang.reflect.AnnotatedType.getAnnotatedOwnerType() + /// ``` @JavaMethod public func getAnnotatedOwnerType() -> AnnotatedType! - @JavaMethod - public func getAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public abstract T java.lang.reflect.AnnotatedType.getAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedType.getAnnotations() + /// ``` @JavaMethod public func getAnnotations() -> [Annotation?] + /// Java method `getDeclaredAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedType.getDeclaredAnnotations() + /// ``` @JavaMethod public func getDeclaredAnnotations() -> [Annotation?] + /// Java method `getType`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.reflect.Type java.lang.reflect.AnnotatedType.getType() + /// ``` @JavaMethod public func getType() -> Type! + /// Java method `isAnnotationPresent`. + /// + /// ### Java method signature + /// ```java + /// public default boolean java.lang.reflect.AnnotatedElement.isAnnotationPresent(java.lang.Class) + /// ``` @JavaMethod public func isAnnotationPresent(_ arg0: JavaClass?) -> Bool + /// Java method `getAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getAnnotationsByType(_ arg0: JavaClass?) -> [T?] - @JavaMethod - public func getDeclaredAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getDeclaredAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public default T java.lang.reflect.AnnotatedElement.getDeclaredAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getDeclaredAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getDeclaredAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [T?] } diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/Constructor.swift b/Sources/JavaStdlib/JavaLangReflect/generated/Constructor.swift index dbc61c89..501309e4 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/Constructor.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/Constructor.swift @@ -118,7 +118,7 @@ open class Constructor: Executable { /// ```java /// public T java.lang.reflect.Constructor.getAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open override func getAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getDeclaredAnnotations`. diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/Executable.swift b/Sources/JavaStdlib/JavaLangReflect/generated/Executable.swift index ddbcd7f7..fa7ee379 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/Executable.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/Executable.swift @@ -73,7 +73,7 @@ open class Executable: AccessibleObject { /// ```java /// public T java.lang.reflect.Executable.getAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open override func getAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getAnnotationsByType`. diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/Field.swift b/Sources/JavaStdlib/JavaLangReflect/generated/Field.swift index be96bea6..75739f7b 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/Field.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/Field.swift @@ -172,7 +172,7 @@ open class Field: AccessibleObject { /// ```java /// public T java.lang.reflect.Field.getAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open override func getAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getAnnotationsByType`. diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/GenericDeclaration.swift b/Sources/JavaStdlib/JavaLangReflect/generated/GenericDeclaration.swift index 31190474..9290f29b 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/GenericDeclaration.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/GenericDeclaration.swift @@ -4,27 +4,75 @@ import SwiftJavaJNICore @JavaInterface("java.lang.reflect.GenericDeclaration") public struct GenericDeclaration { + /// Java method `getTypeParameters`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.reflect.TypeVariable[] java.lang.reflect.GenericDeclaration.getTypeParameters() + /// ``` @JavaMethod public func getTypeParameters() -> [TypeVariable?] + /// Java method `isAnnotationPresent`. + /// + /// ### Java method signature + /// ```java + /// public default boolean java.lang.reflect.AnnotatedElement.isAnnotationPresent(java.lang.Class) + /// ``` @JavaMethod public func isAnnotationPresent(_ arg0: JavaClass?) -> Bool - @JavaMethod - public func getAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public abstract T java.lang.reflect.AnnotatedElement.getAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getAnnotationsByType(_ arg0: JavaClass?) -> [T?] + /// Java method `getAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedElement.getAnnotations() + /// ``` @JavaMethod public func getAnnotations() -> [Annotation?] - @JavaMethod - public func getDeclaredAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getDeclaredAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public default T java.lang.reflect.AnnotatedElement.getDeclaredAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getDeclaredAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getDeclaredAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [T?] + /// Java method `getDeclaredAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedElement.getDeclaredAnnotations() + /// ``` @JavaMethod public func getDeclaredAnnotations() -> [Annotation?] } diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/Method.swift b/Sources/JavaStdlib/JavaLangReflect/generated/Method.swift index d81e112c..5881c1aa 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/Method.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/Method.swift @@ -127,7 +127,7 @@ open class Method: Executable { /// ```java /// public T java.lang.reflect.Method.getAnnotation(java.lang.Class) /// ``` - @JavaMethod(typeErasedResult: "T!") + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) open override func getAnnotation(_ arg0: JavaClass?) -> T! /// Java method `getDeclaredAnnotations`. diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/Parameter.swift b/Sources/JavaStdlib/JavaLangReflect/generated/Parameter.swift index cde76ce4..40abae3b 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/Parameter.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/Parameter.swift @@ -1,64 +1,177 @@ // Auto-generated by Java-to-Swift wrapper generator. -import JavaUtil import SwiftJava import SwiftJavaJNICore @JavaClass("java.lang.reflect.Parameter") open class Parameter: JavaObject { + /// Java method `getName`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.String java.lang.reflect.Parameter.getName() + /// ``` @JavaMethod open func getName() -> String + /// Java method `equals`. + /// + /// ### Java method signature + /// ```java + /// public boolean java.lang.reflect.Parameter.equals(java.lang.Object) + /// ``` @JavaMethod open override func equals(_ arg0: JavaObject?) -> Bool + /// Java method `toString`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.String java.lang.reflect.Parameter.toString() + /// ``` @JavaMethod open override func toString() -> String + /// Java method `hashCode`. + /// + /// ### Java method signature + /// ```java + /// public int java.lang.reflect.Parameter.hashCode() + /// ``` @JavaMethod open override func hashCode() -> Int32 + /// Java method `getModifiers`. + /// + /// ### Java method signature + /// ```java + /// public int java.lang.reflect.Parameter.getModifiers() + /// ``` @JavaMethod open func getModifiers() -> Int32 + /// Java method `isSynthetic`. + /// + /// ### Java method signature + /// ```java + /// public boolean java.lang.reflect.Parameter.isSynthetic() + /// ``` @JavaMethod open func isSynthetic() -> Bool - @JavaMethod - open func getAnnotation(_ arg0: JavaClass?) -> Annotation! - - @JavaMethod - open func getAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] - + /// Java method `getAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public T java.lang.reflect.Parameter.getAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + open func getAnnotation(_ arg0: JavaClass?) -> T! + + /// Java method `getAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public T[] java.lang.reflect.Parameter.getAnnotationsByType(java.lang.Class) + /// ``` + @JavaMethod + open func getAnnotationsByType(_ arg0: JavaClass?) -> [T?] + + /// Java method `getAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.annotation.Annotation[] java.lang.reflect.Parameter.getAnnotations() + /// ``` @JavaMethod open func getAnnotations() -> [Annotation?] - @JavaMethod - open func getDeclaredAnnotation(_ arg0: JavaClass?) -> Annotation! - - @JavaMethod - open func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] - + /// Java method `getDeclaredAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public T java.lang.reflect.Parameter.getDeclaredAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + open func getDeclaredAnnotation(_ arg0: JavaClass?) -> T! + + /// Java method `getDeclaredAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public T[] java.lang.reflect.Parameter.getDeclaredAnnotationsByType(java.lang.Class) + /// ``` + @JavaMethod + open func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [T?] + + /// Java method `getDeclaredAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.annotation.Annotation[] java.lang.reflect.Parameter.getDeclaredAnnotations() + /// ``` @JavaMethod open func getDeclaredAnnotations() -> [Annotation?] + /// Java method `getType`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.Class java.lang.reflect.Parameter.getType() + /// ``` @JavaMethod open func getType() -> JavaClass! + /// Java method `getAnnotatedType`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.reflect.AnnotatedType java.lang.reflect.Parameter.getAnnotatedType() + /// ``` @JavaMethod open func getAnnotatedType() -> AnnotatedType! + /// Java method `getParameterizedType`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.reflect.Type java.lang.reflect.Parameter.getParameterizedType() + /// ``` @JavaMethod open func getParameterizedType() -> Type! + /// Java method `isVarArgs`. + /// + /// ### Java method signature + /// ```java + /// public boolean java.lang.reflect.Parameter.isVarArgs() + /// ``` @JavaMethod open func isVarArgs() -> Bool + /// Java method `getDeclaringExecutable`. + /// + /// ### Java method signature + /// ```java + /// public java.lang.reflect.Executable java.lang.reflect.Parameter.getDeclaringExecutable() + /// ``` @JavaMethod - open func isNamePresent() -> Bool + open func getDeclaringExecutable() -> Executable! + /// Java method `isNamePresent`. + /// + /// ### Java method signature + /// ```java + /// public boolean java.lang.reflect.Parameter.isNamePresent() + /// ``` @JavaMethod - open func getDeclaringExecutable() -> Executable! + open func isNamePresent() -> Bool + /// Java method `isImplicit`. + /// + /// ### Java method signature + /// ```java + /// public boolean java.lang.reflect.Parameter.isImplicit() + /// ``` @JavaMethod open func isImplicit() -> Bool } diff --git a/Sources/JavaStdlib/JavaLangReflect/generated/TypeVariable.swift b/Sources/JavaStdlib/JavaLangReflect/generated/TypeVariable.swift index 76c21b7d..a9fe224e 100644 --- a/Sources/JavaStdlib/JavaLangReflect/generated/TypeVariable.swift +++ b/Sources/JavaStdlib/JavaLangReflect/generated/TypeVariable.swift @@ -3,40 +3,112 @@ import SwiftJava import SwiftJavaJNICore @JavaInterface("java.lang.reflect.TypeVariable", extends: Type.self) -public struct TypeVariable: CustomStringConvertible { - @JavaMethod - public func getGenericDeclaration() -> GenericDeclaration! +public struct TypeVariable { + /// Java method `getGenericDeclaration`. + /// + /// ### Java method signature + /// ```java + /// public abstract D java.lang.reflect.TypeVariable.getGenericDeclaration() + /// ``` + @JavaMethod(typeErasedResult: "D!", typeErasedResultBound: GenericDeclaration?.self) + public func getGenericDeclaration() -> D! + /// Java method `getAnnotatedBounds`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.reflect.AnnotatedType[] java.lang.reflect.TypeVariable.getAnnotatedBounds() + /// ``` @JavaMethod public func getAnnotatedBounds() -> [AnnotatedType?] + /// Java method `getName`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.String java.lang.reflect.TypeVariable.getName() + /// ``` @JavaMethod public func getName() -> String + /// Java method `getBounds`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.reflect.Type[] java.lang.reflect.TypeVariable.getBounds() + /// ``` @JavaMethod public func getBounds() -> [Type?] + /// Java method `getTypeName`. + /// + /// ### Java method signature + /// ```java + /// public default java.lang.String java.lang.reflect.Type.getTypeName() + /// ``` @JavaMethod public func getTypeName() -> String + /// Java method `isAnnotationPresent`. + /// + /// ### Java method signature + /// ```java + /// public default boolean java.lang.reflect.AnnotatedElement.isAnnotationPresent(java.lang.Class) + /// ``` @JavaMethod public func isAnnotationPresent(_ arg0: JavaClass?) -> Bool - @JavaMethod - public func getAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public abstract T java.lang.reflect.AnnotatedElement.getAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getAnnotationsByType(_ arg0: JavaClass?) -> [T?] + /// Java method `getAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedElement.getAnnotations() + /// ``` @JavaMethod public func getAnnotations() -> [Annotation?] - @JavaMethod - public func getDeclaredAnnotation(_ arg0: JavaClass?) -> Annotation! + /// Java method `getDeclaredAnnotation`. + /// + /// ### Java method signature + /// ```java + /// public default T java.lang.reflect.AnnotatedElement.getDeclaredAnnotation(java.lang.Class) + /// ``` + @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Annotation?.self) + public func getDeclaredAnnotation(_ arg0: JavaClass?) -> T! + /// Java method `getDeclaredAnnotationsByType`. + /// + /// ### Java method signature + /// ```java + /// public default T[] java.lang.reflect.AnnotatedElement.getDeclaredAnnotationsByType(java.lang.Class) + /// ``` @JavaMethod - public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [Annotation?] + public func getDeclaredAnnotationsByType(_ arg0: JavaClass?) -> [T?] + /// Java method `getDeclaredAnnotations`. + /// + /// ### Java method signature + /// ```java + /// public abstract java.lang.annotation.Annotation[] java.lang.reflect.AnnotatedElement.getDeclaredAnnotations() + /// ``` @JavaMethod public func getDeclaredAnnotations() -> [Annotation?] } diff --git a/Sources/SwiftJava/Macros.swift b/Sources/SwiftJava/Macros.swift index c0993254..7c384425 100644 --- a/Sources/SwiftJava/Macros.swift +++ b/Sources/SwiftJava/Macros.swift @@ -134,16 +134,24 @@ public macro JavaStaticField(_ javaFieldName: String? = nil, isFinal: Bool = fal /// /// In order to mark a generic return type you must indicate it to the @JavaMethod macro like this: /// ```swift -/// // Java: class Test { public get(); } +/// // Java: class Test { public T get(); } /// @JavaMethod(typeErasedResult: "T!") /// func get() -> T! /// ``` /// This allows the macro to form a call into the get() method, which at runtime, will have an `java.lang.Object` /// returning method signature, and then, convert the result to the expected `T` type on the Swift side. +/// +/// If the return type is a bounded type parameter, specify the bound type instead of `java.lang.Object`: +/// ```swift +/// // Java: class Test { public T get(); } +/// @JavaMethod(typeErasedResult: "T!", typeErasedResultBound: Animal?.self) +/// func get() -> T! +/// ``` @attached(body) -public macro JavaMethod( +public macro JavaMethod( _ javaMethodName: String? = nil, - typeErasedResult: String? = nil + typeErasedResult: String? = nil, + typeErasedResultBound: ResultBoundType.Type = JavaObject?.self ) = #externalMacro(module: "SwiftJavaMacros", type: "JavaMethodMacro") /// Attached macro that turns a Swift method on JavaClass into one that wraps diff --git a/Sources/SwiftJavaMacros/JavaMethodMacro.swift b/Sources/SwiftJavaMacros/JavaMethodMacro.swift index 9996127d..3e685137 100644 --- a/Sources/SwiftJavaMacros/JavaMethodMacro.swift +++ b/Sources/SwiftJavaMacros/JavaMethodMacro.swift @@ -53,10 +53,8 @@ extension JavaMethodMacro: BodyMacro { var resultStatements: [CodeBlockItemSyntax] = [] let funcName = - if case .argumentList(let arguments) = node.arguments, - let argument = arguments.first, - argument.label?.text != "typeErasedResult", - let stringLiteral = argument.expression.as(StringLiteralExprSyntax.self), + if let expression = node.arguments?.firstExpr(label: nil), + let stringLiteral = expression.as(StringLiteralExprSyntax.self), stringLiteral.segments.count == 1, case let .stringSegment(funcNameSegment)? = stringLiteral.segments.first { @@ -91,10 +89,8 @@ extension JavaMethodMacro: BodyMacro { } let genericResultType: String? = - if case let .argumentList(arguments) = node.arguments, - let element = arguments.first(where: { $0.label?.text == "typeErasedResult" }), - let stringLiteral = element.expression - .as(StringLiteralExprSyntax.self), + if let expression = node.arguments?.firstExpr(label: "typeErasedResult"), + let stringLiteral = expression.as(StringLiteralExprSyntax.self), stringLiteral.segments.count == 1, case let .stringSegment(wrapperName)? = stringLiteral.segments.first { @@ -110,6 +106,13 @@ extension JavaMethodMacro: BodyMacro { nil } + let typeErasedResultBound: String? = + if let expression = node.arguments?.firstExpr(label: "typeErasedResultBound") { + expression.trimmedDescription + } else { + nil + } + // Determine the result type let resultType: String = if let returnClause = funcDecl.signature.returnClause { @@ -117,7 +120,7 @@ extension JavaMethodMacro: BodyMacro { // we need to type-erase the signature, because on JVM level generics are erased and we'd otherwise // form a signature with the "concrete" type, which would not match the real byte-code level signature // of the method we're trying to call -- which would result in a MethodNotFound exception. - ", resultType: /*type-erased:\(genericResultType)*/JavaObject?.self" + ", resultType: /*type-erased:\(genericResultType)*/\(typeErasedResultBound ?? "JavaObject?.self")" } else { ", resultType: \(returnClause.type.typeReferenceString).self" } diff --git a/Sources/SwiftJavaMacros/SwiftSyntaxUtils.swift b/Sources/SwiftJavaMacros/SwiftSyntaxUtils.swift index 08607056..baf62ffa 100644 --- a/Sources/SwiftJavaMacros/SwiftSyntaxUtils.swift +++ b/Sources/SwiftJavaMacros/SwiftSyntaxUtils.swift @@ -29,3 +29,15 @@ extension FunctionParameterSyntax { return firstName.text } } + +extension AttributeSyntax.Arguments { + func firstExpr(label: String?) -> ExprSyntax? { + if case let .argumentList(arguments) = self, + let element = arguments.first(where: { $0.label?.text == label }) + { + element.expression + } else { + nil + } + } +} diff --git a/Sources/SwiftJavaToolLib/JavaClassTranslator.swift b/Sources/SwiftJavaToolLib/JavaClassTranslator.swift index 31bc2e56..48cf3313 100644 --- a/Sources/SwiftJavaToolLib/JavaClassTranslator.swift +++ b/Sources/SwiftJavaToolLib/JavaClassTranslator.swift @@ -988,9 +988,26 @@ extension JavaClassTranslator { parameters.append("\"init\"") } if hasTypeEraseGenericResultType { - parameters.append("typeErasedResult: \"\(resultType)\"") + let returnType = javaMethod.getReturnType()! + parameters.append(#"typeErasedResult: "\#(resultType)""#) + if returnType.getName() != "java.lang.Object" { + var boundType = try translator.getSwiftTypeNameAsString( + method: javaMethod, + returnType.as(Type.self), + substitution: substitution, + preferValueTypes: true, + outerOptional: .nonoptional + ) + // `getSwiftTypeNameAsString` does not include generic parameters for non parameterized type + if let returnClass = returnType.as(JavaClass.self) { + let typeParameters = returnClass.getTypeParameters() + if !typeParameters.isEmpty { + boundType += "<\([String](repeating: "JavaObject", count: typeParameters.count).joined(separator: ", "))>" + } + } + parameters.append(#"typeErasedResultBound: \#(boundType)?.self"#) + } } - // TODO: generic parameters? if !parameters.isEmpty { methodAttributeStr += "(" diff --git a/Tests/SwiftJavaToolLibTests/CompileJavaWrapTools.swift b/Tests/SwiftJavaToolLibTests/CompileJavaWrapTools.swift index df21bc68..1fb48fc4 100644 --- a/Tests/SwiftJavaToolLibTests/CompileJavaWrapTools.swift +++ b/Tests/SwiftJavaToolLibTests/CompileJavaWrapTools.swift @@ -162,7 +162,9 @@ func assertWrapJavaOutput( "Expected chunk: \n" + "\(expectedChunk.yellow)" + "\n" + "not found in:\n" + "\(swiftCompleteOutputText)" XCTAssertTrue( checkAgainstText.contains(checkAgainstExpectedChunk), - "\(failureMessage)" + "\(failureMessage)", + file: file, + line: line ) } } diff --git a/Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift b/Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift index c64735e6..af36ab6f 100644 --- a/Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift +++ b/Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift @@ -489,7 +489,7 @@ final class GenericsWrapJavaTests: XCTestCase { open class Something: JavaObject { """, """ - @JavaMethod(typeErasedResult: "M!") + @JavaMethod(typeErasedResult: "M!", typeErasedResultBound: Map?.self) open func putIn(_ arg0: M?) -> M! """, ]