Search in sources :

Example 1 with ArrayTypeName

use of com.squareup.javapoet.ArrayTypeName 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 2 with ArrayTypeName

use of com.squareup.javapoet.ArrayTypeName in project tiger by google.

the class Utils method getSourceCodeName.

/**
   * Returns "com_Foo" for com.Foo, "com_Foo_com_Bar_com_Baz" for Foo<Bar, Baz>.
   * upper_bounds_UpperBound_Foo for "? extends Foo" and
   * lower_bounds_LowerBound_Foo for "? super Foo". Assuming raw types are not
   * used, there will be not conflicts.
   */
public static String getSourceCodeName(TypeName typeName) {
    Preconditions.checkNotNull(typeName);
    if (typeName.isPrimitive()) {
        return typeName.toString();
    } else if (typeName instanceof ClassName) {
        return getClassCanonicalName((ClassName) typeName).replace(".", "_");
    } else if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName p = (ParameterizedTypeName) typeName;
        StringBuilder builder = new StringBuilder(getSourceCodeName(p.rawType));
        for (TypeName t : p.typeArguments) {
            builder.append("_").append(getSourceCodeName(t));
        }
        return builder.toString();
    } else if (typeName instanceof WildcardTypeName) {
        WildcardTypeName w = (WildcardTypeName) typeName;
        if (w.upperBounds.size() > 0) {
            return "upper_bounds_" + getSourceCodeName(w.upperBounds.get(0));
        } else {
            Preconditions.checkState(w.lowerBounds.size() > 0);
            return "lower_bounds_" + getSourceCodeName(w.lowerBounds.get(0));
        }
    } else if (typeName instanceof ArrayTypeName) {
        ArrayTypeName arrayTypeName = (ArrayTypeName) typeName;
        return "ArrayOf" + getSourceCodeName(arrayTypeName.componentType);
    }
    throw new RuntimeException("Unexpected type: " + typeName);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 3 with ArrayTypeName

use of com.squareup.javapoet.ArrayTypeName 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)

Aggregations

ArrayTypeName (com.squareup.javapoet.ArrayTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 Builder (com.squareup.javapoet.MethodSpec.Builder)2 ClassName (com.squareup.javapoet.ClassName)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1