本文整理汇总了Java中com.squareup.javapoet.NameAllocator.newName方法的典型用法代码示例。如果您正苦于以下问题:Java NameAllocator.newName方法的具体用法?Java NameAllocator.newName怎么用?Java NameAllocator.newName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.javapoet.NameAllocator
的用法示例。
在下文中一共展示了NameAllocator.newName方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTypeAdapters
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(List<JsonProperty> properties,
NameAllocator nameAllocator) {
Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
for (JsonProperty property : properties) {
if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
name = nameAllocator.newName(name, typeName);
typeAdapters.put(property.typeAdapter,
FieldSpec.builder(typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC,
FINAL).initializer("new $T()", typeName).build());
}
}
return ImmutableMap.copyOf(typeAdapters);
}
示例2: getTypeAdapters
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(ImmutableList<Property> properties) {
Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
NameAllocator nameAllocator = new NameAllocator();
nameAllocator.newName("CREATOR");
for (Property property : properties) {
if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
name = nameAllocator.newName(name, typeName);
typeAdapters.put(property.typeAdapter, FieldSpec.builder(
typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC, FINAL)
.initializer("new $T()", typeName).build());
}
}
return ImmutableMap.copyOf(typeAdapters);
}
示例3: getTypeAdapters
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(List<Property> properties) {
Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
NameAllocator nameAllocator = new NameAllocator();
nameAllocator.newName("CREATOR");
for (Property property : properties) {
if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
name = nameAllocator.newName(name, typeName);
typeAdapters.put(property.typeAdapter, FieldSpec.builder(
typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC, FINAL)
.initializer("new $T()", typeName).build());
}
}
return ImmutableMap.copyOf(typeAdapters);
}
示例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();
}
示例5: addColumnAdaptersToMethod
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
public static ImmutableMap<ClassName, String> addColumnAdaptersToMethod(
MethodSpec.Builder method,
List<ColumnProperty> properties) {
Map<ClassName, String> columnAdapters = new LinkedHashMap<>();
NameAllocator nameAllocator = new NameAllocator();
for (ColumnProperty property : properties) {
ClassName adapter = property.columnAdapter();
if (adapter != null && !columnAdapters.containsKey(adapter)) {
String name = nameAllocator.newName(toLowerCase(adapter.simpleName()));
method.addStatement("$1T $2L = new $1T()", adapter, name);
columnAdapters.put(adapter, name);
}
}
return ImmutableMap.copyOf(columnAdapters);
}
示例6: load
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
@Override public NameAllocator load(Type type) throws Exception {
NameAllocator nameAllocator = new NameAllocator();
if (type instanceof MessageType) {
nameAllocator.newName("serialVersionUID", "serialVersionUID");
nameAllocator.newName("ADAPTER", "ADAPTER");
nameAllocator.newName("MESSAGE_OPTIONS", "MESSAGE_OPTIONS");
if (emitAndroid) {
nameAllocator.newName("CREATOR", "CREATOR");
}
ImmutableList<Field> fieldsAndOneOfFields = ((MessageType) type).fieldsAndOneOfFields();
Set<String> collidingNames = collidingFieldNames(fieldsAndOneOfFields);
for (Field field : fieldsAndOneOfFields) {
String suggestion = collidingNames.contains(field.name())
? field.qualifiedName()
: field.name();
nameAllocator.newName(suggestion, field);
}
} else if (type instanceof EnumType) {
nameAllocator.newName("value", "value");
nameAllocator.newName("i", "i");
nameAllocator.newName("reader", "reader");
nameAllocator.newName("writer", "writer");
for (EnumConstant constant : ((EnumType) type).constants()) {
nameAllocator.newName(constant.name(), constant);
}
}
return nameAllocator;
}
示例7: 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();
}
示例8: 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();
}
示例9: 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();
}
示例10: 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();
}
示例11: createPropertyMethod
import com.squareup.javapoet.NameAllocator; //导入方法依赖的package包/类
private MethodSpec createPropertyMethod(Element element) {
if (TypeKind.ARRAY.equals(element.asType().getKind())) {
NameAllocator names = new NameAllocator();
String name = names.newName(element.getSimpleName().toString());
String array = names.newName("array");
String value = names.newName("value");
String size = names.newName("size");
TypeMirror componentType = TypeMapper.asArrayType(element.asType()).getComponentType();
CodeBlock code = CodeBlock.builder()
.beginControlFlow("if ($T.isClient())", ClassNames.GWT_SHARED_HELPER)
.addStatement("JsArray<$T> $L", componentType, array)
.beginControlFlow("if (this.object.$L != null)", name)
.addStatement("Object $L = this.object.$L", value, name)
.addStatement("$L = (JsArray<$T>) $L", array, componentType, value)
.nextControlFlow("else")
.addStatement("$L = new JsArray<>()", array)
.addStatement("Object $L = $L", value, array)
.addStatement("this.object.$L = ($T[]) $L", name, componentType, value)
.endControlFlow()
.beginControlFlow("for (int i = 0; i < $L.length; i++)", name)
.addStatement("$L.push($L[i])", array, name)
.endControlFlow()
.nextControlFlow("else")
.beginControlFlow("if (this.object.$L == null)", name)
.addStatement("this.object.$L = new $T[0]", name, componentType)
.endControlFlow()
.addStatement("this.object.$L = $T.concat("
+ "\n$T.stream(this.object.$L), $T.stream($L))"
+ "\n.toArray($L -> new $T[$L])",
name, Stream.class, Arrays.class, name, Arrays.class, name, size, componentType, size)
.endControlFlow()
.addStatement("return this")
.build();
return MethodSpec.methodBuilder(getWithMethodName(element))
.addModifiers(Modifier.PUBLIC)
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "unchecked")
.build())
.returns(getBuilderName(element.getEnclosingElement()))
.addParameter(ParameterSpec.builder(TypeName.get(element.asType()), element.getSimpleName().toString())
.build())
.varargs()
.addCode(code)
.build();
} else {
return MethodSpec.methodBuilder(getWithMethodName(element))
.addModifiers(Modifier.PUBLIC)
.returns(getBuilderName(element.getEnclosingElement()))
.addParameter(ParameterSpec.builder(TypeName.get(element.asType()), element.getSimpleName().toString())
.build())
.addCode(CodeBlock.builder()
.addStatement("this.object.$L = $L", element.getSimpleName(), element.getSimpleName())
.addStatement("return this")
.build())
.build();
}
}
示例12: 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);
}
}
}
}