Re: Extending/replacing a class without a public constructor

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 06/10/04


Date: Thu, 10 Jun 2004 15:12:53 -0500

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.

> 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.

John Bollinger
jobollin@indiana.edu



Relevant Pages

  • Extending/replacing a class without a public constructor
    ... I'm trying to write a test suite for a graphics-heavy app that uses ... These apps use the Graphics class to do their screen updates. ...
    (comp.lang.java.programmer)
  • Extending/replacing a class without a public constructor
    ... I'm trying to write a test suite for a graphics-heavy app that uses ... These apps use the Graphics class to do their screen updates. ... static factory method ). ...
    (comp.lang.java.developer)
  • Re: Problem with Graphics Class...
    ... Please post the code that causes the exception. ... Graphics Transform stack and Flood-Filling ... "Sujith Manuel" wrote in message ... > Iam also using Disposemethod of Graphics Class. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Graphics.ScaleTransform vs. Graphics.Transform.Scale
    ... Actually there is a difference - asking for the transform of the graphics ... object will create a copy of the transform used by the graphics object. ... on the graphics class itself. ...
    (microsoft.public.dotnet.framework.drawing)