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;"
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.
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.
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.
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.
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 (typeAsString) AsObject
Creates and returns a new object of the specified type using the default constructor.
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.
DebugModeAsBoolean
Returns a boolean value, True if the program is running in Debug mode otherwise False.
GetArray (indeces() AsInt) AsObject
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 (componentAsString) AsClass
Returns the Java Class for the specified B4A module. To use this requires a knowledge of the structure of a B4J application.
GetBAAsBA
Returns the BA of the calling module To use this requires a knowledge of the structure of a B4J application.
GetField (fieldAsString) AsObject
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 (fieldinfoAsField) AsObject
Returns the value of the field of the current target. Target must be an instance of a Class, not a Class object.
GetFieldInfo (fieldAsString) AsField
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.
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 (moduleAsString) AsBA
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 (moduleAsString) AsObject
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 (fieldAsString) AsObject
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.
GetStacktraceAsStackTraceElement()
Gets an array of StacktraceElements which represents the present stack.
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.
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 (methodAsString) AsObject
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.
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.
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.
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.
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.
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.
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() AsInt, valueAsObject)
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.
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 (fieldAsString, valueAsObject)
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.
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 (fieldAsString, valueAsObject)
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.
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.
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.
TargetAsObject
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.
TargetRankAsInt()
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.
ToStringAsString
Returns the result of running the "toString()" method of the current object.
TypeNameAsString [read only]
Returns the name of the class of the current object.