B4J Programming Press on the image to return to the main documentation page.

jReflection

Written by Andrew Graham

This library contains a Reflector object that allows access to methods and fields
of Java objects that are not exposed to the B4J language. It does this by means of
a facility called "Reflection" that uses meta-data for objects that is included in the
application package and allows dynamic access to fields and methods at runtime.

The specifications for the primitive Java types recognised by this object are
"java.lang.boolean", "java.lang.byte", "java.lang.char", "java.lang.short",
"java.lang.int", "java.lang.long", "java.lang.float", and "java.lang.double"
Note the lower-cased names. The single uppercase letter representation, as
listed for arrays below, is also recognised.

The specifications for boxed primitive Java types which are true objects are
"java.lang.Boolean", "java.lang.Byte", "java.lang.Char", "java.lang.Short",
"java.lang.Int", "java.lang.Long", "java.lang.Float", and "java.lang.Double"
Note the upper-cased names.

The specifications for arrays of primitive types is a number of square left brackets
corresponding to the rank of the array followed by a single letter indicating the type.
B represents the byte primitive type, S represents the short primitive type,
I represents the int primitive type, J represents the long primitive type,
F represents the float primitive type, D represents the double primitive type,
C represents the char primitive type, Z represents the boolean primitive type.
So a two dimension integer array is represented by "[[I".

The specifications for arrays of objects is similar but with the single letter replaced
the letter "L", followed by the class name terminated by a semicolon ";".
So a single dimension array of strings is represented by "[Ljava.lang.String;"

List of types:

Reflector

Reflector

This is the object that does the reflection. In order to use this successfully you will
need an understanding of the use of Java classes and their fields and methods.

Java is case sensitive and as used for B4J does not support properties. Properties as
implemented in B4J are actually methods with lower case prefixes 'set' and 'get'.
'set' methods take a single parameter and return void, 'get' methods take no parameters and
return the requested values. Any other method signatures are exposed by B4J as normal methods.
For example the Left property of a Node is actually implemented in Java code as two methods,
int getLeft() and void setLeft(int left). The B4J compiler makes them look like a single property
to the programmer.

Events:

None

Members:


  AddChangeListener (b4jeventname As String, property As String)

  AddEventFilter (b4jeventname As String, jfxeventtypename As String)

  AddEventHandler (b4jeventname As String, jfxeventtypename As String)

  AddEventHandler2 (b4jeventname As String, property As String)

  CreateObject (type As String) As Object

  CreateObject2 (type As String, args() As Object, types() As String) As Object

  DebugMode As Boolean

  GetArray (indeces() As Int) As Object

  GetB4JClass (component As String) As Class

  GetBA As BA

  GetField (field As String) As Object

  GetField2 (fieldinfo As Field) As Object

  GetFieldInfo (field As String) As Field

  GetMethod (method As String, types() As String) As Method

  GetModuleBA (module As String) As BA

  GetMostCurrent (module As String) As Object

  GetPublicField (field As String) As Object

  GetStacktrace As StackTraceElement()

  GetStaticField (classname As String, field As String) As Object

  InvokeMethod (instance As Object, method As Method, args() As Object) As Object

  IsNull As Boolean [read only]

  ParseStackTraceElement (element As StackTraceElement) As String()

  PrintStacktrace

  RunMethod (method As String) As Object

  RunMethod2 (method As String, arg1 As String, type1 As String) As Object

  RunMethod3 (method As String, arg1 As String, type1 As String, arg2 As String, type2 As String) As Object

  RunMethod4 (method As String, args() As Object, types() As String) As Object

  RunPublicmethod (Method As String, Args() As Object, types() As String) As Object

  RunStaticMethod (classname As String, method As String, args() As Object, types() As String) As Object

  SetArray (indeces() As Int, value As String, type As String)

  SetArray2 (indeces() As Int, value As Object)

  SetField (field As String, value As String, type As String)

  SetField2 (field As String, value As Object)

  SetField3 (fieldinfo As Field, value As String, type As String)

  SetField4 (fieldinfo As Field, value As Object)

  SetPublicField (field As String, value As String, type As String)

  SetPublicField2 (field As String, value As Object)

  SetStaticField (classname As String, field As String, value As String, type As String)

  SetStaticField2 (classname As String, field As String, value As Object)

  Target As Object

  TargetRank As Int()

  ToString As String

  TypeName As String [read only]

  Version As Double [read only]

Members description:

AddChangeListener (b4jeventname As String, property As String)
Many JavaFX Nodes have internal properties that implement the ObservableValue interface that allows a ChangeListener
to be registered with them. That listener will then be called when the value of the property changes.

This method allows a change listener to be added to the specified property of the current target that will call
a Sub named b4Jeventname_Changed(OldVal as Object, NewVal As Object) when the value of that property changes.

Depending upon the underlying type of the ObservableValue OldVal and NewVal might be able to be directly used in B4J
by assigning them to variables of the correct type, or it might be necessary to manipulate them with a JavaObject or a Reflector.
AddEventFilter (b4jeventname As String, jfxeventtypename As String)
Events in JavaFX "filter down" from the top of the scene graph and then "bubble up" to the top if not consumed on the way.
A node can "filter" the event on the way down and "handle" it on the way up. It can also stop the propagation of the event
in either direction.

This method allows an event filter to be added to the current target that will call a Sub named b4jeventname_Filter(e As Event))
when a specified event or one of a related set of events occurs. The event object passed will have a type derived from the
Event class and will be appropriate to the type of the event filter. The event object may be manipulated with a JavaObject
or a Reflector to obtain the data relevant to the event.

The jfxeventtypename parameter is the fully qualified class name of a JavaFX event type plus one of its' static EventType field names.
eg. "javafx.scene.input.KeyEvent.ANY" or "javafx.scene.input.KeyEvent.KEY_TYPED"

To prevent a child node seeing the event an event Sub can consume the event by calling Consume on the event object passed to it.
If this is done the event will propagate no further.
AddEventHandler (b4jeventname As String, jfxeventtypename As String)
Events in JavaFX "filter down" from the top of the scene graph and then "bubble up" to the top if not consumed on the way.
A node can "filter" the event on the way down and "handle" it on the way up.

This method allows an event handler to be added to the current target that will call a Sub named b4jeventname_Event(e As Event))
when a specified event or one of a related set of events occurs. The event object passed will have a type derived from the
Event class and will be appropriate to the type of the event handler. The event object may be manipulated with a JavaObject
or a Reflector to obtain the data relevant to the event.

The jfxeventtypename parameter is the fully qualified class name of a JavaFX event type plus one of it's static EventType field names.
eg. "javafx.scene.input.KeyEvent.ANY" or "javafx.scene.input.KeyEvent.KEY_TYPED"

To prevent a parent node seeing the event an event Sub can consume the event by calling Consume on the event object passed to it.
If this is done the event will propagate no further. Note that the default handlers for the JavaFX UI controls typically consume
most of the input events.
AddEventHandler2 (b4jeventname As String, property As String)
This is an alternative way of adding an event handler to a JavaFX node. It is less flexible than AddEventHandler in that it
only allows a single type of event to be handled rather than a set of related events and only supports a limited range of events.
The JavaFX documentation calls it a "convenience" way of setting events by specifying a property rather than an event type
but it seems no more convenient to me.

JavaFX Nodes have internal properties, generally named onSomeEvent that allow an EventHandler to be registered with them.
That handler will then be called when the event occurs. You can see which ones a node suupprts from the Javadoc for the node.

This method allows an event handler to be added to the specified property of the current target that will call a Sub named
b4jeventname_Event(e As Event)) when the event occurs. The event object passed will have a type derived from the Event class and
will be appropriate to the type of the event handler. The event object may be manipulated with a JavaObject or a Reflector to obtain
the data relevant to the event.

Events in JavaFX "bubble up" from the bottom of the scene graph. To prevent a parent node seeing the event an event Sub can
consume the event by calling Consume on the event object passed to it. If this is done the event will propagate no further.
CreateObject (type As String) As Object
Creates and returns a new object of the specified type using the default constructor.
CreateObject2 (type As String, args() As Object, types() As String) As Object
Creates and returns new object of the specified type using the constructor that matches
the array of type names given and passing it the arguments provided.

The array of type names is needed in order to find the correct constructor because
primitives passed in the Args array are boxed and so CreateNew cannot tell whether
to look for a target constructor that accepts a primitive parameter type or a boxed
primitive object type.
DebugMode As Boolean
Returns a boolean value, True if the program is running in Debug mode otherwise False.
GetArray (indeces() As Int) As Object
Returns the Object at the position(s) in an array specified by the contents of indices.
indices must be an integer array of the same rank as the Target array or an error will occur.
GetB4JClass (component As String) As Class
Returns the Java Class for the specified B4A module.
To use this requires a knowledge of the structure of a B4J application.
GetBA As BA
Returns the BA of the calling module
To use this requires a knowledge of the structure of a B4J application.
GetField (field As String) As Object
Returns the value of the field of the current target. Protected and private fields
may be accessed if allowed by any security manager which may be present.
Target must be an instance of a Class, not a Class object.
GetField2 (fieldinfo As Field) As Object
Returns the value of the field of the current target.
Target must be an instance of a Class, not a Class object.
GetFieldInfo (field As String) As Field
Finding a field from its string representation is expensive so this method can be used
to get the Field information object and save it for multiple accesses of the same field.
Protected and private fields may be accessed if allowed by any security manager which may be present.
GetMethod (method As String, types() As String) As Method
Finding a method from its string representation is expensive so this method can be used
to get the Method information object and save it for multiple invocations of the same method.

The String array of type names is needed in order to find the correct variant of the method.
GetModuleBA (module As String) As BA
Returns the BA instance for the specified B4J module.
Note that Class modules do not have a BA instance.
To use this requires a knowledge of the structure of a B4J application.
GetMostCurrent (module As String) As Object
Returns the current instance for the specified B4J module.
Note that Class modules do not have a current instance.
To use this requires a knowledge of the structure of a B4J application.
GetPublicField (field As String) As Object
Returns the value of the public field of the current target.
This is more efficient than GetField but can only access public fields.
Target must be an instance of a Class, not a Class object.
GetStacktrace As StackTraceElement()
Gets an array of StacktraceElements which represents the present stack.
GetStaticField (classname As String, field As String) As Object
Returns the value of the specified static field of the specified class.
Protected and private fields may be accessed if allowed by any security manager
which may be present.
Static fields may also be accessed with GetField and an instance of the class.
InvokeMethod (instance As Object, method As Method, args() As Object) As Object
Invoke the provided Method on the provided object instance and return the result.
IsNull As Boolean [read only]
Returns True if the present value of Target is Null.
ParseStackTraceElement (element As StackTraceElement) As String()
Gets an array of strings which describe the provided stack trace element

[0] = Returns the fully qualified name of the class containing the execution point represented by this stack trace element
[1] = Returns the name of the source file containing the execution point represented by this stack trace element
[2] = Returns the line number of the source line containing the execution point represented by this stack trace element.
[3] = Returns the name of the method containing the execution point represented by this stack trace element.
[4] = Returns a string representation of this stack trace element as printed by PrintStacktrace.
PrintStacktrace
Print a current stack trace to the standard error stream. This will appear in the B4J Logs.
RunMethod (method As String) As Object
Runs the specified method on the current target. Protected and private methods
may be accessed if allowed by any security manager which may be present.
RunMethod2 (method As String, arg1 As String, type1 As String) As Object
Runs the specified method on the current target passing it the argument provided.
Protected and private methods may be accessed if allowed by any security manager
which may be present.
RunMethod3 (method As String, arg1 As String, type1 As String, arg2 As String, type2 As String) As Object
Runs the specified method on the current object passing it the arguments provided.
Protected and private methods may be accessed if allowed by any security manager
which may be present.
RunMethod4 (method As String, args() As Object, types() As String) As Object
Runs the specified method on the current target passing it the arguments provided.
Protected and private methods may be accessed if allowed by any security manager
which may be present.

The String array of type names is needed in order to find the correct method because
primitives passed in the Args array are boxed and so RunMethod cannot tell whether to
look for a target method that accepts a primitive parameter type or a boxed primitive
object type.
RunPublicmethod (Method As String, Args() As Object, types() As String) As Object
Runs the specified method on the current target passing it the arguments provided.
This is more efficient that RunMethd4 but the method must be public.

The String array of type names is needed in order to find the correct method because
primitives passed in the Args array are boxed and so RunMethod cannot tell whether to
look for a target method that accepts a primitive parameter type or a boxed primitive
object type.
RunStaticMethod (classname As String, method As String, args() As Object, types() As String) As Object
Runs the specified static method of the specified class passing it the arguments provided.
Protected and private methods may be accessed if allowed by any security manager
which may be present.

The String array of type names is needed in order to find the correct method because
primitives passed in the Args array are boxed and so RunMethod cannot tell whether to
look for a target method that accepts a primitive parameter type or a boxed primitive
object type. For methods that take no parameters Null may passed for args and types.
SetArray (indeces() As Int, value As String, type As String)
Set the position(s) in an array specified by the contents of indices to the specified value.
indices must be an integer array of the same rank as the Target array or an error will occur.
SetArray2 (indeces() As Int, value As Object)
Set the position(s) in an array specified by the contents of indices to the specified value.
indices must be an integer array of the same rank as the Target array or an error will occur.
SetField (field As String, value As String, type As String)
Sets the specified field of the current target to the value provided. Protected and
private fields may be accessed if allowed by any security manager which may be present.
Target must be an instance of a Class, not a Class object.
SetField2 (field As String, value As Object)
Sets the specified field of the current target to the value provided. Protected and
private fields may be accessed if allowed by any security manager which may be present.
Target must be an instance of a Class, not a Class object.
SetField3 (fieldinfo As Field, value As String, type As String)
Sets the specified field of the current target to the value provided.
Target must be an instance of a Class, not a Class object.
SetField4 (fieldinfo As Field, value As Object)
Sets the specified field of the current target to the value provided.
Target must be an instance of a Class, not a Class object.
SetPublicField (field As String, value As String, type As String)
Sets the specified field of the current target to the value provided.
This is more efficient than SetField but can only access public fields.
Target must be an instance of a Class, not a Class object.
SetPublicField2 (field As String, value As Object)
Sets the specified field of the current target to the value provided.
This is more efficient than SetField but can only access public fields.
Target must be an instance of a Class, not a Class object.
SetStaticField (classname As String, field As String, value As String, type As String)
Sets the specified static field of the specified class to the value provided.
Protected and private fields may be accessed if allowed by any security manager
which may be present.
Static fields may also be accessed with SetField and an instance of the class.
SetStaticField2 (classname As String, field As String, value As Object)
Sets the specified static field of the specified class to the value provided.
Protected and private fields may be accessed if allowed by any security manager
which may be present.
Static fields may also be accessed with SetField and an instance of the class.
Target As Object
This field holds the object that is being reflected upon. The target object is
assigned to this field where it can then be manipulated as required.
TargetRank As Int()
Returns an int array whose length is the number of dimensions of the array and whose contents
are the length of the first element of each array dimension.
A zero length integer array is returned if Target is not an array.
ToString As String
Returns the result of running the "toString()" method of the current object.
TypeName As String [read only]
Returns the name of the class of the current object.
Version As Double [read only]
Returns the version number of the library.
Top