use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method abstractAdapterName.
/**
* Returns the Java type of the abstract adapter class generated for a corresponding {@code
* protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
*/
public ClassName abstractAdapterName(ProtoType protoType) {
TypeName profileJavaName = profile.getTarget(protoType);
if (profileJavaName == null)
return null;
ClassName javaName = nameToJavaName.get(protoType);
return javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method fieldInitializer.
private CodeBlock fieldInitializer(ProtoType type, Object value) {
TypeName javaType = typeName(type);
if (value instanceof List) {
CodeBlock.Builder builder = CodeBlock.builder();
builder.add("$T.asList(", Arrays.class);
boolean first = true;
for (Object o : (List<?>) value) {
if (!first)
builder.add(",");
first = false;
builder.add("\n$>$>$L$<$<", fieldInitializer(type, o));
}
builder.add(")");
return builder.build();
} else if (value instanceof Map) {
CodeBlock.Builder builder = CodeBlock.builder();
builder.add("new $T.Builder()", javaType);
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
ProtoMember protoMember = (ProtoMember) entry.getKey();
Field field = schema.getField(protoMember);
CodeBlock valueInitializer = fieldInitializer(field.type(), entry.getValue());
builder.add("\n$>$>.$L($L)$<$<", fieldName(type, field), valueInitializer);
}
builder.add("\n$>$>.build()$<$<");
return builder.build();
} else if (javaType.equals(TypeName.BOOLEAN.box())) {
return CodeBlock.of("$L", value != null ? value : false);
} else if (javaType.equals(TypeName.INT.box())) {
return CodeBlock.of("$L", value != null ? new BigDecimal(String.valueOf(value)).intValue() : 0);
} else if (javaType.equals(TypeName.LONG.box())) {
return CodeBlock.of("$LL", value != null ? Long.toString(new BigDecimal(String.valueOf(value)).longValue()) : 0L);
} else if (javaType.equals(TypeName.FLOAT.box())) {
return CodeBlock.of("$Lf", value != null ? String.valueOf(value) : 0f);
} else if (javaType.equals(TypeName.DOUBLE.box())) {
return CodeBlock.of("$Ld", value != null ? String.valueOf(value) : 0d);
} else if (javaType.equals(STRING)) {
return CodeBlock.of("$S", value != null ? (String) value : "");
} else if (javaType.equals(BYTE_STRING)) {
if (value == null) {
return CodeBlock.of("$T.EMPTY", ByteString.class);
} else {
return CodeBlock.of("$T.decodeBase64($S)", ByteString.class, ByteString.of(String.valueOf(value).getBytes(Charsets.ISO_8859_1)).base64());
}
} else if (isEnum(type) && value != null) {
return CodeBlock.of("$T.$L", javaType, value);
} else {
throw new IllegalStateException(type + " is not an allowed scalar type");
}
}
use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method fieldType.
private TypeName fieldType(Field field) {
ProtoType type = field.type();
if (type.isMap()) {
return ParameterizedTypeName.get(ClassName.get(Map.class), typeName(type.keyType()), typeName(type.valueType()));
}
TypeName messageType = typeName(type);
return field.isRepeated() ? listOf(messageType) : messageType;
}
use of com.squareup.javapoet.TypeName in project graylog2-server by Graylog2.
the class WithBeanGetterExtension method generateGetterMethod.
private MethodSpec generateGetterMethod(String name, ExecutableElement element) {
final TypeName returnType = ClassName.get(element.getReturnType());
final String prefix = isBoolean(returnType) ? "is" : "get";
final String getterName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
final MethodSpec.Builder builder = MethodSpec.methodBuilder(prefix + getterName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(JsonIgnore.class).build()).addStatement("return $N()", name).returns(returnType);
// Copy all annotations but @JsonProperty, @JsonIgnore, and @Override to the new method.
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
final TypeName annotationType = ClassName.get(annotationMirror.getAnnotationType());
if (SKIP_ANNOTATIONS.contains(annotationType)) {
continue;
}
builder.addAnnotation(AnnotationSpec.get(annotationMirror));
}
return builder.build();
}
use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.
the class ButterKnifeProcessor method parseBindView.
private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Start by verifying common generated code restrictions.
boolean hasError = isInaccessibleViaGeneratedCode(BindView.class, "fields", element) || isBindingInWrongPackage(BindView.class, element);
// Verify that the target type extends from View.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
Name qualifiedName = enclosingElement.getQualifiedName();
Name simpleName = element.getSimpleName();
if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
if (elementType.getKind() == TypeKind.ERROR) {
note(element, "@%s field with unresolved type (%s) " + "must elsewhere be generated as a View or interface. (%s.%s)", BindView.class.getSimpleName(), elementType, qualifiedName, simpleName);
} else {
error(element, "@%s fields must extend from View or be an interface. (%s.%s)", BindView.class.getSimpleName(), qualifiedName, simpleName);
hasError = true;
}
}
if (hasError) {
return;
}
// Assemble information on the field.
int id = element.getAnnotation(BindView.class).value();
BindingSet.Builder builder = builderMap.get(enclosingElement);
QualifiedId qualifiedId = elementToQualifiedId(element, id);
if (builder != null) {
String existingBindingName = builder.findExistingBindingName(getId(qualifiedId));
if (existingBindingName != null) {
error(element, "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)", BindView.class.getSimpleName(), id, existingBindingName, enclosingElement.getQualifiedName(), element.getSimpleName());
return;
}
} else {
builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
}
String name = simpleName.toString();
TypeName type = TypeName.get(elementType);
boolean required = isFieldRequired(element);
builder.addField(getId(qualifiedId), new FieldViewBinding(name, type, required));
// Add the type-erased version to the valid binding targets set.
erasedTargetNames.add(enclosingElement);
}
Aggregations