Reflection API in Progress 4GL

Hello! In our application we need some classes to be created only once. I will implement these classes as singletons (with the help of the static keyword in 10.1.C)


METHOD PUBLIC STATIC CLASS CLSBETaskUserBase getInstance():
IF NOT VALID-OBJECT (clsBEModel) THEN
clsBEModel = NEW CLSBETaskUserBase().
RETURN CAST (clsBEModel, CLSBETaskUserBase).
END METHOD.

As I am not willing to implement the getInstance() method in every businessentity, I want to place a getInstance() in the superclass with an additionaly string-parameter, which will indicate the type of class to create, e.g.


METHOD PUBLIC STATIC CLASS CLSBusinessEntity getInstance (INPUT arg AS CHARACTER):
DEFINE VARIABLE cls AS Progress.Lang.Class NO-UNDO.
cls = PROGRESS.Lang.Class:GetClass(arg).
clsBEModel = CAST(cls, PROGRESS.lang.class:getclass(arg)).
RETURN clsBEModel.
END METHOD.

The call was thought to be like this:

DEFINE VARIABLE clsBE AS CLASS CLSBETaskPerson.
clsBE = CLSBusinessEntity:getInstance(
"CLSBETaskPerson").
.
.
.

*/

FYI:
CLSBusinessEntitiy is the superclass of CLSBETaskUserBase.

This code compiles fine, but at runtime a exception resp. an error is raised telling me about an illegal cast-operation.

How can I created an object dynamically at runtime like I can do this in Java? Does anybody know? I think in my code...there must be an error.

First I tried to use an interface to force every businessentity to override the method getInstance(), but I cannot place a static method in an interface.

The second attempt was to place the getInstance() in the superclass - the result is an illegal cast (see codesniplet above). Of course it can be that the second code is wrong - if anyone can tell me how I can make it - thank you very much.

Greetings
Harry


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Reflection API in Progress 4GL

Harry,

Take a look at OpenEdge Getting Started : Object-oriented Programming
page 4-58 "Common use case for STATIC members" where you'll find an
example for implementation of singleton classes.
For the other question about the instantiating object dynamically,
take a look at the DYNAMIC-NEW statement.

regards,
Sasha

P.S. This should go to OOABL Forum.


Re: Reflection API in Progress 4GL

Sasha,

Have you tried running the singleton example on page 4-59 of OpenEdge Getting Started : Object-oriented Programming (10.1C) ??

I ran the example from OpenEdge Architect 10.1C. It doesn't seem to work... the SingletonProp instance is somehow being deleted in between invocations.

To quote the document:
Note that if you re-run this procedure, both MESSAGE statements display "Max temp is 98.6" because rSingle:MaxTemperature is already initialized from the first execution and retains its previous value.

On the First Invocation i get MESSAGE statements:

Max temp is 0

Max temp is 98.6

Every Invocation thereafter I get MESSAGE statements:

Invalid Widget-Handle. Not initialised or points to a deleted widget. (3135)

Max temp is

Invalid Widget-Handle. Not initialised or points to a deleted widget. (3135)

I'm not using any Launch Configurations nor am I restarting the runtime session in between invocations...
Is anyone else having the same problem?


Re: Reflection API in Progress 4GL

Hi,

Yes I did and the sample code works for me.
I am getting the expected results - tested with 10.1C.
I've tested in OE Architect and also haven't used any Launch Configuration.
I'd suggest that you recompile the code and eventually run the
sample through the debugger - that will tell you (and show you)
whether static member gets initiated and set properly.

Sasha


OOABL Singleton

Thanks for replying.

I tried the using the debugger in OEA, but the problem with the debugger is that it restarts the OpenEdge Runtime (session) after running a procedure (.p) ... So all static instances/variables are deleted.

It's odd, I've tried it on a couple of PC's around the office, and also running it from the Procedure Editor, all give me the same result.

Just to confirm, when you RERUN the .p from the example you see these two MESSAGE boxes?

Max temp is 98.6

Max temp is 98.6

My session startup parameters are:
-ide -debugalert -inp 16000 -tok 4000 -rereadnolock -s 200 -mmax 65534

Cheers,
Cameron