Search in sources :

Example 6 with Builder

use of com.squareup.javapoet.MethodSpec.Builder in project glide by bumptech.

the class GlideGenerator method overrideGlideStaticMethod.

private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
    List<ParameterSpec> parameters = ProcessorUtil.getParameters(methodToOverride);
    TypeElement element = (TypeElement) processingEnv.getTypeUtils().asElement(methodToOverride.getReturnType());
    MethodSpec.Builder builder = MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride)).addParameters(parameters);
    addReturnAnnotations(builder, methodToOverride);
    boolean returnsValue = element != null;
    if (returnsValue) {
        builder.returns(ClassName.get(element));
    }
    StringBuilder code = new StringBuilder(returnsValue ? "return " : "");
    code.append("$T.$N(");
    List<Object> args = new ArrayList<>();
    args.add(ClassName.get(glideType));
    args.add(methodToOverride.getSimpleName());
    if (!parameters.isEmpty()) {
        for (ParameterSpec param : parameters) {
            code.append("$L, ");
            args.add(param.name);
        }
        code = new StringBuilder(code.substring(0, code.length() - 2));
    }
    code.append(")");
    builder.addStatement(code.toString(), args.toArray(new Object[0]));
    return builder.build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 7 with Builder

use of com.squareup.javapoet.MethodSpec.Builder in project glide by bumptech.

the class RequestManagerGenerator method generateRequestManagerRequestManagerMethodOverride.

private MethodSpec generateRequestManagerRequestManagerMethodOverride(String generatedPackageName, ExecutableElement method) {
    ClassName generatedRequestManagerName = ClassName.get(generatedPackageName, GENERATED_REQUEST_MANAGER_SIMPLE_NAME);
    Builder returns = ProcessorUtil.overriding(method).addAnnotation(nonNull()).returns(generatedRequestManagerName);
    return returns.addCode(ProcessorUtil.generateCastingSuperCall(generatedRequestManagerName, returns.build())).build();
}
Also used : Builder(com.squareup.javapoet.MethodSpec.Builder) ClassName(com.squareup.javapoet.ClassName)

Example 8 with Builder

use of com.squareup.javapoet.MethodSpec.Builder in project epoxy by airbnb.

the class EpoxyProcessor method generateHashCode.

private MethodSpec generateHashCode(ClassToGenerateInfo helperClass) {
    Builder builder = MethodSpec.methodBuilder("hashCode").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(int.class).addStatement("int result = super.hashCode()");
    for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
        if (!attributeInfo.useInHash()) {
            continue;
        }
        if (attributeInfo.getType() == DOUBLE) {
            builder.addStatement("long temp");
            break;
        }
    }
    for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
        TypeName type = attributeInfo.getType();
        if (!attributeInfo.useInHash() && type.isPrimitive()) {
            continue;
        }
        String name = attributeInfo.getName();
        if (attributeInfo.useInHash()) {
            if ((type == BYTE) || (type == CHAR) || (type == SHORT) || (type == INT)) {
                builder.addStatement("result = 31 * result + $L", name);
            } else if (type == LONG) {
                builder.addStatement("result = 31 * result + (int) ($L ^ ($L >>> 32))", name, name);
            } else if (type == FLOAT) {
                builder.addStatement("result = 31 * result + ($L != +0.0f " + "? Float.floatToIntBits($L) : 0)", name, name);
            } else if (type == DOUBLE) {
                builder.addStatement("temp = Double.doubleToLongBits($L)", name).addStatement("result = 31 * result + (int) (temp ^ (temp >>> 32))");
            } else if (type == BOOLEAN) {
                builder.addStatement("result = 31 * result + ($L ? 1 : 0)", name);
            } else if (type instanceof ArrayTypeName) {
                builder.addStatement("result = 31 * result + Arrays.hashCode($L)", name);
            } else {
                builder.addStatement("result = 31 * result + ($L != null ? $L.hashCode() : 0)", name, name);
            }
        } else {
            builder.addStatement("result = 31 * result + ($L != null ? 1 : 0)", name);
        }
    }
    return builder.addStatement("return result").build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 9 with Builder

use of com.squareup.javapoet.MethodSpec.Builder in project epoxy by airbnb.

the class EpoxyProcessor method generateSetter.

private MethodSpec generateSetter(ClassToGenerateInfo helperClass, AttributeInfo data) {
    String attributeName = data.getName();
    Builder builder = MethodSpec.methodBuilder(attributeName).addModifiers(Modifier.PUBLIC).returns(helperClass.getParameterizedGeneratedName()).addParameter(ParameterSpec.builder(data.getType(), attributeName).addAnnotations(data.getSetterAnnotations()).build()).addStatement("this.$L = $L", attributeName, attributeName);
    if (data.hasSuperSetterMethod()) {
        builder.addStatement("super.$L($L)", attributeName, attributeName);
    }
    return builder.addStatement("return this").build();
}
Also used : Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 10 with Builder

use of com.squareup.javapoet.MethodSpec.Builder in project microservice_framework by CJSCommonPlatform.

the class ActionNameToMediaTypesMapperClassBuilder method constructor.

private MethodSpec constructor(final List<ActionNameMapping> actionNameMappings) {
    final Builder builder = constructorBuilder().addModifiers(PUBLIC);
    actionNameMappings.forEach(actionNameMapping -> addStatement(builder, actionNameMapping));
    return builder.build();
}
Also used : MethodSpec.constructorBuilder(com.squareup.javapoet.MethodSpec.constructorBuilder) Builder(com.squareup.javapoet.MethodSpec.Builder) TypeSpec.classBuilder(com.squareup.javapoet.TypeSpec.classBuilder)

Aggregations

Builder (com.squareup.javapoet.MethodSpec.Builder)11 MethodSpec (com.squareup.javapoet.MethodSpec)4 ArrayList (java.util.ArrayList)3 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)2 ClassName (com.squareup.javapoet.ClassName)2 ParameterSpec (com.squareup.javapoet.ParameterSpec)2 TypeName (com.squareup.javapoet.TypeName)2 ConstructorInfo (com.airbnb.epoxy.ClassToGenerateInfo.ConstructorInfo)1 MethodInfo (com.airbnb.epoxy.ClassToGenerateInfo.MethodInfo)1 MethodSpec.constructorBuilder (com.squareup.javapoet.MethodSpec.constructorBuilder)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeSpec.classBuilder (com.squareup.javapoet.TypeSpec.classBuilder)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1