Question

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

Thanks, Bilal

Was it helpful?

Solution

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();

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top