Search in sources :

Example 1 with Builder

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

the class EpoxyProcessor method generateToString.

private MethodSpec generateToString(ClassToGenerateInfo helperClass) {
    Builder builder = MethodSpec.methodBuilder("toString").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(String.class);
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("\"%s{\" +\n", helperClass.getGeneratedName().simpleName()));
    boolean first = true;
    for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
        String attributeName = attributeInfo.getName();
        if (first) {
            sb.append(String.format("\"%s=\" + %s +\n", attributeName, attributeName));
            first = false;
        } else {
            sb.append(String.format("\", %s=\" + %s +\n", attributeName, attributeName));
        }
    }
    sb.append("\"}\" + super.toString()");
    return builder.addStatement("return $L", sb.toString()).build();
}
Also used : Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 2 with Builder

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

the class EpoxyProcessor method generateMethodsReturningClassType.

private Iterable<MethodSpec> generateMethodsReturningClassType(ClassToGenerateInfo info) {
    List<MethodSpec> methods = new ArrayList<>(info.getMethodsReturningClassType().size());
    for (MethodInfo methodInfo : info.getMethodsReturningClassType()) {
        Builder builder = MethodSpec.methodBuilder(methodInfo.name).addModifiers(methodInfo.modifiers).addParameters(methodInfo.params).addAnnotation(Override.class).varargs(methodInfo.varargs).returns(info.getParameterizedGeneratedName());
        StringBuilder statementBuilder = new StringBuilder(String.format("super.%s(", methodInfo.name));
        generateParams(statementBuilder, methodInfo.params);
        methods.add(builder.addStatement(statementBuilder.toString()).addStatement("return this").build());
    }
    return methods;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.MethodSpec.Builder) ArrayList(java.util.ArrayList) MethodInfo(com.airbnb.epoxy.ClassToGenerateInfo.MethodInfo)

Example 3 with Builder

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

the class EpoxyProcessor method generateConstructors.

/** Include any constructors that are in the super class. */
private Iterable<MethodSpec> generateConstructors(ClassToGenerateInfo info) {
    List<MethodSpec> constructors = new ArrayList<>(info.getConstructors().size());
    for (ConstructorInfo constructorInfo : info.getConstructors()) {
        Builder builder = MethodSpec.constructorBuilder().addModifiers(constructorInfo.modifiers).addParameters(constructorInfo.params).varargs(constructorInfo.varargs);
        StringBuilder statementBuilder = new StringBuilder("super(");
        generateParams(statementBuilder, constructorInfo.params);
        constructors.add(builder.addStatement(statementBuilder.toString()).build());
    }
    return constructors;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.MethodSpec.Builder) ArrayList(java.util.ArrayList) ConstructorInfo(com.airbnb.epoxy.ClassToGenerateInfo.ConstructorInfo)

Example 4 with Builder

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

the class EpoxyProcessor method generateEquals.

private MethodSpec generateEquals(ClassToGenerateInfo helperClass) {
    Builder builder = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(boolean.class).addParameter(Object.class, "o").beginControlFlow("if (o == this)").addStatement("return true").endControlFlow().beginControlFlow("if (!(o instanceof $T))", helperClass.getGeneratedName()).addStatement("return false").endControlFlow().beginControlFlow("if (!super.equals(o))").addStatement("return false").endControlFlow().addStatement("$T that = ($T) o", helperClass.getGeneratedName(), helperClass.getGeneratedName());
    for (AttributeInfo attributeInfo : helperClass.getAttributeInfo()) {
        TypeName type = attributeInfo.getType();
        if (!attributeInfo.useInHash() && type.isPrimitive()) {
            continue;
        }
        String name = attributeInfo.getName();
        if (attributeInfo.useInHash()) {
            if (type == FLOAT) {
                builder.beginControlFlow("if (Float.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
            } else if (type == DOUBLE) {
                builder.beginControlFlow("if (Double.compare(that.$L, $L) != 0)", name, name).addStatement("return false").endControlFlow();
            } else if (type.isPrimitive()) {
                builder.beginControlFlow("if ($L != that.$L)", name, name).addStatement("return false").endControlFlow();
            } else if (type instanceof ArrayTypeName) {
                builder.beginControlFlow("if (!$T.equals($L, that.$L))", TypeName.get(Arrays.class), name, name).addStatement("return false").endControlFlow();
            } else {
                builder.beginControlFlow("if ($L != null ? !$L.equals(that.$L) : that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
            }
        } else {
            builder.beginControlFlow("if ($L != null && that.$L == null" + " || $L == null && that.$L != null)", name, name, name, name).addStatement("return false").endControlFlow();
        }
    }
    return builder.addStatement("return true").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 5 with Builder

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

the class GlideGenerator method overrideGlideWithMethod.

private MethodSpec overrideGlideWithMethod(String packageName, TypeSpec generatedRequestManager, ExecutableElement methodToOverride) {
    ClassName generatedRequestManagerClassName = ClassName.get(packageName, generatedRequestManager.name);
    List<ParameterSpec> parameters = ProcessorUtil.getParameters(methodToOverride);
    Preconditions.checkArgument(parameters.size() == 1, "Expected size of 1, but got %s", methodToOverride);
    ParameterSpec parameter = parameters.iterator().next();
    Builder builder = MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride)).addParameters(parameters).returns(generatedRequestManagerClassName).addStatement("return ($T) $T.$N($L)", generatedRequestManagerClassName, glideType, methodToOverride.getSimpleName().toString(), parameter.name);
    return addReturnAnnotations(builder, methodToOverride).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) Builder(com.squareup.javapoet.MethodSpec.Builder) ClassName(com.squareup.javapoet.ClassName)

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