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();
}
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;
}
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;
}
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();
}
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();
}
Aggregations