Skip to content

Commit

Permalink
Clean up method refactor, heap item
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebFenton committed Feb 23, 2016
1 parent 65f30e0 commit 5d8997a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
73 changes: 40 additions & 33 deletions smalivm/src/main/java/org/cf/smalivm/MethodReflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,29 @@ 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 ("<init>".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());
}

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);
}
}

Expand Down Expand Up @@ -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 ("<init>".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;
}

}
32 changes: 16 additions & 16 deletions smalivm/src/main/java/org/cf/smalivm/context/HeapItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 5d8997a

Please sign in to comment.