/**
   * 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();
  }
示例#2
0
 /**
  * Returns a code block that injects the instance's field or method by calling a static method on
  * the parent MembersInjector class.
  */
 private CodeBlock delegateInjectMemberCodeBlock(
     ImmutableMap<BindingKey, FieldSpec> dependencyFields, InjectionSite injectionSite) {
   return CodeBlocks.format(
       "$L.$L($L);",
       javapoetMembersInjectorNameForType(
           MoreElements.asType(injectionSite.element().getEnclosingElement())),
       injectionSiteDelegateMethodName(injectionSite.element()),
       makeParametersCodeBlock(
           new ImmutableList.Builder<CodeBlock>()
               .add(CodeBlocks.format("instance"))
               .addAll(parameterCodeBlocks(dependencyFields, injectionSite.dependencies(), false))
               .build()));
 }