Detecting overridden methods..

Discussion in 'Java' started by Andrew Thompson, Jun 2, 2009.

  1. I was just writing a multi-line comment for some code for
    appleteer, about a little hack that is intended to overcome
    a problem with the current version.

    /* This little hack allows us to overcome the 'applet overwrites
    everything' (e.g. scroll bars, menus) problem seen in early
    versions of the program, but only for Swing applets that 'do
    not override paint'. That latter part is hard to detect, but we
    take the simple approach of counting the numbers of
    components in the root and content panes. If these are
    more than for a blank applet (i.e. 'components have been
    added'), it is presumed the applet does not override paint. */

    The hack then (presuming no override of paint()) adds the
    JApplet's rootpane to the AppletLoaderContainer, instead of
    the applet itself.

    Are there better ways to detect if an arbitrary Swing applet
    overrides paint(Graphics)?

    (Or any method, of any class, for that matter.)

    --
    Andrew T.
    pscode.org
     
    Andrew Thompson, Jun 2, 2009
    #1
    1. Advertising

  2. Andrew Thompson

    Mark Space Guest

    Andrew Thompson wrote:

    > Are there better ways to detect if an arbitrary Swing applet
    > overrides paint(Graphics)?
    >
    > (Or any method, of any class, for that matter.)


    Wouldn't you be able to get this with reflection and
    getDeclaredMethod()? I didn't double check but I think that's the intent.
     
    Mark Space, Jun 2, 2009
    #2
    1. Advertising

  3. On Jun 2, 3:50 pm, Mark Space <> wrote:
    > Andrew Thompson wrote:
    > > Are there better ways to detect if an arbitrary Swing applet
    > > overrides paint(Graphics)?

    >
    > > (Or any method, of any class, for that matter.)

    >
    > Wouldn't you be able to get this with reflection and
    > getDeclaredMethod()?


    I mistakenly thought that would not work, since
    both the applet and it's subclass should have access
    to the Method. But my initial assumption was wrong,
    as this code supports. Comment out the overridden
    method to see an NSME stacktrace.

    <sscce>
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    import java.lang.reflect.*;

    public class TestAppletOverride extends JApplet {

    public void start() {
    try {
    Method testAppletPaint =
    this.
    getClass().
    getDeclaredMethod("paint", Graphics.class);

    JOptionPane.showMessageDialog(
    this,
    testAppletPaint.getDeclaringClass());
    } catch(NoSuchMethodException nsme) {
    nsme.printStackTrace();
    }
    }

    @Override
    public void paint(Graphics g) {
    g.setColor(Color.YELLOW);
    g.fillRect(0,0,getWidth(),getHeight());
    }
    }
    </sscce>

    That is one nasty hack, straight out the door. :)

    Thanks!

    --
    Andrew T.
    pscode.org
     
    Andrew Thompson, Jun 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco
    Replies:
    7
    Views:
    9,173
    Alex Hunsley
    Sep 13, 2004
  2. Replies:
    15
    Views:
    13,872
    Chris Uppal
    Sep 24, 2004
  3. Milan Gornik

    Exceptions and overridden methods

    Milan Gornik, Mar 29, 2005, in forum: C++
    Replies:
    1
    Views:
    2,924
    lilburne
    Mar 29, 2005
  4. Ravi
    Replies:
    10
    Views:
    669
    Chris Uppal
    Mar 26, 2007
  5. Kira Yamato
    Replies:
    13
    Views:
    1,256
    proudbug
    Jan 3, 2008
Loading...

Share This Page