Introspection
From Real Software Documentation
Gets information about a program’s structure at runtime. Classes and methods in the Introspection module have Public rather than Global scope, so references to them must include the module name.
Classes
Each class is described in its own section.
| Name | Super Class | Description |
|---|---|---|
| AttributeInfo | MemberInfo | Provides information about an item’s attributes. Attributes are compile-time properties that are created via the Attributes Editor in the IDE. Each attribute consists of its identifier (name) and optionally a value. |
| ConstructorInfo | MemberInfo | Provides information about a datatype’s constructors. Use the GetParameters function of this class to get information about the parameters of the constructors. |
| MemberInfo | The super class for AttributeInfo, ConstructorInfo, MethodInfo, PropertyInfo, and TypeInfo. Contains a property that contains the datatype's name. | |
| MethodInfo | MemberInfo | Provides information on the methods in the datatype. Use the GetParameters function of this class to get information on the parameters of each method in the class. |
| ParameterInfo | Provides information on the parameters of methods belonging to the class. Use the MethodInfo.GetParameters function to obtain a ParameterInfo array to get the parameter info. | |
| PropertyInfo | MemberInfo | Provides information on the properties of the datatype. |
| TypeInfo | MemberInfo | The root of the introspection system and the primary way to access program metadata. It is an abstract class that describes all the attributes or members a datatype might have. |
| Methods | ||
|
Examples
The following example in the Action event of a PushButton in a window gets information about the class instance and displays the name of its class. See the examples for the AttributeInfo, ConstructorInfo, MemberInfo, MethodInfo, PropertyInfo, TypeInfo, and ParameterInfo for examples of those classes.
Dim t as Introspection.TypeInfo
t=Introspection.GetType(tcp)
MsgBox "My class name is " + t.Name
The following example (download the project file from ErrorReporting.rbp.zip) shows how Introspection can massively simplify code, reduce the size of an app, and obviate the need to manually update the code when new exceptions are added to the REALbasic language:
To enable error reporting one can:
• Add a window win_ReportError
• Add a module Module_ErrorHandling with function: HandleException(error as runtimeexception, source as string) As boolean
Normally one could just read the stack trace for error reporting, however as the stack on Macs does not have the required information on where the error occurred you need to put in a bit more work to get this info.
In each method or event call the HandleException funcion with:
if not Module_ErrorHandling.HandleException(err,"window1.EventHandlers.Open") then // <- SPECIFY LOCATION
// calls the HandleException method and passes it the error type
// and the location of where the error happened
// if that doesn't work then
Raise err
end if
Now instead of writing this code:
dim Message as string
If error IsA FunctionNotFoundException then
Message = "IllegalCastException in " + source
ElseIf error IsA IllegalCastException then
Message = "IllegalCastException in " + source
ElseIf error IsA IllegalLockingException then
Message = "IllegalLockingException in " + source
ElseIf error isA InvalidParentException Then
Message = "Unhandled InvalidParentException error in "+ source
ElseIf error IsA KeyChainException then
Message = "KeyChainException in " + source
ElseIf error IsA KeyNotFoundException then
Message = "KeyNotFoundException in " + source
ElseIf error IsA NilObjectException then
Message = "NilObjectException in " + source
ElseIf error isA NoOpenTransportException Then
Message = "Unhandled NoOpenTransportException error in" + source
ElseIf error isA OLEException Then
Message = "Unhandled OLEException error in " + source
ElseIf error IsA OutOfBoundsException then
Message = "OutOfBoundsException in " + source
ElseIf error IsA OutOfMemoryException then
Message = "OutOfMemoryException in " + source
ElseIf error isA RbScriptAlreadyRunningException Then
Message = "Unhandled RbScriptAlreadyRunningException error in" + source
ElseIf error isA RbScriptException Then
Message = "Unhandled RbScriptException error in " + source
ElseIf error IsA RegExException then
Message = "RegExException in " + source
ElseIf error IsA RegExSearchPatternException then
Message = "RegExSearchPatternException in " + source
ElseIf error IsA RegistryAccessErrorException then
Message = "RegExSearchPatternException in " + source
ElseIf error isA RuntimeException Then
Message = "Unhandled RuntimeException error in " + source
ElseIf error isA RegistryAccessErrorException Then
Message = "Unhandled RegistryAccessErrorException error in " + source
ElseIf error isA ServiceNotAvailableException Then
Message = "Unhandled ServiceNotAvailableException error in " + source
ElseIf error IsA ShellNotAvailableException then
Message = "ShellNotAvailableException in " + source
ElseIf error IsA ShellNotRunningException then
Message = "ShellNotRunningException in " + source
ElseIf error IsA SpotlightException then
Message = "ShellNotRunningException in " + source
ElseIf error IsA StackOverflowException then
Message = "StackOverflowException in " + source
ElseIf error IsA ThreadAlreadyRunningException then
Message = "ThreadAlreadyRunningException in " + source
ElseIf error IsA TypeMismatchException then
Message = "TypeMismatchException in " + source
ElseIf error IsA UnsupportedFormatException then
Message = "UnsupportedFormatException in " + source
Else
return False
end if
dim win_ReportThisError as new win_ReportError(Message)
win_ReportThisError.Show
return True
Exception
MsgBox "HandleException Error"
Return True
End Function
You can simply use Introspection and write:
// can use Introspection instead of if…elseif statement
dim Message as string = Introspection.GetType(error).FullName + " in " + source
dim win_ReportThisError as new win_ReportError(Message)
win_ReportThisError.Show
return True
Exception
MsgBox "HandleException Error"
Return True
End Function
This not only reduces the code but also the app size — a simple demo app has:
- with the if…elseif statement: 15.7 MB
- comment out the two RBscript exceptions 5.2 MB
- use the Introspection method 4.9 MB (still works with RBscript exceptions)
Another advantage is that whenever new Exceptions are added to the REALbasic language, you don't need to add them manually to the ElseIf construct but they will be automatically dealt with as well through the generic introspection system.
See Also
ConstructorInfo, AttributeInfo, MemberInfo, MethodInfo, ParameterInfo, PropertyInfo, TypeInfo classes; GetTypeInfo function.
