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

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


    if (id != -1) {
      append(", id = ");
      append(Integer.toString(id));
    }

    MethodModel contracted = contract.getContractedMethod();
    if (contracted != null) {
      append(", target = \"");
      append(contracted.getSimpleName());
      append("\"");
    }

    List<Long> lineNumbers = contract.getLineNumbers();
    if (lineNumbers != null && !lineNumbers.isEmpty()) {
View Full Code Here


      ContractCreationTrait trait, ContractMethodModel contract,
      ContractAnnotationModel annotation) {
    if (!trait.visit(annotation)) {
      return null;
    }
    MethodModel helper = createContractHelper(trait, annotation);
    return createContractMethod(trait, contract, annotation, helper);
  }
View Full Code Here

  static ContractMethodModel createBlankContractMethod(
      ContractKind kind, ContractAnnotationModel annotation,
      String nameSuffix) {
    TypeModel type = Elements.getTypeOf(annotation);

    MethodModel contracted = null;
    if (kind.isMethodContract()) {
      contracted = (MethodModel) annotation.getEnclosingElement();
    }

    String name = kind.getNameSpace() + getContractName(kind, contracted);
View Full Code Here

  @Ensures("result != null")
  static MethodModel createBlankContractHelper(
      ContractKind kind, ContractAnnotationModel annotation,
      String nameSuffix) {
    TypeModel type = Elements.getTypeOf(annotation);
    MethodModel method = null;
    ContractMethodModel contract = null;

    MethodModel contracted = null;
    if (kind.isMethodContract()) {
      contracted = (MethodModel) annotation.getEnclosingElement();
    }

    TypeName returnType = new TypeName("void");
    String name = getHelperName(kind, annotation.getOwner(), contracted);
    if (nameSuffix != null) {
      name += nameSuffix;
    }
    if (annotation.isPrimary()) {
      contract = new ContractMethodModel(ContractKind.HELPER, name,
                                         returnType, contracted);

      contract.setSourceInfo(annotation.getSourceInfo());

      if (!annotation.isVirtual()) {
        for (TypeName typeParam : type.getTypeParameters()) {
          contract.addTypeParameter(typeParam);
        }
      }

      method = contract;
    } else {
      method = new MethodModel(ElementKind.CONTRACT_MOCK, name, returnType);
      if (contracted != null) {
        Elements.copyParameters(method, contracted.getParameters());
      }
    }

    if (!annotation.isVirtual()) {
      method.addParameter(
View Full Code Here

  })
  @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());
View Full Code Here

    "lineNumber == null || lineNumber >= 1"
  })
  private void createOldMethods(ContractKind kind,
      int pos, int id, String expr, ContractAnnotationModel annotation,
      Long lineNumber) {
    MethodModel helper =
        ContractCreation.createBlankContractHelper(kind, annotation,
                                                   "$" + Integer.toString(pos));
    helper.setReturnType(new ClassName("java/lang/Object"));

    if (helper.getKind() == ElementKind.CONTRACT_METHOD) {
      ContractMethodModel helperContract = (ContractMethodModel) helper;
      if (lineNumber != null) {
        helperContract.setLineNumbers(Collections.singletonList(lineNumber));
      }
      String code = expr;
View Full Code Here

    return null;
  }

  @Override
  public Void visitExecutable(ExecutableElement e, ElementModel p) {
    MethodModel exec = null;
    String name = e.getSimpleName().toString();

    /*
     * For enum types, synthesized methods values() and
     * valueOf(String) are reflected by the API but must not be
     * reproduced in the mock.
     */
    if (p.getKind() == ElementKind.ENUM) {
      ExecutableType t = (ExecutableType) e.asType();
      if (name.equals("values")) {
        if (t.getParameterTypes().isEmpty()) {
          return null;
        }
      } else if (name.equals("valueOf")) {
        List<TypeMirror> valueOfParameterTypes =
            Collections.singletonList(
                utils.elementUtils
                .getTypeElement("java.lang.String").asType());
        if (t.getParameterTypes().equals(valueOfParameterTypes)) {
          return null;
        }
      }
    }

    /* Create element; decide if constructor or not. */
    if (name.toString().equals("<init>")) {
      exec = new MethodModel();
    } else {
      exec = new MethodModel(ElementKind.METHOD, name,
                             utils.getTypeNameForType(e.getReturnType()));
    }
    utils.copyModifiers(e, exec);

    /* Add generic signature. */
    List<? extends TypeParameterElement> genericTypes = e.getTypeParameters();
    for (TypeParameterElement tp : genericTypes) {
      exec.addTypeParameter(utils.getGenericTypeName(tp));
    }

    /* Add parameters. */
    scan(e.getParameters(), exec);
    exec.setVariadic(e.isVarArgs());

    /* Add throws list. */
    for (TypeMirror tt : e.getThrownTypes()) {
      exec.addException(utils.getTypeNameForType(tt));
    }

    /* Add annotations. */
    scanAnnotations(e, true, type.getName(), exec);

View Full Code Here

TOP

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

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.