@Override public Throwable execute(Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException { if (!isStaticField) { Object receiver = scope.getObject(parameters.get(1)); if (receiver == null) return new CodeUnderTestException(new NullPointerException()); } return super.execute(scope, out); }
/** {@inheritDoc} */ @Override public void afterStatement(Statement statement, Scope scope, Throwable exception) { try { for (VariableReference var : statement.getVariableReferences()) { if (var.equals(statement.getReturnValue()) || var.equals(statement.getReturnValue().getAdditionalVariableReference())) continue; Object object = var.getObject(scope); if (var.isPrimitive()) { ConstantValue value = new ConstantValue(test, var.getGenericClass()); value.setValue(object); // logger.info("Statement before inlining: " + statement.getCode()); statement.replace(var, value); // logger.info("Statement after inlining: " + statement.getCode()); } else if (var.isString() && object != null) { ConstantValue value = new ConstantValue(test, var.getGenericClass()); try { String val = StringEscapeUtils.unescapeJava(object.toString()); value.setValue(val); statement.replace(var, value); } catch (IllegalArgumentException e) { // Exceptions may happen if strings are not valid unicode logger.info("Cannot escape invalid string: " + object); } // logger.info("Statement after inlining: " + statement.getCode()); } else if (var.isArrayIndex()) { // If this is an array index and there is an object outside the array // then replace the array index with that object for (VariableReference otherVar : scope.getElements(var.getType())) { Object otherObject = otherVar.getObject(scope); if (otherObject == object && !otherVar.isArrayIndex() && otherVar.getStPosition() < statement.getPosition()) { statement.replace(var, otherVar); break; } } } else { // TODO: Ignoring exceptions during getObject, but keeping the assertion for now if (object == null) { ConstantValue value = new ConstantValue(test, var.getGenericClass()); value.setValue(null); // logger.info("Statement before inlining: " + statement.getCode()); statement.replace(var, value); // logger.info("Statement after inlining: " + statement.getCode()); } } } } catch (CodeUnderTestException e) { logger.warn("Not inlining test: " + e.getCause()); // throw new AssertionError("This case isn't handled yet: " + e.getCause() // + ", " + Arrays.asList(e.getStackTrace())); } }