Reflection API

FLX language, it’s virtual machine, FLX Lisp interpreter, and all of its computational features are implemented and based on using Kotlin and JVM. To provide a mechanism to extend FLX capabilities and functionality, FLX provides a simple reflection API consisting of the following functions.

Reflection API Functions

Returns a Java Class for the given fully qualified class name.

  • className – the fully qualified class name (e.g. java.lang.Int) as a String

Returns: a Java Class.

Creates a new instance of the specified Java class. The Class constructor may require parameters.

  • class – a Java Class
  • params – optional parameters.

Returns: The created instance

Returns the Java Class of the given object.

  • object – an Any object

Returns: A Java Class

Returns the value of the specified property of the given object.

  • object – an Any object
  • property – the name of the property as a `String“

Returns: the value of the property

Invokes the specified method for the given object. The invoked method may have parameters that should be given as additional parameters.

  • object – an Any object
  • method – the name of the method

Returns: the value from method invocation

Tests if the given object is an instance of the specified class. This function is same as (isInstance object class), but provides more Kotlin-ish syntax.

  • object – an Any object
  • class – a Java Class

Returns: a Boolean value.

Tests if the given object is an instance of the specified class. This function is same as (is object class), but provides more Lisp-ish syntax.

  • object – an Any object
  • class – a Java Class

Returns: a Boolean value.

Sets the given value to be current value of the specified property of the given object.

  • object – an Any object
  • property – the name of the property as a String
  • value – the value to be set

Returns: the set value

Examples

Here are examples of using the Reflection API functions described above.

  1. FLX provides a dedicated Class object that is actually just a Java class.
  2. Class.forName function returns a Java class for the specified fully qualified class name.
  3. Class.new function creates an instance of the given Java class. If the constructor of the class takes parameters they can be provided as arguments for this function.
  4. invoke function calls the function (e.g. setName) specified by its name and with parameters given as function arguments. In the example, function setName has only one parameter of type String.
  5. getProperty obtains the specified property of the given object. The property is specified by its name.
  6. setProperty sets the value of the specified property of the given object. The property is specified by its name.
  7. getClass returns the Java class of the given object.
  8. isInstance checks if the given object is instance of the class.

Implementation of FLX Reflection API naturally is based on Java’s Reflection API.

FLX’s Search Forms Picker provides means for obtaining Java class forms to be added to code in visual code editor:

  1. Type the fully qualified class name e.g. java.lang.String.
  2. If a Java class with the typed name exists it will be provided as a Java Class form.