Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.80 KB | None | 0 0
  1. /*
  2.  * Created on 27 sep 2010
  3.  */
  4.  
  5. package interpreter.api.util;
  6.  
  7. import java.lang.reflect.Method;
  8. import java.util.Arrays;
  9.  
  10. import interpreter.api.BytecodeMethod;
  11. import interpreter.api.BytecodeMethodReplacement;
  12. import interpreter.api.ExecutionFrame;
  13.  
  14. public class JavaBytecodeMethodBridge implements BytecodeMethodReplacement
  15. {
  16.    private final BytecodeMethod method;
  17.    private final Method         javaMethod;
  18.    private final Class< ? >     dc;
  19.    private final Class< ? >     rt;
  20.    private final Class< ? >[]   ps;
  21.  
  22.    public JavaBytecodeMethodBridge(BytecodeMethod method, Class< ? > javaClass) throws Exception
  23.    {
  24.       this.method = method;
  25.  
  26.       this.javaMethod = javaClass.getMethod(method.name, method.parameterTypes);
  27.       this.dc = javaMethod.getDeclaringClass();
  28.       this.rt = javaMethod.getReturnType();
  29.       this.ps = javaMethod.getParameterTypes();
  30.  
  31.       if (!Arrays.equals(ps, this.method.parameterTypes))
  32.          throw new IllegalStateException();
  33.    }
  34.  
  35.    @Override
  36.    public ExecutionFrame call(ExecutionFrame callSite)
  37.    {
  38.       try
  39.       {
  40.          Object[] args = InterpreterTypeUtil.loadValuesFromLocals(callSite, this.method.isStatic, ps);
  41.  
  42.          Object target = this.method.isStatic ? null : InterpreterTypeUtil.loadValueFromLocals(callSite, 0, dc);
  43.  
  44.          Object ret = javaMethod.invoke(target, args);
  45.          if (ret != null && rt != void.class && !rt.isPrimitive())
  46.             callSite.pushInt(callSite.thread.process.heap.ensureObject(ret));
  47.          else
  48.             InterpreterTypeUtil.pushTypedValueOnStack(callSite, rt, ret);
  49.  
  50.          InterpreterTypeUtil.returnTypedValue(callSite.thread, rt); // pops target
  51.       }
  52.       catch (Exception exc)
  53.       {
  54.          throw new RuntimeException(exc);
  55.       }
  56.  
  57.       return null;
  58.    }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement