The BSF (Bean Scripting Framework) was originally a project of the IBM Alpha Workgroup and later contributed to the APACHE,BSF to enable Java to better integrate with various popular scripting languages, enabling interactivity between different languages. BeanShell is a scripting language that fully conforms to Java syntax.
Run the following code to download the relevant jar package (http://jakarta.apache.org/bsf/,http://www.beanshell.org/)
PS: Sina Blog regards "eval" as a dangerous angle, so replace all "eval" methods in the following example with "Eval_r" .
Test class
Public class Bsfscript extends scriptable {
Public Static void Main (string[] args) {
Try {
Return to True/false (3 > 2)
Object flag = bsfengine. Eval_r (defaultengine, 0, 0, "(3 > 2)");
if (Boolean. valueOf (Flag.tostring ())) {
System. out. println ("Do something ...");
}
Authorbean bean = new Authorbean ();
Variablemap. Put ("Author", "lijingcheng1984");
The Variablemap and Authorbean class names are pre-registered to the BSF manager when they are subsequently executed.
So author and Authorbean can be used as variables in the corner book.
String expr = "if (authorbean.getblogaddress (). LastIndexOf (author)! =-1)"
+ "{return authorbean.getblogaddress ();}";
System. out. println (Bean.getscriptable (). Eval_r (expr));
} catch (Exception e) {
System. out. println (E.getmessage ());
}
}
}
This interface is used to identify inherited classes that support angular localization
Public Interface Iscriptabletoken {
scriptable getscriptable ();
}
Java Bean class with angular localization
Public class Authorbean implements Iscriptabletoken {
Public String getblogaddress () {
return "http://blog.sina.com/lijingcheng1984";
}
@Override
Public scriptable getscriptable () {
return New Scriptable (this);
}
}
import java.util.Hashtable;
import Java.util.Map;
import org.apache.bsf.BSFEngine;
import org.apache.bsf.BSFException;
import Org.apache.bsf.BSFManager;
Angular-oriented functional class
Public class scriptable {
Private Static Bsfmanager Mgr;
protected Static Bsfengine bsfengine;
protected Static Map<string, object> variablemap;
Private Iscriptabletoken Scriptableobject;
protected Static Final String defaultengine = "BeanShell";
Public Scriptable () {
}
Public Scriptable (Iscriptabletoken scriptableobject) {
this. Scriptableobject = Scriptableobject;
}
Static {
Try {
mgr = new bsfmanager ();
bsfengine = Mgr. Loadscriptingengine (defaultengine);
Variablemap = new hashtable<string, object> ();
} catch (Bsfexception e) {
E.printstacktrace ();
}
}
Registers and binds the object, which can be accessed later through the key value
Private void putvariable (String key, Object value) throws bsfexception {
for (final map.entry<string, object> Entry: variablemap. EntrySet ()) {
Mgr. Declarebean (Entry.getkey (), Entry.getvalue (), Entry.getvalue ()
. GetClass ());
}
Mgr. Declarebean (key, Value, Value.getclass ());
}
Perform a corner book and provide a return value
protected Object Eval_r (final String expr) {
Try {
String className = Scriptableobject.getclass (). GetName ();
String key = Classname.substring (Classname.lastindexof (".") + 1);
Putvariable (key, Scriptableobject);
return Mgr. Eval_r (defaultengine, "", 0, 0, expr);
} catch (final bsfexception e) {
return e.getmessage ();
}
}
}
Use BSF + BeanShell to enable Java programs to run code in string form (reprint)