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

Reflection

Written by Andrew Graham

This library contains a Reflector object that allows access to methods and fields
of Android objects that are not exposed to the Basic4Android 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.

GetActivity and GetActivityBA in this library only work for Basic4android 2.00 and higher.
Use version 2.1 of this library for earlier versions of Basic4android.

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.

'Full' (it is often lacking useful explanatory details) technical documentation is available
on the Google Android website at http://developer.android.com.

Java is case sensitive and as used for Android does not support properties. Properties as
implemented in Basic4Android 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 Basic4Android as
normal methods. For example the Top property of a View is actually implemented in Java code as
two methods, int getLeft() and void setLeft(int left). The Basic4Android compiler makes them
look like a single property to the programmer.

Events:

Click(ViewTag As Object)
LongClick(ViewTag As Object) As Boolean
Focus(ViewTag As Object, Focus As Boolean)
Key(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
Touch(ViewTag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

Members:


  CreateObject (type As String) As Object

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

  GetActivity As android.app.Activity

  GetActivityBA As BA

  GetArray (indeces() As Int) As Object

  GetB4AClass (component As String) As Class

  GetContext As android.content.Context

  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

  GetMostCurrent (component As String) As Object

  GetProcessBA (component As String) As BA

  GetProxy (interfacenames() As String, b4asubname As String) As Proxy

  GetPublicField (field As String) As Object

  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]

  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)

  SetOnClickListener (sub As String)

  SetOnCreateContextMenuListener (sub As String)

  SetOnFocusListener (sub As String)

  SetOnKeyListener (sub As String)

  SetOnLongClickListener (sub As String)

  SetOnTouchListener (sub As String)

  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:

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.
GetActivity As android.app.Activity
Returns the current activity if any.
To avoid memory leaks this should not be used by a Reflector that is a Process object.
To use this requires a knowledge of the structure of a Basic4android application.
GetActivityBA As BA
Returns the Activity BA of the current activity.
To avoid memory leaks this should not be used by a Reflector that is a Process object.
To use this requires a knowledge of the structure of a Basic4android application.
GetArray (indeces() As Int) As Object
Returns the Object at the position(s) in an array specified by the contents of indeces.
indeces must be an integer array of the same rank as the Target array or an error will occur.
GetB4AClass (component As String) As Class
Returns the Java Class for the specified B4A Activity, Service or Code module.
To use this requires a knowledge of the structure of a Basic4android application.
GetContext As android.content.Context
Returns the Context of the Process to which the Reflection object belongs.
This is the Application object returned from Activity.getApplicationContext().
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.
GetMostCurrent (component As String) As Object
Returns the current instance for the specified B4A Activity or Service module.
This might return null if the Activity or Service is not instantiated.
Note that Code modules do not have a current instance.
To use this requires a knowledge of the structure of a Basic4android application.
GetProcessBA (component As String) As BA
Returns the processBA instance for the specified B4A Activity or Service module.
To use this requires a knowledge of the structure of a Basic4android application.
GetProxy (interfacenames() As String, b4asubname As String) As Proxy
In Java you can generate an interface at runtime and have it run a pre-compiled method.
Many events in Android are handled by an interface that typically has an "onXxxxx" method
that is called with some parameters relevant to the event. The interface is typically
set on an object using that objects "setOnXxxxxListener" method.

This GetProxy method dynamically creates a proxy instance that implements one or more
specified interfaces and which contains the code to call a specified Basic4android Sub
when any of the interface methods are called.

Typically this instance will implement one or more listeners and will then be assigned
to an object instance using RunMethod4 and its setOnXxxxxListener method.

When a method of one of the specified interfaces is called the proxy will call the specified
Basic4android Sub passing the method name as a string and any arguments in an object array.

Note that interfaces declared as internal to a class will need a "$" instead of a "." as their
final separator and all interfaces need to be fully qualified.
e.g android.view.View$OnTouchListener

The Basic4android Sub called must have the signature
Sub WhateverName(method As String, anyargs() As Object) As Object
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.
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 prsent value of Target is Null.
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 indeces to the spcified value.
indeces 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 indeces to the specified value.
indeces 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.
SetOnClickListener (sub As String)
Target must be a View of some sort.
In most cases Basic4Android will have already exposed this as a Click event.
Sets the OnClickListener of the view to a Sub that must have a signature of
Sub Whatever(viewtag As Object).
SetOnCreateContextMenuListener (sub As String)
Target must be a View of some sort.
This is included for completeness of all the Listeners that class View supports.
Sets the OnCreateContextMenuListener of the view to a Sub that must have a signature of
Sub Whatever(viewtag As Object, menu As Object, menuinfo As Object)
SetOnFocusListener (sub As String)
Target must be a View of some sort.
Sets the onFocusChangeListener of the view to a Sub that must have a signature of
Sub Whatever(viewtag As Object, focus As Boolean).

You should make sure not to call DoEvents, Msgbox or any modal Dialog inside this
event as it will fail in Android 4.0.3 and above.

It may also fail if Debug is paused in the event in Android 4.0.3 and above.
SetOnKeyListener (sub As String)
Target must be a View of some sort.
Sets the onKeyListener of the view to a Sub that must have a signature of
Sub Whatever(viewtag As Object, keycode As Int, keyevent As Object) As Boolean.

This Sub must return True if it wants to consume the event or False otherwise.
SetOnLongClickListener (sub As String)
Target must be a View of some sort.
In most cases Basic4Android will have already exposed this as a LongClick event.
Sets the OnLongClickListener of the view to a Sub that must have a signature of
Sub Whatever(viewtag As Object) As Boolean.

This Sub must return True if it wants to consume the event or False otherwise.
SetOnTouchListener (sub As String)
Target must be a View of some sort.
Sets the onTouchListener of the view to a Sub that must have a signature of
Sub Whatever (viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean.

This Sub must return True if it wants to consume the event or False otherwise.

You should make sure not to call DoEvents, Msgbox or any modal Dialog inside this
event as it will fail in Android 4.0.3 and above. If you want to do so, put the code in another sub
and call this sub with CallSubDelayed.

It may also fail if Debug is paused in the event in Android 4.0.3 and above.
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