"annotation != null"
})
static void addContractClauses(ContractMethodModel contract,
ContractCreationTrait trait,
ContractAnnotationModel annotation) {
ContractKind kind = getContractKind(annotation);
Iterator<String> itCode = trait.getExpressions().iterator();
Iterator<String> itMsg = trait.getMessages().iterator();
Iterator<String> itComment = trait.getSourceExpressions().iterator();
int successVariableCount = 0;
int exceptionVariableCount = 0;
while (itCode.hasNext()) {
StringBuilder buffer = new StringBuilder();
String expr = itCode.next();
String exprMsg = itMsg.next();
String exprComment = itComment.next();
/*
* Evaluate predicate. The success variable is first assigned a
* dummy value so as to be able to track the start of the
* evaluation in the generated bytecode.
*/
String successVariableName =
JavaUtils.SUCCESS_VARIABLE_PREFIX + "$" + successVariableCount++;
String exceptionTempVariableName =
JavaUtils.EXCEPTION_VARIABLE_PREFIX + "$" + exceptionVariableCount++;
String exceptionVariableName =
JavaUtils.EXCEPTION_VARIABLE_PREFIX + "$" + exceptionVariableCount++;
buffer.append("boolean ");
buffer.append(successVariableName);
buffer.append(" = false; ");
buffer.append("Throwable ");
buffer.append(exceptionVariableName);
buffer.append(" = null; ");
buffer.append("try { ");
buffer.append(successVariableName);
buffer.append(" = ");
buffer.append(JavaUtils.BEGIN_LOCATION_COMMENT);
buffer.append(JavaUtils.quoteComment(exprComment));
buffer.append(JavaUtils.END_LOCATION_COMMENT);
if (!annotation.isVirtual()) {
buffer.append(rebaseLocalCalls(expr, JavaUtils.THAT_VARIABLE, null));
} else {
buffer.append(expr);
}
buffer.append("; ");
buffer.append("} catch(Throwable ");
buffer.append(exceptionTempVariableName);
buffer.append(") {");
buffer.append(exceptionVariableName);
buffer.append(" = ");
buffer.append(exceptionTempVariableName);
buffer.append("; } ");
/* Handle failure. */
buffer.append("if (!(");
buffer.append(successVariableName);
buffer.append(")) { ");
if (kind.getVariance() == ContractVariance.CONTRAVARIANT) {
buffer.append("return new ");
buffer.append(trait.getExceptionName());
buffer.append("(\"");
buffer.append(ContractWriter.quoteString(exprMsg));
buffer.append("\", ");