Class JvmFunction

java.lang.Object
org.daiitech.naftah.builtin.lang.JvmFunction
All Implemented Interfaces:
Serializable, JvmExecutable

public final class JvmFunction extends Object implements Serializable, JvmExecutable
Represents a Java method that can be invoked dynamically.

This class wraps a Method along with its containing class, and stores information about whether the method is static and invocable. It also supports serialization, recreating the transient Method upon deserialization by matching the method name.

Author:
Chakib Daii
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • qualifiedCall

      private final String qualifiedCall
      Fully qualified call signature of the method.
    • clazz

      private final Class<?> clazz
      The class that declares the method.
    • methodName

      private final String methodName
      The name of the method.
    • methodParameterTypes

      private final Class<?>[] methodParameterTypes
      The method parameter types.
    • isStatic

      private final boolean isStatic
      Whether the method is static.
    • isInvocable

      private final boolean isInvocable
      Whether the method is invocable.
    • method

      private transient Method method
      The reflected method instance. Marked transient for serialization.
  • Constructor Details

    • JvmFunction

      public JvmFunction(String qualifiedCall, Class<?> clazz, Method method, boolean isStatic, boolean isInvocable)
      Constructs a new JvmFunction.
      Parameters:
      qualifiedCall - the fully qualified method call signature
      clazz - the class declaring the method
      method - the reflected method
      isStatic - true if the method is static
      isInvocable - true if the method is invocable
  • Method Details

    • of

      public static JvmFunction of(String qualifiedCall, Class<?> clazz, Method method)
      Factory method to create a JvmFunction from a class and method.
      Parameters:
      qualifiedCall - the fully qualified method call signature
      clazz - the class declaring the method
      method - the reflected method
      Returns:
      a new JvmFunction instance
    • getQualifiedCall

      public String getQualifiedCall()
      Gets the fully qualified call signature.
      Returns:
      the qualified call string
    • getClazz

      public Class<?> getClazz()
      Gets the class declaring the method.
      Returns:
      the declaring class
    • getMethod

      public Method getMethod()
      Gets the reflected method.
      Returns:
      the method instance
    • getMethodName

      public String getMethodName()
      Gets the method's name.
      Returns:
      the method name
    • getMethodParameterTypes

      public Class<?>[] getMethodParameterTypes()
      Returns the parameter types accepted by this method.
      Returns:
      an array of Class objects representing the method's parameter types
    • isStatic

      public boolean isStatic()
      Checks if the method is static.
      Returns:
      true if static, false otherwise
    • isInvocable

      public boolean isInvocable()
      Checks if the method is invocable.
      Returns:
      true if invocable, false otherwise
    • getExecutable

      public Executable getExecutable()
      Returns the underlying reflective Executable instance represented by this object.

      This may correspond to a Java method, constructor, or other callable element recognized by the JVM.

      Specified by:
      getExecutable in interface JvmExecutable
      Returns:
      the associated Executable instance
    • writeObject

      private void writeObject(ObjectOutputStream oos) throws IOException
      Custom serialization logic to write the object's non-transient fields.
      Parameters:
      oos - the object output stream
      Throws:
      IOException - if an I/O error occurs
    • readObject

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
      Custom deserialization logic to restore the transient Method by searching methods with matching name and parameter types in the class.
      Parameters:
      ois - the object input stream
      Throws:
      IOException - if an I/O error occurs
      ClassNotFoundException - if the class of a serialized object cannot be found
    • toString

      public String toString()
      Returns a concise string representation of this JvmFunction, formatted in Arabic as <دالة qualifiedCall>.

      This is intended for short, human-readable descriptions, using the Arabic label "دالة" (which means "function" or "method").

      Overrides:
      toString in class Object
      Returns:
      a string in the format <دالة qualifiedCall>
    • toDetailedString

      public String toDetailedString()
      Returns a detailed, Arabic-formatted string representation of this JvmFunction, including metadata about the method, such as return type, parameters, modifiers, and annotations.

      The output is structured in a human-readable Arabic format and includes phonetic transliterations (into Arabic script) for class names, return types, modifiers, and annotations using ClassUtils.getQualifiedName(String) or ScriptUtils.transliterateToArabicScriptDefault(String...).

      The following information is included:

      • Qualified method name — including both the fully qualified and transliterated form
      • Return type — full Java type name with its Arabic transliteration
      • Parameters — each parameter type with its fully qualified name and transliteration
      • Modifiers — Java keywords (e.g., public static) with their Arabic transliteration
      • Annotations — fully qualified annotation names with transliterations

      Example output:

       تفاصيل الدالة:
       - الاسم المؤهل: com.example.MyClass::greet - كوم:إِكْزامْبِل:ماي_كْلاس::غْرِيتْ
       - نوع الإرجاع: java.lang.String - جافا:لانغ:سترينج
       - المعاملات:
       - java.lang.String - جافا:لانغ:سترينج
       - المُعدّلات: public static - بَبْلِكْ سْتَاتِكْ
       - التعليقات التوضيحية:
       - @java.lang.Deprecated - جافا:لانغ:دِبْرِكَيْتِدْ
       
      Returns:
      a formatted, multi-line Arabic string describing the method's structure and metadata