Coverage Report - org.webslinger.ext.bsf.beanshell.BeanShellCompiler
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanShellCompiler
69%
36/52
100%
15/15
0
BeanShellCompiler$BeanShellCompilerContext
100%
4/4
N/A
0
 
 1  
 package org.webslinger.ext.bsf.beanshell;
 2  
 
 3  
 import java.util.HashMap;
 4  
 
 5  
 import org.apache.bsf.BSFDeclaredBean;
 6  
 import org.apache.bsf.BSFException;
 7  
 
 8  
 //import org.python.core.Py;
 9  
 //import org.python.core.PyException;
 10  
 //import org.python.core.PyObject;
 11  
 //import org.python.util.InteractiveInterpreter;
 12  
 
 13  
 import bsh.Interpreter;
 14  
 import bsh.BshMethod;
 15  
 import bsh.EvalError;
 16  
 import bsh.Primitive;
 17  
 
 18  
 import org.webslinger.bsf.ApplyKey;
 19  
 import org.webslinger.bsf.Compiler;
 20  
 import org.webslinger.bsf.EvalKey;
 21  
 import org.webslinger.bsf.ExecKey;
 22  
 import org.webslinger.bsf.Key;
 23  
 
 24  6
 public final class BeanShellCompiler extends Compiler<BeanShellCompiled, BeanShellCompiled> {
 25  
     public class BeanShellCompilerContext extends CompilerContext {
 26  3
         protected final Interpreter interpreter = new Interpreter();
 27  3
         protected BeanShellCompilerContext(Key<BeanShellCompiled> key) throws BSFException {
 28  3
             super(key);
 29  3
         }
 30  
     }
 31  
 
 32  
     public BeanShellCompiler(BeanShellEngine engine) {
 33  3
         super(engine);
 34  3
     }
 35  
 
 36  
     public void declareBean(BSFDeclaredBean bean) throws BSFException {
 37  0
         clear();
 38  0
     }
 39  
 
 40  
     public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
 41  0
         clear();
 42  0
     }
 43  
 
 44  
     protected CompilerContext newContext(Key<BeanShellCompiled> key) throws BSFException {
 45  3
         return new BeanShellCompilerContext(key);
 46  
     }
 47  
 
 48  
     protected BeanShellCompiled init(CompilerContext context, BeanShellCompiled compiled) {
 49  3
         return compiled;
 50  
     }
 51  
 
 52  
     protected BeanShellCompiled compileBeanShell(CompilerContext context, String methodName) throws BSFException {
 53  3
         StringBuilder code = context.code;
 54  3
         BeanShellCompilerContext bscc = (BeanShellCompilerContext) context;
 55  3
         Interpreter interpreter = bscc.interpreter;
 56  
         try {
 57  3
             interpreter.set("bsf", engine.getBSFFunctions());
 58  3
             for (BSFDeclaredBean bean: context.beans) {
 59  0
                 interpreter.set(bean.name, bean.bean);
 60  
             }
 61  3
             if (engine.isDebugOn()) System.err.println(code);
 62  3
             interpreter.eval(code.toString());
 63  0
         } catch (EvalError e) {
 64  0
             throw (BSFException) new BSFException(BSFException.REASON_OTHER_ERROR, "unable to compile context for BeanShell: " + context).initCause(e);
 65  3
         }
 66  3
         for (BshMethod method: interpreter.getNameSpace().getMethods()) {
 67  3
             if (methodName.equals(method.getName())) return new BeanShellCompiled(interpreter, method);
 68  
         }
 69  0
         throw new InternalError("Method [" + methodName + "] not found");
 70  
     }
 71  
 
 72  
     public BeanShellCompiled compileKey(ApplyKey<BeanShellCompiled> key, CompilerContext context) throws BSFException {
 73  3
         StringBuilder code = context.code;
 74  3
         String[] names = key.getNames();
 75  3
         Class[] types = key.getTypes();
 76  3
         code.append("Object _BSF_BeanShell_Apply_(Object[] _BSF_BeanShell_Apply_Args_) {\n");
 77  12
         for (int i = 0; i < names.length; i++) {
 78  
             //if (i != 0) code.append(", ");
 79  9
             code.append(types[i].getName()).append(' ').append(names[i]).append(" = (").append(types[i].getName()).append(") _BSF_BeanShell_Apply_Args_[").append(i).append("];\n");
 80  
         }
 81  3
         appendText(code, "\t", context.text);
 82  3
         context.code.append('}');
 83  3
         return compileBeanShell(context, "_BSF_BeanShell_Apply_");
 84  
     }
 85  
 
 86  
     public BeanShellCompiled compileKey(EvalKey<BeanShellCompiled> key, CompilerContext context) throws BSFException {
 87  0
         context.code.append("Object _BSF_BeanShell_Eval_() {");
 88  0
         appendText(context.code, "\t", context.text);
 89  0
         context.code.append('}');
 90  0
         return compileBeanShell(context, "_BSF_BeanShell_Eval_");
 91  
     }
 92  
 
 93  
     public BeanShellCompiled compileKey(ExecKey<BeanShellCompiled> key, CompilerContext context) throws BSFException {
 94  0
         context.code.append("_BSF_BeanShell_Exec_() {");
 95  0
         appendText(context.code, "\t", context.text);
 96  0
         context.code.append('}');
 97  0
         return compileBeanShell(context, "_BSF_BeanShell_Exec_");
 98  
     }
 99  
 
 100  
     public Object wrap(Object value, Class type) {
 101  9
         if (type == Void.TYPE) return Primitive.VOID;
 102  9
         if (value == null) return Primitive.NULL;
 103  9
         if (type == Boolean.TYPE) return new Primitive((Boolean) value);
 104  9
         if (type == Byte.TYPE) return new Primitive((Byte) value);
 105  9
         if (type == Short.TYPE) return new Primitive((Short) value);
 106  9
         if (type == Character.TYPE) return new Primitive((Character) value);
 107  9
         if (type == Integer.TYPE) return new Primitive((Integer) value);
 108  9
         if (type == Long.TYPE) return new Primitive((Long) value);
 109  9
         if (type == Float.TYPE) return new Primitive((Float) value);
 110  9
         if (type == Double.TYPE) return new Primitive((Double) value);
 111  9
         return value;
 112  
     }
 113  
 
 114  
     public Object unwrap(Object value) {
 115  3
         return Primitive.unwrap(value);
 116  
     }
 117  
 }