From 5d8997a715edefdd4d5404f5ac53fc44b1fe2f64 Mon Sep 17 00:00:00 2001 From: Caleb Fenton Date: Mon, 22 Feb 2016 19:50:56 -0800 Subject: [PATCH] Clean up method refactor, heap item --- .../java/org/cf/smalivm/MethodReflector.java | 73 ++++++++++--------- .../java/org/cf/smalivm/context/HeapItem.java | 32 ++++---- 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/smalivm/src/main/java/org/cf/smalivm/MethodReflector.java b/smalivm/src/main/java/org/cf/smalivm/MethodReflector.java index 22e59abdb..b334948d2 100644 --- a/smalivm/src/main/java/org/cf/smalivm/MethodReflector.java +++ b/smalivm/src/main/java/org/cf/smalivm/MethodReflector.java @@ -46,42 +46,15 @@ public MethodReflector(VirtualMachine vm, VirtualMethod virtualMethod) { } - public void reflect(MethodState calleeContext) { + public void reflect(MethodState mState) { if (log.isDebugEnabled()) { - log.debug("Reflecting {} with context:\n{}", virtualMethod, calleeContext); + log.debug("Reflecting {} with context:\n{}", virtualMethod, mState); } - Object resultValue = null; + Object returnValue = null; try { - // TODO: easy - add tests for array class method reflecting - Class klazz = Class.forName(virtualMethod.getBinaryClassName()); - InvocationArguments invocationArgs = getArguments(calleeContext); - Object[] args = invocationArgs.getArgs(); - Class[] parameterTypes = invocationArgs.getParameterTypes(); - if (virtualMethod.isStatic()) { - if (log.isDebugEnabled()) { - log.debug("Reflecting {}, clazz={} args={}", virtualMethod, klazz, Arrays.toString(args)); - } - resultValue = MethodUtils - .invokeStaticMethod(klazz, virtualMethod.getMethodName(), args, parameterTypes); - } else { - if ("".equals(virtualMethod.getMethodName())) { - if (log.isDebugEnabled()) { - log.debug("Reflecting {}, class={} args={}", virtualMethod, klazz, Arrays.toString(args)); - } - resultValue = ConstructorUtils.invokeConstructor(klazz, args); - calleeContext.assignParameter(0, new HeapItem(resultValue, virtualMethod.getClassName())); - } else { - HeapItem targetItem = calleeContext.peekRegister(0); - if (log.isDebugEnabled()) { - log.debug("Reflecting {}, target={} args={}", virtualMethod, targetItem, Arrays.toString(args)); - } - resultValue = MethodUtils.invokeMethod(targetItem.getValue(), virtualMethod.getMethodName(), args, - parameterTypes); - } - } + returnValue = invoke(mState); } catch (NullPointerException | ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - resultValue = new UnknownValue(); if (log.isWarnEnabled()) { log.warn("Failed to reflect {}: {}", virtualMethod, e.getMessage()); } @@ -89,11 +62,13 @@ public void reflect(MethodState calleeContext) { if (log.isDebugEnabled()) { log.debug("Stack trace:", e); } + + returnValue = new UnknownValue(); } if (!virtualMethod.returnsVoid()) { - HeapItem resultItem = new HeapItem(resultValue, virtualMethod.getReturnType()); - calleeContext.assignReturnRegister(resultItem); + HeapItem returnItem = new HeapItem(returnValue, virtualMethod.getReturnType()); + mState.assignReturnRegister(returnItem); } } @@ -140,4 +115,36 @@ private InvocationArguments getArguments(MethodState mState) throws ClassNotFoun return new InvocationArguments(args, parameterTypes); } + private Object invoke(MethodState mState) throws ClassNotFoundException, NoSuchMethodException, + IllegalAccessException, InvocationTargetException, InstantiationException { + Object returnValue; + Class klazz = Class.forName(virtualMethod.getBinaryClassName()); + InvocationArguments invocationArgs = getArguments(mState); + Object[] args = invocationArgs.getArgs(); + Class[] parameterTypes = invocationArgs.getParameterTypes(); + if (virtualMethod.isStatic()) { + if (log.isDebugEnabled()) { + log.debug("Reflecting {}, clazz={} args={}", virtualMethod, klazz, Arrays.toString(args)); + } + returnValue = MethodUtils.invokeStaticMethod(klazz, virtualMethod.getMethodName(), args, parameterTypes); + } else { + if ("".equals(virtualMethod.getMethodName())) { + if (log.isDebugEnabled()) { + log.debug("Reflecting {}, class={} args={}", virtualMethod, klazz, Arrays.toString(args)); + } + returnValue = ConstructorUtils.invokeConstructor(klazz, args); + mState.assignParameter(0, new HeapItem(returnValue, virtualMethod.getClassName())); + } else { + HeapItem targetItem = mState.peekRegister(0); + if (log.isDebugEnabled()) { + log.debug("Reflecting {}, target={} args={}", virtualMethod, targetItem, Arrays.toString(args)); + } + returnValue = MethodUtils.invokeMethod(targetItem.getValue(), virtualMethod.getMethodName(), args, + parameterTypes); + } + } + + return returnValue; + } + } diff --git a/smalivm/src/main/java/org/cf/smalivm/context/HeapItem.java b/smalivm/src/main/java/org/cf/smalivm/context/HeapItem.java index 9e5a9bd9e..accb3fee1 100644 --- a/smalivm/src/main/java/org/cf/smalivm/context/HeapItem.java +++ b/smalivm/src/main/java/org/cf/smalivm/context/HeapItem.java @@ -33,22 +33,6 @@ public HeapItem(Object value, String type) { type = other.getType(); } - public double asDouble() { - return Utils.getDoubleValue(getValue()); - } - - public float asFloat() { - return Utils.getFloatValue(getValue()); - } - - public int asInteger() { - return Utils.getIntegerValue(getValue()); - } - - public long asLong() { - return Utils.getLongValue(getValue()); - } - @Override public boolean equals(Object obj) { if (obj == null) { @@ -69,6 +53,22 @@ public String getComponentBase() { return ClassNameUtils.getComponentBase(getType()); } + public double asDouble() { + return Utils.getDoubleValue(getValue()); + } + + public float asFloat() { + return Utils.getFloatValue(getValue()); + } + + public int asInteger() { + return Utils.getIntegerValue(getValue()); + } + + public long asLong() { + return Utils.getLongValue(getValue()); + } + public String getType() { return type; }