Extending java classes and overriding methods called from constructor.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Extending java classes and overriding methods called from constructor.

Derek Lewis
I've been trying to use beanshell for an application of mine, and I've run
across something odd.

If I have a class in Java, say:

class BaseClass
{
        public void doSomething()
        {
                // do something
        }
}

And I extend it in a beanshell script:

class SomeClass extends BaseClass
{
        public void doSomething()
        {
                // do something else
        }
}

Then that works.  But as soon as I add a constructor to BaseClass that calls
doSomething() :

public BaseClass()
{
        super();
        doSomething();
}

I get an exception from beanshell whenever I try to instanciate SomeClass.
If I remove the doSomething method from SomeClass, so it's not overriding the
native java method, then the exception goes away, but, of course, then the
code's not doing what I want it to. :)

Here's the exception I get:

java.lang.NullPointerException
at NewClass2.doSomething(BeanShell Generated via ASM (www.objectweb.org))
at com.lewisd.woim.SimpleBaseClass.<init>(SimpleBaseClass.java:13)
at NewClass2.<init>(BeanShell Generated via ASM (www.objectweb.org))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at bsh.Reflect.constructObject(Unknown Source)
at bsh.BSHAllocationExpression.constructObject(Unknown Source)
at bsh.BSHAllocationExpression.objectAllocation(Unknown Source)
at bsh.BSHAllocationExpression.eval(Unknown Source)
at bsh.BSHPrimaryExpression.eval(Unknown Source)
at bsh.BSHPrimaryExpression.eval(Unknown Source)
at bsh.BSHAssignment.eval(Unknown Source)
at bsh.Interpreter.eval(Unknown Source)
at bsh.Interpreter.source(Unknown Source)
at bsh.Interpreter.source(Unknown Source)
at com.lewisd.woim.BshExtendTest.testExtend(BshExtendTest.java:20)


In this trace, BshExtendTest is my unit test, and SimpleBaseClass.java:13 is the
line in the constructor where doSomething is called.

Any idea what's going on?  Is this possible?

--
Derek Lewis

===================================================================
      Java Web-Application Developer
 
      Email    : [hidden email]
      Cellular : 778.898.5825
      Website  : http://www.lewisd.com

 "If you've got a 5000-line JSP page that has "all in one" support
 for three input forms and four follow-up screens, all controlled
 by "if" statements in scriptlets, well ... please don't show it
 to me :-).  Its almost dinner time, and I don't want to lose my
 appetite :-)."
      - Craig R. McClanahan


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users