Package com.google.java.contract.core.model

Examples of com.google.java.contract.core.model.ContractKind


    "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("\", ");
View Full Code Here


  })
  @Ensures("result != null")
  static ContractMethodModel createContractMethod(
      ContractCreationTrait trait, ContractMethodModel contract,
      ContractAnnotationModel annotation, MethodModel helper) {
    ContractKind kind = getContractKind(annotation);

    if (contract == null) {
      contract = createBlankContractMethod(kind, annotation, "");
      Elements.copyParameters(contract, trait.getInitialParameters());

      if (kind.getVariance() == ContractVariance.CONTRAVARIANT) {
        contract.setPrologue(trait.getExceptionName() + " "
                             + JavaUtils.ERROR_VARIABLE + " = null;");
        contract.setEpilogue(RAISE_METHOD + "("
                             + JavaUtils.ERROR_VARIABLE + ");");
      }
    }
    Elements.copyParameters(contract, trait.getExtraParameters());

    if (annotation.isPrimary()) {
      contract.setSourceInfo(annotation.getSourceInfo());
    }

    String code = getHelperCallCode(helper, annotation) + ";";
    if (kind.getVariance() == ContractVariance.CONTRAVARIANT) {
      code = JavaUtils.ERROR_VARIABLE + " = " + code
          + "if (" + JavaUtils.ERROR_VARIABLE + " == null) { return; }";
    }
    contract.addStatement(code);
View Full Code Here

    "annotation != null"
  })
  @Ensures("result != null")
  static MethodModel createContractHelper(ContractCreationTrait trait,
                                          ContractAnnotationModel annotation) {
    ContractKind kind = getContractKind(annotation);
    MethodModel method = createBlankContractHelper(kind, annotation, null);

    TypeName returnType =
        new TypeName(kind.getVariance() == ContractVariance.CONTRAVARIANT
                     ? trait.getExceptionName()
                     : "void");
    method.setReturnType(returnType);

    if (kind.getVariance() == ContractVariance.CONTRAVARIANT) {
      method.addParameter(
          new VariableModel(ElementKind.PARAMETER,
                            JavaUtils.ERROR_VARIABLE, returnType));
    }

    if (annotation.isPrimary()) {
      method.setSourceInfo(annotation.getSourceInfo());
    }

    if (method.getKind() == ElementKind.CONTRACT_METHOD) {
      ContractMethodModel contract = (ContractMethodModel) method;

      Elements.copyParameters(contract, trait.getInitialParameters());
      Elements.copyParameters(contract, trait.getExtraParameters());

      addContractClauses(contract, trait, annotation);
      if (kind.getVariance() == ContractVariance.CONTRAVARIANT) {
        contract.setEpilogue("return null;");
      }

      if (annotation.isPrimary()) {
        contract.setLineNumbers(annotation.getLineNumbers());
View Full Code Here

      int id = transformer.getNextOldId();
      transformer.setAcceptOld(true);
      boolean success = super.transform(code, lineNumbers, sourceInfo);

      if (success) {
        ContractKind oldKind =
            ContractCreation.getContractKind(annotation).getOldKind();
        Iterator<String> iterCode =
            transformer.getOldParametersCode().iterator();
        Iterator<Long> iterLineNumber =
            transformer.getOldParametersLineNumbers().iterator();
View Full Code Here

  protected void captureLastMethodNode() {
    if (lastMethodNode == null) {
      return;
    }

    ContractKind kind = ContractMethodSignatures.getKind(lastMethodNode);
    if (kind != null) {
      List<Long> lineNumbers =
          ContractMethodSignatures.getLineNumbers(lastMethodNode);

      if (kind.isClassContract() || kind.isHelperContract()) {
        ClassContractHandle ch =
            new ClassContractHandle(kind, className,
                                    lastMethodNode, lineNumbers);
        classHandles.add(ch);
      } else {
View Full Code Here

TOP

Related Classes of com.google.java.contract.core.model.ContractKind

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.