/** INTERNAL: Visit an executable and create a MetadataMethod object. */
  @Override
  public MetadataMethod visitExecutable(
      ExecutableElement executableElement, MetadataClass metadataClass) {
    MetadataMethod method = new MetadataMethod(metadataClass.getMetadataFactory(), metadataClass);

    // Set the name.
    method.setName(executableElement.getSimpleName().toString());

    // Set the attribute name.
    method.setAttributeName(Helper.getAttributeNameFromMethodName(method.getName()));

    // Set the modifiers.
    method.setModifiers(getModifiers(executableElement.getModifiers()));

    // Visit executable element for the parameters, return type and generic type.
    executableElement.asType().accept(typeVisitor, method);

    // Set the annotations.
    buildMetadataAnnotations(method, executableElement.getAnnotationMirrors());

    // Handle multiple methods with the same name.
    MetadataMethod existing = metadataClass.getMethods().get(method.getName());
    if (existing == null) {
      metadataClass.addMethod(method);
    } else {
      while (existing.getNext() != null) {
        existing = existing.getNext();
      }
      existing.setNext(method);
    }

    return method;
  }
  /**
   * Formats an ExecutableElement as if it were contained within the container, if the container is
   * present.
   */
  public String format(ExecutableElement method, Optional<DeclaredType> container) {
    StringBuilder builder = new StringBuilder();
    TypeElement type = MoreElements.asType(method.getEnclosingElement());
    ExecutableType executableType = MoreTypes.asExecutable(method.asType());
    if (container.isPresent()) {
      executableType = MoreTypes.asExecutable(types.asMemberOf(container.get(), method));
      type = MoreElements.asType(container.get().asElement());
    }

    // TODO(cgruber): AnnotationMirror formatter.
    List<? extends AnnotationMirror> annotations = method.getAnnotationMirrors();
    if (!annotations.isEmpty()) {
      Iterator<? extends AnnotationMirror> annotationIterator = annotations.iterator();
      for (int i = 0; annotationIterator.hasNext(); i++) {
        if (i > 0) {
          builder.append(' ');
        }
        builder.append(ErrorMessages.format(annotationIterator.next()));
      }
      builder.append(' ');
    }
    builder.append(nameOfType(executableType.getReturnType()));
    builder.append(' ');
    builder.append(type.getQualifiedName());
    builder.append('.');
    builder.append(method.getSimpleName());
    builder.append('(');
    checkState(method.getParameters().size() == executableType.getParameterTypes().size());
    Iterator<? extends VariableElement> parameters = method.getParameters().iterator();
    Iterator<? extends TypeMirror> parameterTypes = executableType.getParameterTypes().iterator();
    for (int i = 0; parameters.hasNext(); i++) {
      if (i > 0) {
        builder.append(", ");
      }
      appendParameter(builder, parameters.next(), parameterTypes.next());
    }
    builder.append(')');
    return builder.toString();
  }
예제 #3
0
  void addMethod(ModuleMetaModel context, AnnotationKey key2, AnnotationState annotation) {

    //
    String id = (String) annotation.get("id");

    ExecutableElement methodElt =
        (ExecutableElement) context.processingContext.get(key2.getElement());

    //
    for (Phase phase : Phase.values()) {
      if (phase.annotation.getName().equals(key2.getType().toString())) {
        ElementHandle.Method origin = (ElementHandle.Method) key2.getElement();

        // First remove the previous method
        Key<MethodMetaModel> key = Key.of(origin, MethodMetaModel.class);
        if (getChild(key) == null) {
          // Parameters
          ArrayList<ParameterMetaModel> parameters = new ArrayList<ParameterMetaModel>();
          List<? extends TypeMirror> parameterTypeMirrors =
              ((ExecutableType) methodElt.asType()).getParameterTypes();
          List<? extends VariableElement> parameterVariableElements = methodElt.getParameters();
          for (int i = 0; i < parameterTypeMirrors.size(); i++) {
            VariableElement parameterVariableElt = parameterVariableElements.get(i);
            TypeMirror parameterTypeMirror = parameterTypeMirrors.get(i);
            String typeLiteral = context.processingContext.getLiteralName(parameterTypeMirror);

            //
            String parameterName = parameterVariableElt.getSimpleName().toString();

            // Determine cardinality
            TypeMirror parameterSimpleTypeMirror;
            Cardinality parameterCardinality;
            switch (parameterTypeMirror.getKind()) {
              case DECLARED:
                DeclaredType dt = (DeclaredType) parameterTypeMirror;
                TypeElement col = context.processingContext.getTypeElement("java.util.List");
                TypeMirror tm = context.processingContext.erasure(col.asType());
                TypeMirror err = context.processingContext.erasure(dt);
                // context.env.isSubtype(err, tm)
                if (err.equals(tm)) {
                  if (dt.getTypeArguments().size() != 1) {
                    throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
                  } else {
                    parameterCardinality = Cardinality.LIST;
                    parameterSimpleTypeMirror = dt.getTypeArguments().get(0);
                  }
                } else {
                  parameterCardinality = Cardinality.SINGLE;
                  parameterSimpleTypeMirror = parameterTypeMirror;
                }
                break;
              case ARRAY:
                // Unwrap array
                ArrayType arrayType = (ArrayType) parameterTypeMirror;
                parameterCardinality = Cardinality.ARRAY;
                parameterSimpleTypeMirror = arrayType.getComponentType();
                break;
              default:
                throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
            }
            if (parameterSimpleTypeMirror.getKind() != TypeKind.DECLARED) {
              throw CONTROLLER_METHOD_PARAMETER_NOT_RESOLVED.failure(parameterVariableElt);
            }

            //
            TypeElement te =
                (TypeElement) context.processingContext.asElement(parameterSimpleTypeMirror);
            ElementHandle.Type a = ElementHandle.Type.create(te);

            //
            if (te.toString().equals("java.lang.String")
                || te.getAnnotation(Mapped.class) != null) {
              // Not sure we should use @Param for this (i.e for now it looks hackish)
              // however it does make sense later to use the regex part for non router
              // parameters
              Param param = parameterVariableElt.getAnnotation(Param.class);
              String alias = param != null && param.name().length() > 0 ? param.name() : null;
              parameters.add(
                  new PhaseParameterMetaModel(
                      parameterName, parameterCardinality, a, typeLiteral, alias));
            } else {
              parameters.add(new ContextualParameterMetaModel(parameterName, typeLiteral));
            }
          }

          //
          MethodMetaModel method =
              new MethodMetaModel(
                  origin, id, phase, methodElt.getSimpleName().toString(), parameters);
          addChild(key, method);
          modified = true;
        }
        break;
      }
    }
  }
예제 #4
0
 private void visitConstructorList(final List<ExecutableElement> constructorsOfClass) {
   for (ExecutableElement constructorOfClass : constructorsOfClass) {
     final ConstructorGeneratorVisitor visitor = new ConstructorGeneratorVisitor();
     constructors.add(constructorOfClass.asType().accept(visitor, constructorOfClass));
   }
 }