private static Set<Integer> getNormalRegistersAssigned(MethodState mState) { Set<Integer> assigned = new HashSet<>(); for (int register : mState.getRegistersAssigned()) { if (register < 0) { continue; } assigned.add(register); } for (int i = 0; i < mState.getParameterCount(); i++) { int parameterRegister = mState.getParameterStart() + i; assigned.remove(parameterRegister); } return assigned; }
public String toString(boolean onlyPeekCachedRegisters) { StringBuilder sb = new StringBuilder(); int localsCount = getRegisterCount() - getParameterCount(); sb.append("params: ").append(parameterCount).append(", "); sb.append("locals: ").append(localsCount).append('\n');
@Test public void methodStateForVirtualMethodCreatedCorrectly() throws VirtualMachineException { String methodDescriptor = "someString()Ljava/lang/String;"; VirtualMethod method = vm.getClassManager().getMethod(CLASS_NAME, methodDescriptor); ExecutionContext spawnedContext = new ExecutionContext(vm, method); ClassState templateClassState = TemplateStateFactory.forClass(spawnedContext, method.getDefiningClass()); spawnedContext.setClassState(templateClassState); MethodState templateMethodState = TemplateStateFactory.forMethod(spawnedContext); spawnedContext.setMethodState(templateMethodState); assertEquals(2, templateMethodState.getRegisterCount()); assertEquals(1, templateMethodState.getParameterCount()); int instanceRegister = templateMethodState.getParameterStart(); assertEquals(1, instanceRegister); HeapItem instanceItem = templateMethodState.peekRegister(instanceRegister); assertEquals(CLASS_NAME, instanceItem.getType()); assertEquals(UnknownValue.class, instanceItem.getValue().getClass()); }
@Test public void methodStateForObjectInitializationMethodCreatedCorrectly() throws VirtualMachineException { String methodDescriptor = "<init>()V"; VirtualMethod method = vm.getClassManager().getMethod(CLASS_NAME, methodDescriptor); ExecutionContext spawnedContext = new ExecutionContext(vm, method); ClassState templateClassState = TemplateStateFactory.forClass(spawnedContext, method.getDefiningClass()); spawnedContext.setClassState(templateClassState); MethodState templateMethodState = TemplateStateFactory.forMethod(spawnedContext); spawnedContext.setMethodState(templateMethodState); assertEquals(1, templateMethodState.getRegisterCount()); assertEquals(1, templateMethodState.getParameterCount()); int instanceRegister = templateMethodState.getParameterStart(); assertEquals(0, instanceRegister); HeapItem instanceItem = templateMethodState.peekRegister(instanceRegister); assertEquals(CLASS_NAME, instanceItem.getType()); assertEquals(UninitializedInstance.class, instanceItem.getValue().getClass()); }