开发者

beanshell inner class

i wanted to use my java code as beanshell script but beanshell throws Exception saying class not found in namespace. Isn't there inner class in beanshell or does it have any other usage?

my script looks like here:

.......
.......
java code
.......
.......
MyClass m = new MyClass(); //Error here: MyClass not fount in namespace



class MyClass {

}

i use the inner class in script, which i declare in the scr开发者_如何学Cipt.

Thanks, Bilal


Maybe a silly answer here but could it be that the MyClass definition needs to be above its usage in the file? Doesn't bean shell process scripts linearly?

A quick look in the docs doesn't spell this out but tests of the following script certainly works fine for me:

class MyClass {
}
MyClass m = new MyClass();


Class definition is not supported by BeanShell.

You can use BeanShell inner class syntax to implement an interface:

    x = new MyInterface() {
        overriddenMethod() {
          // ....
        }
    }

v = x.overriddenMethod(); 

Or

    overriddenMethod() {
       //.....
    }

    // 'this' is a object of whatever Foo expects 
    //
    new Foo(this);

In your case I think you are better off going with the scripted object approach:

myClass() {

// methods ...

return this;

};

m = myClass(); // new instance


Additional information: the anonymous inner class as argument can not use, so you need to assign your implementation to a variable. (In JMeter)

Not works:

object.setContext(new SomeInterface(){
  //implement methods
});

Works:

SomeInterface ctx = new SomeInterface(){
    //implement methods
});
object.setContext(ctx);

Hope it will help out someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜