Search in sources :

Example 56 with TypeName

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");
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ClassName(com.squareup.javapoet.ClassName)

Example 57 with TypeName

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");
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CacheBuilder(com.google.common.cache.CacheBuilder) CodeBlock(com.squareup.javapoet.CodeBlock) BigDecimal(java.math.BigDecimal) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) ProtoMember(com.squareup.wire.schema.ProtoMember) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 58 with TypeName

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;
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with TypeName

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();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 60 with TypeName

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);
}
Also used : TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) BindString(butterknife.BindString) ClassName(com.squareup.javapoet.ClassName) TypeName(com.squareup.javapoet.TypeName) Name(javax.lang.model.element.Name) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) BindView(butterknife.BindView)

Aggregations

TypeName (com.squareup.javapoet.TypeName)63 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)54 ClassName (com.squareup.javapoet.ClassName)30 MethodSpec (com.squareup.javapoet.MethodSpec)27 TypeSpec (com.squareup.javapoet.TypeSpec)22 TypeMirror (javax.lang.model.type.TypeMirror)17 TypeElement (javax.lang.model.element.TypeElement)16 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)14 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)10 Map (java.util.Map)10 CodeBlock (com.squareup.javapoet.CodeBlock)9 FieldSpec (com.squareup.javapoet.FieldSpec)9 WireField (com.squareup.wire.WireField)9 Field (com.squareup.wire.schema.Field)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)7 List (java.util.List)7 Set (java.util.Set)7 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6 LinkedHashMap (java.util.LinkedHashMap)6