Class Invokable<T,R>

java.lang.Object
cloud.opencode.base.reflect.invokable.Invokable<T,R>
Type Parameters:
T - the declaring class type | 声明类类型
R - the return type | 返回类型
All Implemented Interfaces:
AnnotatedElement
Direct Known Subclasses:
ConstructorInvokable, MethodInvokable

public abstract class Invokable<T,R> extends Object implements AnnotatedElement
Invokable - Fluent Wrapper for Method/Constructor (Similar to Guava Invokable) 可调用 - 方法/构造器的流畅包装(对标 Guava Invokable)

Provides a unified, fluent API for invoking methods and constructors with type safety and rich metadata access.

为调用方法和构造器提供统一、流畅的API,具有类型安全和丰富的元数据访问。

Features | 主要功能:

  • Unified method/constructor API - 统一的方法/构造器API
  • Type-safe invocation - 类型安全调用
  • Modifier inspection - 修饰符检查
  • Annotation access - 注解访问

Usage Examples | 使用示例:

// From Method
Invokable<Service, String> invokable = Invokable.from(method);
String result = invokable.invoke(service, args);

// From Constructor
Invokable<User, User> ctor = Invokable.from(constructor);
User user = ctor.invoke(null, "name", 25);

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: No (wrapped method/constructor must be non-null) - 空值安全: 否(包装的方法/构造器须非空)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • Invokable

      public Invokable()
  • Method Details

    • from

      public static <T> Invokable<T,Object> from(Method method)
      Creates an Invokable from a Method 从Method创建Invokable
      Type Parameters:
      T - the declaring class type | 声明类类型
      Parameters:
      method - the method | 方法
      Returns:
      Invokable instance | Invokable实例
    • from

      public static <T> Invokable<T,T> from(Constructor<T> constructor)
      Creates an Invokable from a Constructor 从Constructor创建Invokable
      Type Parameters:
      T - the type | 类型
      Parameters:
      constructor - the constructor | 构造器
      Returns:
      Invokable instance | Invokable实例
    • getAccessibleObject

      protected abstract AccessibleObject getAccessibleObject()
      Gets the underlying accessible object 获取底层可访问对象
      Returns:
      the accessible object | 可访问对象
    • getDeclaringClass

      public abstract TypeToken<T> getDeclaringClass()
      Gets the declaring class as TypeToken 获取声明类的TypeToken
      Returns:
      TypeToken of declaring class | 声明类的TypeToken
    • getReturnType

      public abstract TypeToken<? extends R> getReturnType()
      Gets the return type as TypeToken 获取返回类型的TypeToken
      Returns:
      TypeToken of return type | 返回类型的TypeToken
    • getParameters

      public abstract List<Parameter> getParameters()
      Gets the parameter list 获取参数列表
      Returns:
      list of Parameters | 参数列表
    • getParameterTypes

      public abstract List<TypeToken<?>> getParameterTypes()
      Gets the parameter types 获取参数类型
      Returns:
      list of parameter TypeTokens | 参数TypeToken列表
    • getExceptionTypes

      public abstract List<TypeToken<? extends Throwable>> getExceptionTypes()
      Gets the exception types 获取异常类型
      Returns:
      list of exception TypeTokens | 异常TypeToken列表
    • getTypeParameters

      public abstract TypeVariable<?>[] getTypeParameters()
      Gets the type parameters 获取类型参数
      Returns:
      array of type variables | 类型变量数组
    • invoke

      public abstract R invoke(T receiver, Object... args) throws InvocationTargetException, IllegalAccessException
      Invokes the method/constructor 调用方法/构造器
      Parameters:
      receiver - the receiver object | 接收对象
      args - the arguments | 参数
      Returns:
      the result | 结果
      Throws:
      InvocationTargetException - if invocation fails | 如果调用失败
      IllegalAccessException - if access denied | 如果访问被拒绝
    • invokeForced

      public R invokeForced(T receiver, Object... args)
      Invokes ignoring access modifiers 调用(忽略访问修饰符)
      Parameters:
      receiver - the receiver object | 接收对象
      args - the arguments | 参数
      Returns:
      the result | 结果
    • invokeSafe

      public Optional<R> invokeSafe(T receiver, Object... args)
      Safely invokes returning Optional 安全调用返回Optional
      Parameters:
      receiver - the receiver object | 接收对象
      args - the arguments | 参数
      Returns:
      Optional of result | 结果的Optional
    • setAccessible

      public Invokable<T,R> setAccessible(boolean flag)
      Sets accessible flag 设置可访问标志
      Parameters:
      flag - the flag | 标志
      Returns:
      this | 当前对象
    • isPublic

      public boolean isPublic()
      Checks if public 检查是否为public
      Returns:
      true if public | 如果是public返回true
    • isProtected

      public boolean isProtected()
      Checks if protected 检查是否为protected
      Returns:
      true if protected | 如果是protected返回true
    • isPrivate

      public boolean isPrivate()
      Checks if private 检查是否为private
      Returns:
      true if private | 如果是private返回true
    • isPackagePrivate

      public boolean isPackagePrivate()
      Checks if package-private 检查是否为包私有
      Returns:
      true if package-private | 如果是包私有返回true
    • isStatic

      public boolean isStatic()
      Checks if static 检查是否为static
      Returns:
      true if static | 如果是static返回true
    • isFinal

      public boolean isFinal()
      Checks if final 检查是否为final
      Returns:
      true if final | 如果是final返回true
    • isAbstract

      public boolean isAbstract()
      Checks if abstract 检查是否为abstract
      Returns:
      true if abstract | 如果是abstract返回true
    • isNative

      public boolean isNative()
      Checks if native 检查是否为native
      Returns:
      true if native | 如果是native返回true
    • isSynchronized

      public boolean isSynchronized()
      Checks if synchronized 检查是否为synchronized
      Returns:
      true if synchronized | 如果是synchronized返回true
    • isVarArgs

      public abstract boolean isVarArgs()
      Checks if varargs 检查是否为可变参数
      Returns:
      true if varargs | 如果是可变参数返回true
    • isSynthetic

      public abstract boolean isSynthetic()
      Checks if synthetic 检查是否为合成
      Returns:
      true if synthetic | 如果是合成返回true
    • getName

      public abstract String getName()
      Gets the name 获取名称
      Returns:
      the name | 名称
    • getModifiers

      public abstract int getModifiers()
      Gets the modifiers 获取修饰符
      Returns:
      the modifiers | 修饰符
    • getDeclaringClassRaw

      public abstract Class<?> getDeclaringClassRaw()
      Gets the declaring class as raw Class 获取声明类的原始Class
      Returns:
      the declaring class | 声明类
    • isOverridable

      public boolean isOverridable()
      Checks if overridable (not final, not private, not static) 检查是否可重写(非final、非private、非static)
      Returns:
      true if overridable | 如果可重写返回true
    • isAnnotationPresent

      public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
      Specified by:
      isAnnotationPresent in interface AnnotatedElement
    • getAnnotation

      public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
      Specified by:
      getAnnotation in interface AnnotatedElement
    • getAnnotations

      public Annotation[] getAnnotations()
      Specified by:
      getAnnotations in interface AnnotatedElement
    • getAnnotationsByType

      public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass)
      Specified by:
      getAnnotationsByType in interface AnnotatedElement
    • getDeclaredAnnotation

      public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass)
      Specified by:
      getDeclaredAnnotation in interface AnnotatedElement
    • getDeclaredAnnotationsByType

      public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass)
      Specified by:
      getDeclaredAnnotationsByType in interface AnnotatedElement
    • getDeclaredAnnotations

      public Annotation[] getDeclaredAnnotations()
      Specified by:
      getDeclaredAnnotations in interface AnnotatedElement