| 1 | |
package org.webslinger.bsf.janino; |
| 2 | |
|
| 3 | |
import org.apache.bsf.BSFException; |
| 4 | |
|
| 5 | |
import org.webslinger.bsf.ApplyKey; |
| 6 | |
import org.webslinger.bsf.EvalKey; |
| 7 | |
import org.webslinger.bsf.ExecKey; |
| 8 | |
|
| 9 | |
public final class JaninoCompiler extends AbstractJaninoCompiler<Object, Class<Object>> { |
| 10 | |
public interface Apply { |
| 11 | |
Object apply(Object[] args) throws Exception; |
| 12 | |
} |
| 13 | |
public interface Exec { |
| 14 | |
void exec() throws Exception; |
| 15 | |
} |
| 16 | |
public interface Eval { |
| 17 | |
Object eval() throws Exception; |
| 18 | |
} |
| 19 | |
|
| 20 | |
public JaninoCompiler(JaninoEngine engine) { |
| 21 | 21 | super(engine); |
| 22 | 21 | implementsMap.put(ApplyKey.class, Apply.class); |
| 23 | 21 | implementsMap.put(EvalKey.class, Eval.class); |
| 24 | 21 | implementsMap.put(ExecKey.class, Exec.class); |
| 25 | 21 | } |
| 26 | |
|
| 27 | |
protected Class<Object> compileKey(ApplyKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 28 | 70 | StringBuilder code = context.code; |
| 29 | 70 | code.append("public Object apply(Object[] args) throws Exception {"); |
| 30 | 70 | String[] names = key.getNames(); |
| 31 | 70 | Class[] types = key.getTypes(); |
| 32 | 280 | for (int i = 0; i < types.length; i++) { |
| 33 | 210 | Class type = types[i]; |
| 34 | |
String typeName, prefix, postfix; |
| 35 | 210 | if (type == Boolean.TYPE || type == Boolean.class) { |
| 36 | 0 | typeName = "boolean"; |
| 37 | 0 | prefix = "((Boolean) "; |
| 38 | 0 | postfix = ").booleanValue()"; |
| 39 | 210 | } else if (type == Byte.TYPE || type == Byte.class) { |
| 40 | 0 | typeName = "byte"; |
| 41 | 0 | prefix = "((Byte) "; |
| 42 | 0 | postfix = ").byteValue()"; |
| 43 | 210 | } else if (type == Double.TYPE || type == Double.class) { |
| 44 | 0 | typeName = "double"; |
| 45 | 0 | prefix = "((Double) "; |
| 46 | 0 | postfix = ").doubleValue()"; |
| 47 | 210 | } else if (type == Float.TYPE || type == Float.class) { |
| 48 | 0 | typeName = "float"; |
| 49 | 0 | prefix = "((Float) "; |
| 50 | 0 | postfix = ").floatValue()"; |
| 51 | 210 | } else if (type == Integer.TYPE || type == Integer.class) { |
| 52 | 0 | typeName = "int"; |
| 53 | 0 | prefix = "((Integer) "; |
| 54 | 0 | postfix = ").intValue()"; |
| 55 | 210 | } else if (type == Long.TYPE || type == Long.class) { |
| 56 | 0 | typeName = "long"; |
| 57 | 0 | prefix = "((Long) "; |
| 58 | 0 | postfix = ").longValue()"; |
| 59 | 210 | } else if (type == Short.TYPE || type == Short.class) { |
| 60 | 0 | typeName = "short"; |
| 61 | 0 | prefix = "((Short) "; |
| 62 | 0 | postfix = ").shortValue()"; |
| 63 | |
} else { |
| 64 | 210 | typeName = type.getName(); |
| 65 | 210 | prefix = '(' + typeName + ')'; |
| 66 | 210 | postfix = ""; |
| 67 | |
} |
| 68 | 210 | code.append(' ').append(typeName).append(' ').append(names[i]).append(" = ").append(prefix).append("args[").append(i).append(']').append(postfix).append(';'); |
| 69 | |
} |
| 70 | 70 | code.append(context.text); |
| 71 | 70 | code.append(" }"); |
| 72 | 70 | return null; |
| 73 | |
} |
| 74 | |
|
| 75 | |
protected Class<Object> compileKey(EvalKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 76 | 0 | StringBuilder code = context.code; |
| 77 | 0 | code.append("public Object eval() throws Exception {"); |
| 78 | 0 | code.append("return org.codehaus.janino.util.PrimitiveWrapper.wrap("); |
| 79 | 0 | code.append(context.text); |
| 80 | 0 | code.append(");"); |
| 81 | 0 | code.append('}'); |
| 82 | 0 | return null; |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected Class<Object> compileKey(ExecKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 86 | 0 | StringBuilder code = context.code; |
| 87 | 0 | code.append("public void exec() throws Exception {"); |
| 88 | 0 | code.append(context.text); |
| 89 | 0 | code.append('}'); |
| 90 | 0 | return null; |
| 91 | |
} |
| 92 | |
} |