Skip to content

Commit

Permalink
Use final class to instantiate
Browse files Browse the repository at this point in the history
Fixes #173
  • Loading branch information
niknetniko committed Sep 13, 2020
1 parent 5949f20 commit a0be36c
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ private static boolean generateExternalAdapter(TypeElement element) {
autoValueClassName,
genericTypeNames,
properties,
context.builder().orElse(null),
context.processingEnvironment(),
context,
adapterClassName);

Optional<AnnotationSpec> generatedAnnotation = GeneratedAnnotationSpecs.generatedAnnotationSpec(
Expand Down Expand Up @@ -419,8 +418,7 @@ private TypeSpec.Builder createJsonAdapter(
ClassName autoValueClassName,
TypeVariableName[] genericTypeNames,
List<Property> properties,
@Nullable BuilderContext builderContext,
ProcessingEnvironment processingEnvironment,
Context context,
String adapterClassName
) {

Expand Down Expand Up @@ -504,7 +502,7 @@ && getTypeIndexInArray(genericTypeNames, prop.type) >= 0) {
.addFields(adapters.values())
.addMethod(constructor.build())
.addMethod(createReadMethod(className, autoValueClassName, autoValueTypeName, properties,
adapters, names, builderContext, processingEnvironment))
adapters, names, context))
.addMethod(createWriteMethod(autoValueTypeName, adapters))
.addMethod(MethodSpec.methodBuilder("toString")
.addAnnotation(Override.class)
Expand Down Expand Up @@ -591,8 +589,7 @@ private MethodSpec createWriteMethod(TypeName autoValueTypeName,

private MethodSpec createReadMethod(ClassName className, ClassName autoValueClassName, TypeName autoValueTypeName,
List<Property> properties, ImmutableMap<Property, FieldSpec> adapters,
List<String> names, @Nullable BuilderContext builderContext,
ProcessingEnvironment processingEnvironment) {
List<String> names, Context context) {
NameAllocator nameAllocator = new NameAllocator();
ParameterSpec reader = ParameterSpec.builder(JsonReader.class, nameAllocator.newName("reader"))
.build();
Expand All @@ -602,10 +599,11 @@ private MethodSpec createReadMethod(ClassName className, ClassName autoValueClas
.returns(autoValueTypeName)
.addParameter(reader)
.addException(IOException.class);
BuilderContext builderContext = context.builder().orElse(null);
// Validate the builderContext if there is one.
if (builderContext != null) {
if (!builderContext.buildMethod().isPresent()) {
processingEnvironment.getMessager()
context.processingEnvironment().getMessager()
.printMessage(
Diagnostic.Kind.ERROR,
"Could not determine the build method. Make sure it is named \"build\".",
Expand All @@ -621,7 +619,7 @@ private MethodSpec createReadMethod(ClassName className, ClassName autoValueClas
.collect(Collectors.toSet());

if (annotatedMethods.size() > 1) {
processingEnvironment.getMessager()
context.processingEnvironment().getMessager()
.printMessage(
Diagnostic.Kind.ERROR,
"Too many @AutoValueMoshiBuilder annotated builder methods.",
Expand All @@ -631,7 +629,7 @@ private MethodSpec createReadMethod(ClassName className, ClassName autoValueClas
}

if (annotatedMethods.isEmpty()) {
processingEnvironment.getMessager().printMessage(
context.processingEnvironment().getMessager().printMessage(
Diagnostic.Kind.ERROR,
"Too many builder methods. Annotate builder method with @AutoValueMoshiBuilder.",
builderMethods.stream().findAny().get()
Expand Down Expand Up @@ -730,7 +728,8 @@ private MethodSpec createReadMethod(ClassName className, ClassName autoValueClas
readMethod.addStatement("return $N.$L", builderField.get(), builderContext.buildMethod().get());
} else {
CodeBlock params = CodeBlock.join(constructorCall, ", ");
readMethod.addStatement("return new $T($L)", className, params);
ClassName constructorName = ClassName.bestGuess(context.finalAutoValueClassName());
readMethod.addStatement("return new $T($L)", constructorName, params);
}
return readMethod.build();
}
Expand Down

0 comments on commit a0be36c

Please sign in to comment.