Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Understanding classes in the Progress.Reflect package
 

Understanding classes in the Progress.Reflect package

A number of the public methods of Progress.Lang.Class use classes in the Progress.Reflect package both as parameters and as a way of returning information. The "Get" reflection methods return instances of classes to describe class members. For example, if you call GetMethod( ) to get information about a particular method of a class, GetMethod( ) returns an instance of Progress.Reflect.Method representing the method. The following table describe some of the public properties of Progress.Reflect.Method.
Table 8. Progress.Reflect.Method public properties
Property
Description
AccessMode AS Progress.Reflect.AccessMode
Returns an instance of the built-in enumeration Progress.Reflect.AccessMode indicating the access mode (e.g., Public, Protected, Private) of the method.
IsOverride AS LOGICAL
Returns TRUE if the method is an override.
Name AS CHARACTER
Returns the method name.
OriginatingClass AS Progress.Lang.Class
Returns an instance of Progress.Lang.Class representing the class the method was retrieved from.
As mentioned in the table, Progress.Reflect.AccessMode is a built-in enumeration type. For more information about working with ABL enumeration types, see the ENUM statement entry in OpenEdge Development: ABL Reference.
Progress.Reflect.Method also provides methods, including an Invoke( ) method (with overrides for both static and instance methods) that allows you to call the method that the Progress.Reflect.Method instance represents. This is the syntax for the instance method override:
Invoke ( INPUT instance AS Progress.Lang.Object,
INPUT params AS Progress.Lang.ParameterList )
This override takes an instance of the class that implements the method and a parameter list to hold the parameters that are passed to the method.
The following code uses reflection methods to add two pieces of data to a JSON array and then write that array to a file, following these steps:
1. GetClass( ) is invoked statically to create plcjArray, a Progress.Lang.Class instance representing a Progress.Json.ObjectModel.JsonArray object. The GetConstructor( ) method of Progress.Lang.Class is invoked to create jArray, an instance of JsonArray.
2. GetMethod( ) is invoked on plcjArray to create myMethod, an instance of Progress.Reflect.Method, which in this case represents the Add( ) method of JsonArray. Then Invoke( ) is called on myMethod, which invokes Add( ) to add the character data to jArray.
3. After getting the method again using a revised parameter list, Invoke( ) is called on myMethod to add the date to jArray.
4. GetMethod( ) is invoked again on plcjArray, this time to create an instance of Progress.Reflect.Method that represents the WriteFile( ) method of JsonArray. Invoke( ) is called on myMethod to write jArray to file.
/* Use GetMethod() and Invoke() to add data to a JsonArray */
USING Progress.Json.ObjectModel.* FROM PROPATH.
USING Progress.Reflect.*.

DEFINE VARIABLE plcjArray AS Progress.Lang.Class NO-UNDO.
DEFINE VARIABLE myConstructor AS Constructor NO-UNDO.
DEFINE VARIABLE myMethod AS Method NO-UNDO.
DEFINE VARIABLE jArray AS CLASS JsonArray.
DEFINE VARIABLE myPL AS Progress.Lang.Parameterlist NO-UNDO.

DEFINE VARIABLE char1 AS CHARACTER NO-UNDO INIT "Mr. Smith".
DEFINE VARIABLE date1 AS DATE INIT 11/16/2010.

/* 1. Create a JsonArray object. */
plcjArray =
Progress.Lang.Class:GetClass("Progress.Json.ObjectModel.JsonArray").
myPL = NEW Progress.Lang.Parameterlist(0).
myConstructor = plcjArray:GetConstructor(myPL).
jArray = CAST(myConstructor:Invoke(myPL), Progress.Json.ObjectModel.JsonArray).

/* 2. Get and invoke Add( ) to add the name. */
myPL = NEW Progress.Lang.Parameterlist(1).
myPL:SetParameter(1, "CHARACTER", "INPUT", char1).
myMethod = plcjArray:GetMethod("Add", myPL).
myMethod:Invoke(jArray, myPL).

/* 3. Get and invoke Add( ) again to add the date */
myPL:SetParameter(1, "DATE", "INPUT", date1).
myMethod = plcjArray:GetMethod("Add", myPL).
myMethod:Invoke(jArray, myPL).

/* 4. Get and invoke WriteFile( ). */
myPL = NEW Progress.Lang.Parameterlist(2).
myPL:SetParameter(1, "CHARACTER", "INPUT", "filename.json").
myPL:SetParameter(2, "LOGICAL", "INPUT", TRUE).
myMethod = plcjArray:GetMethod("WriteFile", myPL).
myMethod:Invoke(jArray, myPL).
For full descriptions of the properties and methods of Progress.Reflect.Constructor, Progress.Reflect.Method, Progress.Reflect.Event, Progress.Reflect.Property, and Progress.Reflect.Variable, see their entries in OpenEdge Development: ABL Reference.