Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created on 27 sep 2010
- */
- package interpreter.api.util;
- import java.lang.reflect.Method;
- import java.util.Arrays;
- import interpreter.api.BytecodeMethod;
- import interpreter.api.BytecodeMethodReplacement;
- import interpreter.api.ExecutionFrame;
- public class JavaBytecodeMethodBridge implements BytecodeMethodReplacement
- {
- private final BytecodeMethod method;
- private final Method javaMethod;
- private final Class< ? > dc;
- private final Class< ? > rt;
- private final Class< ? >[] ps;
- public JavaBytecodeMethodBridge(BytecodeMethod method, Class< ? > javaClass) throws Exception
- {
- this.method = method;
- this.javaMethod = javaClass.getMethod(method.name, method.parameterTypes);
- this.dc = javaMethod.getDeclaringClass();
- this.rt = javaMethod.getReturnType();
- this.ps = javaMethod.getParameterTypes();
- if (!Arrays.equals(ps, this.method.parameterTypes))
- throw new IllegalStateException();
- }
- @Override
- public ExecutionFrame call(ExecutionFrame callSite)
- {
- try
- {
- Object[] args = InterpreterTypeUtil.loadValuesFromLocals(callSite, this.method.isStatic, ps);
- Object target = this.method.isStatic ? null : InterpreterTypeUtil.loadValueFromLocals(callSite, 0, dc);
- Object ret = javaMethod.invoke(target, args);
- if (ret != null && rt != void.class && !rt.isPrimitive())
- callSite.pushInt(callSite.thread.process.heap.ensureObject(ret));
- else
- InterpreterTypeUtil.pushTypedValueOnStack(callSite, rt, ret);
- InterpreterTypeUtil.returnTypedValue(callSite.thread, rt); // pops target
- }
- catch (Exception exc)
- {
- throw new RuntimeException(exc);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement