| 1 | |
package org.webslinger.ext.bsf.javaclass; |
| 2 | |
|
| 3 | |
import org.apache.bsf.BSFException; |
| 4 | |
|
| 5 | |
import org.webslinger.bsf.ApplyKey; |
| 6 | |
import org.webslinger.bsf.Compiler; |
| 7 | |
import org.webslinger.bsf.EvalKey; |
| 8 | |
import org.webslinger.bsf.ExecKey; |
| 9 | |
import org.webslinger.bsf.Key; |
| 10 | |
import org.webslinger.bsf.janino.AbstractJaninoCompiler; |
| 11 | |
|
| 12 | |
public final class JavaclassCompiler extends AbstractJaninoCompiler<Object, Class<Object>> { |
| 13 | |
protected interface Call { |
| 14 | |
Object call(Object object, Object[] args) throws Exception; |
| 15 | |
} |
| 16 | |
|
| 17 | |
public JavaclassCompiler(JavaclassEngine engine) { |
| 18 | 0 | super(engine); |
| 19 | 0 | implementsMap.put(JavaclassCallKey.class, Call.class); |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public Class<Object> compileKey(ApplyKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 23 | 0 | throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE, "compileApply"); |
| 24 | |
} |
| 25 | |
|
| 26 | |
public <V> Class<Object> compileKey(JavaclassCallKey key, Compiler<V, Class<Object>>.CompilerContext context) throws BSFException { |
| 27 | 0 | StringBuilder code = context.code; |
| 28 | 0 | code.append("\tpublic Object call(Object object, Object[] args) throws Exception {\n"); |
| 29 | 0 | code.append("\t\t"); |
| 30 | 0 | if (key.hasType()) code.append("return org.codehaus.janino.util.PrimitiveWrapper.wrap("); |
| 31 | 0 | code.append("((").append(key.getType().getName()).append(") object).").append(key.getName()).append("("); |
| 32 | 0 | Class[] types = key.getTypes(); |
| 33 | 0 | for (int i = 0; i < types.length; i++) { |
| 34 | 0 | code.append(i != 0 ? ",\n" : "\n"); |
| 35 | 0 | code.append("\t\t\t(").append(types[i].getName()).append(") args[").append(i).append("]"); |
| 36 | |
} |
| 37 | 0 | if (types.length != 0) code.append("\n\t\t"); |
| 38 | 0 | code.append(')'); |
| 39 | 0 | if (key.hasType()) code.append(')'); |
| 40 | 0 | code.append(";\n"); |
| 41 | 0 | if (!key.hasType()) code.append("\treturn null;\n"); |
| 42 | 0 | code.append("\t}\n"); |
| 43 | 0 | return null; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public Class<Object> compileKey(EvalKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 47 | 0 | throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE, "compileEval"); |
| 48 | |
} |
| 49 | |
|
| 50 | |
public Class<Object> compileKey(ExecKey<Class<Object>> key, JaninoCompilerContext context) throws BSFException { |
| 51 | 0 | throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE, "compileExec"); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public <V> Class<Object> compileKey(JavaclassObjectKey key, Compiler<V, Class<Object>>.CompilerContext context) throws BSFException { |
| 55 | 0 | appendText(context.code, "\t\t", context.text); |
| 56 | 0 | return null; |
| 57 | |
} |
| 58 | |
} |