本文整理汇总了Java中com.squareup.javapoet.NameAllocator类的典型用法代码示例。如果您正苦于以下问题:Java NameAllocator类的具体用法?Java NameAllocator怎么用?Java NameAllocator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NameAllocator类属于com.squareup.javapoet包,在下文中一共展示了NameAllocator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: createFields
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
ImmutableMap<TypeName, FieldSpec> createFields(List<Property> properties) {
ImmutableMap.Builder<TypeName, FieldSpec> fields = ImmutableMap.builder();
ClassName jsonAdapter = ClassName.get(TypeAdapter.class);
Set<TypeName> seenTypes = Sets.newHashSet();
NameAllocator nameAllocator = new NameAllocator();
for (Property property : properties) {
if (!property.shouldDeserialize() && !property.shouldSerialize()) {
continue;
}
TypeName type = property.type.isPrimitive() ? property.type.box() : property.type;
ParameterizedTypeName adp = ParameterizedTypeName.get(jsonAdapter, type);
if (!seenTypes.contains(property.type)) {
fields.put(property.type,
FieldSpec.builder(adp,
nameAllocator.newName(simpleName(property.type)) + "_adapter", PRIVATE, FINAL)
.build());
seenTypes.add(property.type);
}
}
return fields.build();
}
示例4: 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);
}
示例5: 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();
}
示例6: messageAdapterEncode
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
private MethodSpec messageAdapterEncode(NameAllocator nameAllocator, MessageType type,
TypeName javaType, boolean useBuilder) {
MethodSpec.Builder result = MethodSpec.methodBuilder("encode")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addParameter(ProtoWriter.class, "writer")
.addParameter(javaType, "value")
.addException(IOException.class);
for (Field field : type.fieldsAndOneOfFields()) {
int fieldTag = field.tag();
CodeBlock adapter = adapterFor(field);
result.addCode("$L.encodeWithTag(writer, $L, ", adapter, fieldTag)
.addCode((useBuilder ? "value.$L" : "$L(value)"), nameAllocator.get(field))
.addCode(");\n");
}
if (useBuilder) {
result.addStatement("writer.writeBytes(value.unknownFields())");
}
return result.build();
}
示例7: 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);
}
}
示例8: 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();
}
示例9: readValue
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
static CodeBlock readValue(Types types, JsonProperty property, ParameterSpec json,
FieldSpec field, FieldSpec key, NameAllocator nameAllocator) {
//TODO Handle collections.
TypeName type = getTypeNameFromProperty(property, types);
CodeBlock.Builder builder = CodeBlock.builder();
if (type.equals(STRING)) {
builder.addStatement("$N = $N.getString($N)", field, json, key);
} else if (type.equals(TypeName.INT) || type.equals(TypeName.INT.box())) {
builder.addStatement("$N = $N.getInt($N)", field, json, key);
} else if (type.equals(TypeName.DOUBLE) || type.equals(TypeName.DOUBLE.box())) {
builder.addStatement("$N = $N.getDouble($N)", field, json, key);
} else if (type.equals(TypeName.FLOAT) || type.equals(TypeName.FLOAT.box())) {
builder.addStatement("$N = (float) $N.getDouble($N)", field, json, key);
} else if (type.equals(TypeName.BOOLEAN) || type.equals(TypeName.BOOLEAN.box())) {
builder.addStatement("$N = $N.getBoolean($N)", field, json, key);
} else if (type.equals(TypeName.LONG) || type.equals(TypeName.LONG.box())) {
builder.addStatement("$N = $N.getLong($N)", field, json, key);
} else if (type.equals(TypeName.SHORT) || type.equals(TypeName.SHORT.box())) {
builder.addStatement("$N = (short) $N.getInt($N)", field, json, key);
} else if (type.equals(TypeName.BYTE) || type.equals(TypeName.BYTE.box())) {
builder.addStatement("$N = (byte) $N.getInt($N)", field, json, key);
} else if (type.equals(TypeName.CHAR) || type.equals(TypeName.CHAR.box())) {
FieldSpec tempVal =
FieldSpec.builder(String.class, nameAllocator.newName("tempVal"), Modifier.FINAL).build();
builder.addStatement("$T $N = $N.getString($N)", tempVal.type, tempVal, json, key);
builder.beginControlFlow("if(!$N.isEmpty())", tempVal);
builder.addStatement("$N = $N.charAt(0)", field, tempVal);
builder.endControlFlow();
} else if (type.equals(ENUM)) {
builder.addStatement("$N = $T.valueOf($N.getString($N))", field, field.type, json, key);
} else {
throw new IllegalStateException(String.format("supportedType [%s] with not method.", type));
}
return builder.build();
}
示例10: generateClass
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
@Override public String generateClass(Context context, String className, String classToExtend,
boolean isFinal) {
List<JsonProperty> properties = JsonProperty.from(context);
validateProperties(context.processingEnvironment(), properties);
Map<String, TypeName> types = convertPropertiesToTypes(context.properties());
NameAllocator nameAllocator = new NameAllocator();
Map<TypeMirror, FieldSpec> typeAdapters = getTypeAdapters(properties, nameAllocator);
TypeName superClass = TypeVariableName.get(classToExtend);
TypeSpec.Builder subclass = TypeSpec.classBuilder(className)
.superclass(superClass)
.addMethod(generateConstructor(types));
if (generateReadMethod(context)) {
subclass.addMethod(
generateFromJson(context, properties, typeAdapters, nameAllocator.clone()));
}
Optional<ExecutableElement> writeMethod = getJsonWriteMethod(context);
if (writeMethod.isPresent()) {
subclass.addMethod(generateWriteToJson(context, writeMethod.get(), properties, typeAdapters,
nameAllocator.clone()));
}
if (!typeAdapters.isEmpty()) {
for (FieldSpec field : typeAdapters.values()) {
subclass.addField(field);
}
}
if (isFinal) {
subclass.addModifiers(FINAL);
} else {
subclass.addModifiers(ABSTRACT);
}
return JavaFile.builder(context.packageName(), subclass.build()).build().toString();
}
示例11: generateWriteToJson
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
private MethodSpec generateWriteToJson(Context context, ExecutableElement writeMethodElement,
List<JsonProperty> properties, Map<TypeMirror, FieldSpec> typeAdapters,
NameAllocator nameAllocator) {
Set<Modifier> modifierSet = new TreeSet<>(writeMethodElement.getModifiers());
modifierSet.remove(ABSTRACT);
MethodSpec.Builder builder =
MethodSpec.methodBuilder(writeMethodElement.getSimpleName().toString())
.addAnnotation(Override.class)
.addModifiers(modifierSet)
.returns(JSON_OBJ_CLASS_NAME);
FieldSpec json = FieldSpec.builder(JSON_OBJ_CLASS_NAME, nameAllocator.newName("json")).build();
builder.addStatement("$1T $2N = new $1T()", JSON_OBJ_CLASS_NAME, json);
Types types = context.processingEnvironment().getTypeUtils();
for (JsonProperty prop : properties) {
if (prop.typeAdapter != null && typeAdapters.containsKey(prop.typeAdapter)) {
builder.addCode(
JsonGeneratorUtils.writeWithAdapter(typeAdapters.get(prop.typeAdapter), json, prop));
} else {
//TODO Discuss: Is null check needed?
builder.beginControlFlow("try");
if (prop.nullable()) {
builder.beginControlFlow("if ($N() != null)", prop.methodName);
builder.addCode(JsonGeneratorUtils.writeValue(types, prop, json));
builder.endControlFlow();
} else {
builder.addCode(JsonGeneratorUtils.writeValue(types, prop, json));
}
builder.endControlFlow("catch($T e) {}", JSON_EXCEPTION);
}
}
builder.addStatement("return $N", json);
return builder.build();
}
示例12: createValidationMethod
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
private MethodSpec createValidationMethod(TypeName targetClassName,
ImmutableMap<Property, FieldSpec> validators) {
String valueName = "value";
ParameterSpec value = ParameterSpec.builder(targetClassName, valueName)
.build();
MethodSpec.Builder validateMethod = MethodSpec.methodBuilder("validate")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addParameter(value)
.addException(ValidationException.class);
// Go through validators
NameAllocator allocator = new NameAllocator();
validators.entrySet()
.stream()
.filter(entry -> entry.getKey()
.shouldValidate())
.forEach(entry -> {
Property prop = entry.getKey();
FieldSpec validator = entry.getValue();
String name = allocator.newName(entry.getKey().methodName);
validateMethod.addComment("Begin validation for \"$L()\"", prop.methodName)
.addStatement("$T $L = $N.$L()", prop.type, name, value, prop.methodName)
.addCode("\n");
extensions.stream()
.sorted(Comparator.comparing(InspectorExtension::priority))
.filter(e -> e.applicable(prop))
.forEach(e -> {
CodeBlock block = e.generateValidation(prop, name, value);
if (block != null) {
validateMethod.addComment("Validations contributed by $S", e.toString())
.addCode(block);
}
});
validateMethod.addStatement("$N.validate($L)", validator, name)
.addCode("\n");
});
return validateMethod.build();
}
示例13: spec
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
public static FieldSpec spec(NameAllocator nameAllocator, Types types, WeaveDescriptor classDescriptor,
WeaveMethodDescriptor methodDescriptor) {
// overload methods?
String method = methodDescriptor.name();
TypeName targetType = TypeName.get(classDescriptor.element().asType());
TypeName returnType = TypeName.get(types.erasure(methodDescriptor.element().getReturnType()));
List<? extends VariableElement> parameters = methodDescriptor.element().getParameters();
FieldSpec.Builder pointcutBuilder = FieldSpec.builder(StaticPointcut.class,
nameAllocator.newName(method + "Pointcut"));
pointcutBuilder.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
StringBuilder format = new StringBuilder();
format.append("$T.create($S, $T.class, $T.class, $T.<Class<?>>asList(");
for (int i = 0, len = parameters.size(); i < len; i++) {
if (i != 0) {
format.append(", ");
}
format.append("$T.class");
}
format.append("))");
Object[] args = new Object[5 + parameters.size()];
args[0] = StaticPointcut.class;
args[1] = method;
args[2] = targetType;
args[3] = returnType;
args[4] = Arrays.class;
for (int i = 0; i < parameters.size(); i++) {
args[i + 5] = TypeName.get(types.erasure(parameters.get(i).asType()));
}
pointcutBuilder.initializer(format.toString(), args);
return pointcutBuilder.build();
}
示例14: renderConstValue
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
CodeBlock renderConstValue(
final CodeBlock.Builder block,
final NameAllocator allocator,
final AtomicInteger scope,
final ThriftType type,
final ConstValueElement value) {
return type.accept(new ConstRenderingVisitor(block, allocator, scope, type, value));
}
示例15: ConstRenderingVisitor
import com.squareup.javapoet.NameAllocator; //导入依赖的package包/类
ConstRenderingVisitor(
CodeBlock.Builder block,
NameAllocator allocator,
AtomicInteger scope,
ThriftType type,
ConstValueElement value) {
this.block = block;
this.allocator = allocator;
this.scope = scope;
this.type = type;
this.value = value;
}