RequestOptionsGenerator.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:GitHub 作者:
private MethodAndStaticVar generateStaticMethodEquivalentForRequestOptionsStaticMethod(
    ExecutableElement staticMethod) {
  boolean memoize = memoizeStaticMethodFromArguments(staticMethod);
  String staticMethodName = staticMethod.getSimpleName().toString();

  String equivalentInstanceMethodName =
      getInstanceMethodNameFromStaticMethodName(staticMethodName);

  MethodSpec.Builder methodSpecBuilder =
      MethodSpec.methodBuilder(staticMethodName)
          .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
      .addJavadoc(processorUtil.generateSeeMethodJavadoc(staticMethod))
      .returns(glideOptionsName);

  List<? extends VariableElement> parameters = staticMethod.getParameters();
  StringBuilder createNewOptionAndCall = new StringBuilder("new $T().$N(");
  if (!parameters.isEmpty()) {
    methodSpecBuilder.addParameters(ProcessorUtil.getParameters(staticMethod));
    for (VariableElement parameter : parameters) {
      createNewOptionAndCall.append(parameter.getSimpleName().toString());
      // use the Application Context to avoid memory leaks.
      if (memoize && isAndroidContext(parameter)) {
        createNewOptionAndCall.append(".getApplicationContext()");
      }
      createNewOptionAndCall.append(", ");
    }
    createNewOptionAndCall = new StringBuilder(
        createNewOptionAndCall.substring(0, createNewOptionAndCall.length() - 2));
  }
  createNewOptionAndCall.append(")");

  FieldSpec requiredStaticField = null;
  if (memoize) {
    // Generates code that looks like:
    // if (GlideOptions.<methodName> == null) {
    //   GlideOptions.<methodName> = new GlideOptions().<methodName>().autoClone()
    // }

    // Mix in an incrementing unique id to handle method overloading.
    String staticVariableName = staticMethodName + nextStaticFieldUniqueId++;
    requiredStaticField = FieldSpec.builder(glideOptionsName, staticVariableName)
        .addModifiers(Modifier.PRIVATE, Modifier.STATIC)
        .build();
    methodSpecBuilder.beginControlFlow(
        "if ($T.$N == null)", glideOptionsName, staticVariableName)
        .addStatement("$T.$N =\n" + createNewOptionAndCall + ".$N",
            glideOptionsName, staticVariableName, glideOptionsName, equivalentInstanceMethodName,
            "autoClone()")
        .endControlFlow()
        .addStatement("return $T.$N", glideOptionsName, staticVariableName);
  } else {
    // Generates code that looks like:
    // return new GlideOptions().<methodName>()
    methodSpecBuilder.addStatement(
        "return " + createNewOptionAndCall, glideOptionsName, equivalentInstanceMethodName);
  }

  List<? extends TypeParameterElement> typeParameters = staticMethod.getTypeParameters();
  for (TypeParameterElement typeParameterElement : typeParameters) {
    methodSpecBuilder.addTypeVariable(
        TypeVariableName.get(typeParameterElement.getSimpleName().toString()));
  }

  methodSpecBuilder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build());

  return new MethodAndStaticVar(methodSpecBuilder.build(), requiredStaticField);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号