当前位置: 首页>>代码示例>>Java>>正文


Java NameAllocator.get方法代码示例

本文整理汇总了Java中com.squareup.javapoet.NameAllocator.get方法的典型用法代码示例。如果您正苦于以下问题:Java NameAllocator.get方法的具体用法?Java NameAllocator.get怎么用?Java NameAllocator.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.squareup.javapoet.NameAllocator的用法示例。


在下文中一共展示了NameAllocator.get方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: messageAdapterEncodedSize

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec messageAdapterEncodedSize(NameAllocator nameAllocator, MessageType type,
    TypeName javaType, boolean useBuilder) {
  MethodSpec.Builder result = MethodSpec.methodBuilder("encodedSize")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .returns(int.class)
      .addParameter(javaType, "value");

  result.addCode("$[");
  String leading = "return";
  for (Field field : type.fieldsAndOneOfFields()) {
    int fieldTag = field.tag();
    String fieldName = nameAllocator.get(field);
    CodeBlock adapter = adapterFor(field);
    result.addCode("$L $L.encodedSizeWithTag($L, ", leading, adapter, fieldTag)
        .addCode((useBuilder ? "value.$L" : "$L(value)"), fieldName)
        .addCode(")");
    leading = "\n+";
  }
  if (useBuilder) {
    result.addCode("$L value.unknownFields().size()", leading);
  }
  result.addCode(";$]\n", leading);

  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:27,代码来源:JavaGenerator.java

示例2: decodeAndAssign

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private CodeBlock decodeAndAssign(Field field, NameAllocator nameAllocator, boolean useBuilder) {
  String fieldName = nameAllocator.get(field);
  CodeBlock decode = CodeBlock.of("$L.decode(reader)", singleAdapterFor(field));
  if (field.isRepeated()) {
    return useBuilder
        ? CodeBlock.of("builder.$L.add($L)", fieldName, decode)
        : CodeBlock.of("$L.add($L)", fieldName, decode);
  } else if (field.type().isMap()) {
    return useBuilder
        ? CodeBlock.of("builder.$L.putAll($L)", fieldName, decode)
        : CodeBlock.of("$L.putAll($L)", fieldName, decode);
  } else {
    return useBuilder
        ? CodeBlock.of("builder.$L($L)", fieldName, decode)
        : CodeBlock.of("$L = $L", fieldName, decode);
  }
}
 
开发者ID:square,项目名称:wire,代码行数:18,代码来源:JavaGenerator.java

示例3: messageFieldsConstructor

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
  MethodSpec.Builder result = MethodSpec.constructorBuilder();
  result.addModifiers(PUBLIC);
  result.addCode("this(");
  for (Field field : type.fieldsAndOneOfFields()) {
    TypeName javaType = fieldType(field);
    String fieldName = nameAllocator.get(field);
    ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
    if (emitAndroid && field.isOptional()) {
      param.addAnnotation(NULLABLE);
    }
    result.addParameter(param.build());
    result.addCode("$L, ", fieldName);
  }
  result.addCode("$T.EMPTY);\n", BYTE_STRING);
  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:18,代码来源:JavaGenerator.java

示例4: createWriteMethod

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec createWriteMethod(TypeName autoValueClassName,
    ImmutableMap<Property, FieldSpec> adapters) {
  String writerName = "writer";
  String valueName = "value";
  ParameterSpec writer = ParameterSpec.builder(JsonWriter.class, writerName).build();
  ParameterSpec value = ParameterSpec.builder(autoValueClassName, valueName).build();
  MethodSpec.Builder writeMethod = MethodSpec.methodBuilder("toJson")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .addParameter(writer)
      .addParameter(value)
      .addException(IOException.class);

  writeMethod.addStatement("$N.beginObject()", writer);

  NameAllocator nameAllocator = new NameAllocator();
  nameAllocator.newName(writerName);
  nameAllocator.newName(valueName);
  for (Map.Entry<Property, FieldSpec> entry : adapters.entrySet()) {
    Property prop = entry.getKey();
    FieldSpec field = entry.getValue();
    nameAllocator.newName(prop.humanName, prop);

    if (prop.nullable()) {
      String name = nameAllocator.get(prop);
      writeMethod.addStatement("$T $N = $N.$N()", prop.type, name, value, prop.methodName);
      writeMethod.beginControlFlow("if ($N != null)", name);
      writeMethod.addStatement("$N.name($S)", writer, prop.serializedName());
      writeMethod.addStatement("this.$N.toJson($N, $N)", field, writer, name);
      writeMethod.endControlFlow();
    } else {
      writeMethod.addStatement("$N.name($S)", writer, prop.serializedName());
      writeMethod.addStatement("this.$N.toJson($N, $N.$N())", field, writer, value,
          prop.methodName);
    }
  }
  writeMethod.addStatement("$N.endObject()", writer);

  return writeMethod.build();
}
 
开发者ID:rharter,项目名称:auto-value-moshi,代码行数:41,代码来源:AutoValueMoshiExtension.java

示例5: messageEquals

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
  NameAllocator localNameAllocator = nameAllocator.clone();
  String otherName = localNameAllocator.newName("other");
  String oName = localNameAllocator.newName("o");

  TypeName javaType = typeName(type.type());
  MethodSpec.Builder result = MethodSpec.methodBuilder("equals")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .returns(boolean.class)
      .addParameter(Object.class, otherName);

  List<Field> fields = type.fieldsAndOneOfFields();
  if (fields.isEmpty()) {
    result.addStatement("return $N instanceof $T", otherName, javaType);
    return result.build();
  }

  result.addStatement("if ($N == this) return true", otherName);
  result.addStatement("if (!($N instanceof $T)) return false", otherName, javaType);

  result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
  result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
  for (Field field : fields) {
    String fieldName = localNameAllocator.get(field);
    if (field.isRequired() || field.isRepeated() || field.type().isMap()) {
      result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
    } else {
      result.addCode("\n&& $1T.equals($2L, $3N.$2L)", Internal.class, fieldName, oName);
    }
  }
  result.addCode(";\n$]");

  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:36,代码来源:JavaGenerator.java

示例6: messageHashCode

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type) {
  NameAllocator localNameAllocator = nameAllocator.clone();

  String resultName = localNameAllocator.newName("result");
  MethodSpec.Builder result = MethodSpec.methodBuilder("hashCode")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .returns(int.class);

  List<Field> fields = type.fieldsAndOneOfFields();
  if (fields.isEmpty()) {
    result.addStatement("return unknownFields().hashCode()");
    return result.build();
  }

  result.addStatement("int $N = super.hashCode", resultName);
  result.beginControlFlow("if ($N == 0)", resultName);
  result.addStatement("$N = unknownFields().hashCode()", resultName);
  for (Field field : fields) {
    String fieldName = localNameAllocator.get(field);
    result.addCode("$1N = $1N * 37 + ", resultName);
    if (field.isRepeated() || field.isRequired() || field.type().isMap()) {
      result.addStatement("$L.hashCode()", fieldName);
    } else {
      result.addStatement("($1L != null ? $1L.hashCode() : 0)", fieldName);
    }
  }
  result.addStatement("super.hashCode = $N", resultName);
  result.endControlFlow();
  result.addStatement("return $N", resultName);
  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:33,代码来源:JavaGenerator.java

示例7: messageToString

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec messageToString(NameAllocator nameAllocator, MessageType type) {
  NameAllocator localNameAllocator = nameAllocator.clone();

  MethodSpec.Builder result = MethodSpec.methodBuilder("toString")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .returns(String.class);

  String builderName = localNameAllocator.newName("builder");
  result.addStatement("$1T $2N = new $1T()", StringBuilder.class, builderName);

  for (Field field : type.fieldsAndOneOfFields()) {
    String fieldName = nameAllocator.get(field);
    if (field.isRepeated() || field.type().isMap()) {
      result.addCode("if (!$N.isEmpty()) ", fieldName);
    } else if (!field.isRequired()) {
      result.addCode("if ($N != null) ", fieldName);
    }
    if (field.isRedacted()) {
      result.addStatement("$N.append(\", $N=██\")", builderName, field.name());
    } else {
      result.addStatement("$N.append(\", $N=\").append($L)", builderName, field.name(),
          fieldName);
    }
  }

  result.addStatement("return builder.replace(0, 2, \"$L{\").append('}').toString()",
      type.type().simpleName());

  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:32,代码来源:JavaGenerator.java

示例8: builderNoArgsConstructor

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec builderNoArgsConstructor(NameAllocator nameAllocator, MessageType type) {
  MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC);
  for (Field field : type.fieldsAndOneOfFields()) {
    String fieldName = nameAllocator.get(field);
    CodeBlock initialValue = initialValue(field);
    if (initialValue != null) {
      result.addStatement("$L = $L", fieldName, initialValue);
    }
  }
  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:12,代码来源:JavaGenerator.java

示例9: newBuilder

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
  NameAllocator localNameAllocator = nameAllocator.clone();

  String builderName = localNameAllocator.newName("builder");
  ClassName javaType = (ClassName) typeName(message.type());
  ClassName builderJavaType = javaType.nestedClass("Builder");

  MethodSpec.Builder result = MethodSpec.methodBuilder("newBuilder")
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .returns(builderJavaType)
      .addStatement("$1T $2L = new $1T()", builderJavaType, builderName);

  List<Field> fields = message.fieldsAndOneOfFields();
  for (Field field : fields) {
    String fieldName = localNameAllocator.get(field);
    if (field.isRepeated() || field.type().isMap()) {
      result.addStatement("$1L.$2L = $3T.copyOf($2S, $2L)", builderName, fieldName,
          Internal.class);
    } else {
      result.addStatement("$1L.$2L = $2L", builderName, fieldName);
    }
  }

  result.addStatement("$L.addUnknownFields(unknownFields())", builderName);
  result.addStatement("return $L", builderName);
  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:29,代码来源:JavaGenerator.java

示例10: setter

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec setter(
    NameAllocator nameAllocator, TypeName builderType, OneOf oneOf, Field field) {
  TypeName javaType = fieldType(field);
  String fieldName = nameAllocator.get(field);

  MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName)
      .addModifiers(PUBLIC)
      .addParameter(javaType, fieldName)
      .returns(builderType);

  if (!field.documentation().isEmpty()) {
    result.addJavadoc("$L\n", sanitizeJavadoc(field.documentation()));
  }

  if (field.isDeprecated()) {
    result.addAnnotation(Deprecated.class);
  }

  if (field.isRepeated() || field.type().isMap()) {
    result.addStatement("$T.checkElementsNotNull($L)", Internal.class, fieldName);
  }
  result.addStatement("this.$L = $L", fieldName, fieldName);

  if (oneOf != null) {
    for (Field other : oneOf.fields()) {
      if (field != other) {
        result.addStatement("this.$L = null", nameAllocator.get(other));
      }
    }
  }

  result.addStatement("return this");
  return result.build();
}
 
开发者ID:square,项目名称:wire,代码行数:35,代码来源:JavaGenerator.java

示例11: addNecessaryParameterVariables

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
@SuppressWarnings("PMD.CyclomaticComplexity") private void addNecessaryParameterVariables(final NameAllocator nameAllocator, final MethodSpec.Builder result, final Set<Parameter> existingParameters, final ParameterList parameterList) {
    for (final Parameter parameter : parameterList) {
        final boolean isNotPresent = !existingParameters.contains(parameter);

        if (isNotPresent) {
            String parameterName;

            try {
                parameterName = nameAllocator.get(parameter.hashCode()); // The only way to know whether a name has already been generated for that hashCode or not
            } catch (final IllegalArgumentException ignore) {
                parameterName = nameAllocator.newName(parameter.getName(), parameter.hashCode());
            }

            if (AnnotatedParameter.INTENT_DATA == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getIntentData$L($L)", AnnotatedParameter.INTENT_DATA.type, parameterName, INTENT_HELPER, parameter.preCondition.getSuffix(), Parameter.INTENT);
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BOOLEAN == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraBoolean($L, $S, $L)", AnnotatedParameter.BOOLEAN.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BYTE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraByte($L, $S, (byte) $L)", AnnotatedParameter.BYTE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.CHAR == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraChar($L, $S, (char) $L)", AnnotatedParameter.CHAR.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.DOUBLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraDouble($L, $S, $L)", AnnotatedParameter.DOUBLE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.FLOAT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraFloat($L, $S, $Lf)", AnnotatedParameter.FLOAT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.INT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraInt($L, $S, $L)", AnnotatedParameter.INT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.LONG == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraLong($L, $S, $LL)", AnnotatedParameter.LONG.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.SHORT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraShort($L, $S, (short) $L)", AnnotatedParameter.SHORT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.STRING == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraString($L, $S, $S)", AnnotatedParameter.STRING.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.CHAR_SEQUENCE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraCharSequence($L, $S, $L)", AnnotatedParameter.CHAR_SEQUENCE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BUNDLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraBundle($L, $S, $L)", AnnotatedParameter.BUNDLE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.SERIALIZABLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraSerializable($L, $S, $L)", parameter.className, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.PARCELABLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraParcelable($L, $S, $L)", parameter.className, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            }
        }
    }
}
 
开发者ID:vanniktech,项目名称:OnActivityResult,代码行数:60,代码来源:ActivityResultClass.java

示例12: fieldName

import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private String fieldName(ProtoType type, Field field) {
  MessageType messageType = (MessageType) schema.getType(type);
  NameAllocator names = nameAllocators.getUnchecked(messageType);
  return names.get(field);
}
 
开发者ID:square,项目名称:wire,代码行数:6,代码来源:JavaGenerator.java


注:本文中的com.squareup.javapoet.NameAllocator.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。