Class MethodSignature
java.lang.Object
cloud.opencode.base.reflect.invokable.MethodSignature
Method Signature for matching and override detection
方法签名 - 用于匹配和覆盖检测
Represents a method signature consisting of name, parameter types, and optionally a return type. Supports signature matching, override compatibility detection, and JVM descriptor generation.
表示由名称、参数类型和可选返回类型组成的方法签名。 支持签名匹配、覆盖兼容性检测和JVM描述符生成。
Features | 主要功能:
- Signature matching by name and parameter types - 按名称和参数类型匹配签名
- Override detection with covariant return type support - 支持协变返回类型的覆盖检测
- JVM method descriptor generation - JVM方法描述符生成
- Human-readable signature formatting - 人类可读的签名格式化
Usage Examples | 使用示例:
// From Method
MethodSignature sig = MethodSignature.of(method);
// From name and parameter types
MethodSignature sig = MethodSignature.of("foo", String.class, int.class);
// Check override compatibility
boolean canOverride = sig.isOverrideOf(superMethod);
// Generate JVM descriptor
String descriptor = sig.toDescriptor(); // e.g., "(Ljava/lang/String;I)V"
Security | 安全性:
- Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
- Null-safe: No (name and parameter types must be non-null) - 空值安全: 否(名称和参数类型须非空)
- Since:
- JDK 25, opencode-base-reflect V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks equality based on name and parameter types (NOT return type, per Java overloading rules) 基于名称和参数类型检查相等性(不包含返回类型,符合Java重载规则)getName()Gets the method name 获取方法名称intGets the number of parameters 获取参数数量Class<?>[]Gets the parameter types (defensive copy) 获取参数类型(防御性复制)Class<?> Gets the return type, may be null if not specified 获取返回类型,如果未指定可以为nullinthashCode()Hash code based on name and parameter types (NOT return type) 基于名称和参数类型的哈希码(不包含返回类型)booleanisOverrideCompatible(MethodSignature superSignature) Checks if this signature is override-compatible with the given super signature.booleanisOverrideOf(Method superMethod) Checks if a method with this signature can override the given super method.booleanmatches(MethodSignature other) Checks if this signature matches the given signature (name + parameter types) 检查此签名是否匹配给定签名(名称 + 参数类型)booleanChecks if this signature matches the given method (name + parameter types) 检查此签名是否匹配给定方法(名称 + 参数类型)static MethodSignatureCreates a MethodSignature from a Method, extracting name, parameter types, and return type 从Method创建MethodSignature,提取名称、参数类型和返回类型static MethodSignatureCreates a MethodSignature from name and parameter types (no return type) 从名称和参数类型创建MethodSignature(不包含返回类型)Generates the JVM method descriptor string.Generates a human-readable string representation of the method signature.toString()Returns the readable string representation 返回可读字符串表示static MethodSignaturewithReturnType(String name, Class<?> returnType, Class<?>... parameterTypes) Creates a MethodSignature from name, return type, and parameter types 从名称、返回类型和参数类型创建MethodSignature
-
Method Details
-
of
Creates a MethodSignature from a Method, extracting name, parameter types, and return type 从Method创建MethodSignature,提取名称、参数类型和返回类型- Parameters:
method- the method | 方法- Returns:
- MethodSignature instance | MethodSignature实例
- Throws:
OpenReflectException- if method is null | 如果方法为null
-
of
Creates a MethodSignature from name and parameter types (no return type) 从名称和参数类型创建MethodSignature(不包含返回类型)- Parameters:
name- the method name | 方法名称parameterTypes- the parameter types | 参数类型- Returns:
- MethodSignature instance | MethodSignature实例
- Throws:
OpenReflectException- if name is null or empty | 如果名称为null或空
-
withReturnType
public static MethodSignature withReturnType(String name, Class<?> returnType, Class<?>... parameterTypes) Creates a MethodSignature from name, return type, and parameter types 从名称、返回类型和参数类型创建MethodSignature- Parameters:
name- the method name | 方法名称returnType- the return type | 返回类型parameterTypes- the parameter types | 参数类型- Returns:
- MethodSignature instance | MethodSignature实例
- Throws:
OpenReflectException- if name is null or empty | 如果名称为null或空
-
getName
-
getParameterTypes
Gets the parameter types (defensive copy) 获取参数类型(防御性复制)- Returns:
- the parameter types | 参数类型
-
getReturnType
Gets the return type, may be null if not specified 获取返回类型,如果未指定可以为null- Returns:
- the return type or null | 返回类型或null
-
getParameterCount
public int getParameterCount()Gets the number of parameters 获取参数数量- Returns:
- the parameter count | 参数数量
-
matches
Checks if this signature matches the given method (name + parameter types) 检查此签名是否匹配给定方法(名称 + 参数类型)- Parameters:
method- the method to match against | 要匹配的方法- Returns:
- true if name and parameter types match | 如果名称和参数类型匹配返回true
-
matches
Checks if this signature matches the given signature (name + parameter types) 检查此签名是否匹配给定签名(名称 + 参数类型)- Parameters:
other- the other signature to match against | 要匹配的其他签名- Returns:
- true if name and parameter types match | 如果名称和参数类型匹配返回true
-
isOverrideOf
Checks if a method with this signature can override the given super method. Requires name and parameter types to match, and the return type to be covariant (this return type must be assignable to the super method's return type). 检查具有此签名的方法是否可以覆盖给定的父方法。 要求名称和参数类型匹配,且返回类型协变(此返回类型必须可赋值给父方法的返回类型)。- Parameters:
superMethod- the super method to check against | 要检查的父方法- Returns:
- true if this can override the super method | 如果可以覆盖返回true
-
isOverrideCompatible
Checks if this signature is override-compatible with the given super signature. Requires name and parameter types to match, and if both have return types, the return type must be covariant. 检查此签名是否与给定的父签名覆盖兼容。 要求名称和参数类型匹配,如果两者都有返回类型,则返回类型必须协变。- Parameters:
superSignature- the super signature to check against | 要检查的父签名- Returns:
- true if override-compatible | 如果覆盖兼容返回true
-
toDescriptor
Generates the JVM method descriptor string. Uses void as return type if not specified. 生成JVM方法描述符字符串。 如果未指定返回类型则使用void。Examples | 示例:
"(Ljava/lang/String;I)V"forfoo(String, int): void"()Ljava/lang/String;"forbar(): String
- Returns:
- the JVM method descriptor | JVM方法描述符
-
toReadableString
Generates a human-readable string representation of the method signature. 生成方法签名的人类可读字符串表示。Format:
name(Type1, Type2): ReturnType- Returns:
- the readable string | 可读字符串
-
equals
-
hashCode
-
toString
-