Re: Extending/replacing a class without a public constructor

From: Dale King (DaleKing_at_newsrvr.tce.com)
Date: 06/14/04


Date: Mon, 14 Jun 2004 07:54:46 GMT-5

Hello, Hugh Beyer !
You wrote:

> "John C. Bollinger" <jobollin@indiana.edu> wrote in
news:caafc5$fvf$1
> @hood.uits.indiana.edu:
>
> > Hugh Beyer wrote:
> >
> >> I'm having a really frustrating problem and I hope you guys
can give me
> >> some help.
> >>
> >> I'm trying to write a test suite for a graphics-heavy app
that uses
> >> MIDP/J2ME. These apps use the Graphics class to do their
screen
> updates.
> >>
> >> What I want to do is subclass the Graphics class with my own
class that
> >> captures all calls and writes to a log that my tests can
later examine.
> >> Unfortunately, all the constructors of the Graphics class
are private.
> The
> >
> > You are mistaken. Both Graphics and Graphics2D (the actual
class you
> > are most likely to run into) have protected no-arg
constructors, at
> > least in Java 1.4.
>
> I'm dealing with javax.microedition.lcdui.Graphics. It hasn't
got a no-arg
> constructor but bingo, does have an undocumented 2-arg
constructor. I'm
> going to give that a try with width, height as the args.
>
> I've only just gotten comfortable with the idea that my source
tree can
> add to packages I don't own. If I can get this to work, I can
subclass
> Graphics and just do logging on the calls I care about.
>
> >
> >> system passes in a Graphics class when it's time to update
the screen,
> and
> >> you can also create a graphics class through a static
factory method
> >> (Image.getGraphics()). But either way, I can't get in and
capture the
> >> method calls to the resulting Graphics object.
> >>
> >> Is there any way at all to do this, or am I SOL? Can anyone
think of
> >> another way to accomplish what I want?
> >
> > You can subclass either class with an implementation that
delegates to a
> > System-created Graphics instance, with logging wrapped around
the method
> > invocations. I suppose that's what you had in mind to begin
with. If
> > aspect-oriented programming were deeply enough supported by
Java then
> > this would be a perfect case for it, but as it is, you
probably couldn't
> > catch the system's manipulations of a Graphics -- only your
own code's.
>
> I see how I can do this once I can get to the constructor, but
if there
> were no available constructor would I still be SOL? I've got to
subclass
> the Graphics class or my production code won't be able to write
to it; and
> any subclass is required to call super() in its constructors,
isn't it?
> And it has to be a subclass or my paint routines won't accept
it as a
> legitimate Graphics to write to.

There is a more drastic route you can take, but it requires more
work on your part. Basically you can create your own wrapper API
for it. It would look something like this:
- Create an interface which contains the method signatures for
all of the methods in Graphics.
- Create an implementation of that interface that contains a
reference to an instance of the real lcdui.Graphics type that is
passed into the constructor. Its implementations of the interface
methods simply calls the corresponding methods on the
lcdui.Graphics instance.
- You can then overrid that class to add any functionality you
wanted to perform in the subclass you were trying to create.
- You rewrite mostof your application to work with the interface
instead of the lcdui.Graphics class. Since they have the same
methods that mostly involves changing a few variable types.
- For methods like paint that take the actual lcdui.Graphics
instance, you wrap that in the class you created above that adds
your additional functionlity and that is what the rest of your
code uses.

This is certainly a lot of work, but it is doable.

-- 
 Dale King
 My Blog: http://daleking.homedns.org/Blog


Relevant Pages

  • Re: Virtual functions, calling inherited class. Quicky.
    ... The "SubClass(): BaseClass" syntax is used only for constructors. ... way to indicate which base class constructor to use before entering the ... > I want to call the BaseResult class to let it write shared data before I ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Virtual functions, calling inherited class. Quicky.
    ... The "SubClass(): BaseClass" syntax is used only for constructors. ... way to indicate which base class constructor to use before entering the ... > I want to call the BaseResult class to let it write shared data before I ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Base class - Derived class interaction question
    ... Since constructors don't inherit it follows that if you want do this ... at all you /cannot/ use an explicit constructor in the shared code. ... creation of the new object into something that the subclass can override. ... Derived plus(Base operand) ...
    (comp.lang.java.programmer)
  • Re: Do I *have* to use OOP to use modules?
    ... }} And, almost more importantly, can subclass any other class, inside-out ... If Foo is implemented as a hashref, ... Multiple inheritance is still going to be very hard, ... any configuration inside the constructor. ...
    (comp.lang.perl.misc)
  • Re: Named arguments and inheritance
    ... OK, it seems to work, but I don't see how it works as the constructor ... FormProperty = function(arg) { ... subclass within the FormProperty constructor. ...
    (comp.lang.javascript)

Loading