Skip to content

Commit 26ebafe

Browse files
committed
#189 - Format and refactor extract methods only
1 parent d2ed924 commit 26ebafe

File tree

7 files changed

+124
-146
lines changed

7 files changed

+124
-146
lines changed

jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,11 @@ public boolean hasJsonAnnotation() {
139139

140140
@Override
141141
public void read() {
142-
143142
Optional.ofNullable(constructor)
144-
.ifPresent(
145-
c -> {
146-
var enclosing = (TypeElement) c.element().getEnclosingElement();
147-
148-
importTypes.add(enclosing.getQualifiedName().toString());
149-
});
143+
.ifPresent(c -> {
144+
var enclosing = (TypeElement) c.element().getEnclosingElement();
145+
importTypes.add(enclosing.getQualifiedName().toString());
146+
});
150147

151148
importTypes.add(Util.shortName(type));
152149
for (final FieldReader field : allFields) {
@@ -467,24 +464,23 @@ private void writeFromJsonImplementation(Append writer, String varName) {
467464

468465
private void writeJsonBuildResult(Append writer, String varName) {
469466
writer.append(" // build and return %s", shortName).eol();
470-
if (constructor != null) {
471-
writer.append(" %s _$%s = " + constructor.creationString(), shortName, varName);
472-
473-
final List<MethodReader.MethodParam> params = constructor.getParams();
474-
for (int i = 0, size = params.size(); i < size; i++) {
475-
if (i > 0) {
476-
writer.append(", ");
467+
if (constructor == null) {
468+
writer.append(" %s _$%s = new %s(", shortName, varName, shortName);
469+
} else {
470+
writer.append(" %s _$%s = " + constructor.creationString(), shortName, varName);
471+
final List<MethodReader.MethodParam> params = constructor.getParams();
472+
for (int i = 0, size = params.size(); i < size; i++) {
473+
if (i > 0) {
474+
writer.append(", ");
475+
}
476+
final var name = params.get(i).name();
477+
// append increasing numbers to constructor params sharing names with other subtypes
478+
final var frequency = frequencyMap.compute(name, (k, v) -> v == null ? 0 : v + 1);
479+
// assuming name matches field here?
480+
writer.append(constructorParamName(name + (frequency == 0 ? "" : frequency.toString())));
477481
}
478-
final var name = params.get(i).name();
479-
// append increasing numbers to constructor params sharing names with other subtypes
480-
final var frequency = frequencyMap.compute(name, (k, v) -> v == null ? 0 : v + 1);
481-
// assuming name matches field here?
482-
writer.append(constructorParamName(name + (frequency == 0 ? "" : frequency.toString())));
483482
}
484-
} else {
485-
writer.append(" %s _$%s = new %s(", shortName, varName, shortName);
486-
}
487-
writer.append(");").eol();
483+
writer.append(");").eol();
488484
for (final FieldReader allField : allFields) {
489485
if (allField.includeFromJson()) {
490486
frequencyMap.compute(allField.fieldName(), (k, v) -> v == null ? 0 : v + 1);

jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import javax.lang.model.element.ExecutableElement;
55
import javax.lang.model.element.Modifier;
66
import javax.lang.model.element.VariableElement;
7-
87
import java.util.*;
98

109
final class FieldReader {
@@ -24,22 +23,23 @@ final class FieldReader {
2423
private boolean isCreatorParam;
2524

2625
FieldReader(
27-
Element element,
28-
NamingConvention namingConvention,
29-
TypeSubTypeMeta subType,
30-
List<String> genericTypeParams,
31-
Integer frequency) {
26+
Element element,
27+
NamingConvention namingConvention,
28+
TypeSubTypeMeta subType,
29+
List<String> genericTypeParams,
30+
Integer frequency) {
3231

3332
this(element, namingConvention, subType, genericTypeParams, frequency, false);
3433
}
3534

3635
FieldReader(
37-
Element element,
38-
NamingConvention namingConvention,
39-
TypeSubTypeMeta subType,
40-
List<String> genericTypeParams,
41-
Integer frequency,
42-
boolean jsonCreatorPresent) {
36+
Element element,
37+
NamingConvention namingConvention,
38+
TypeSubTypeMeta subType,
39+
List<String> genericTypeParams,
40+
Integer frequency,
41+
boolean jsonCreatorPresent) {
42+
4343
num = frequency == 0 ? "" : frequency.toString();
4444
addSubType(subType);
4545
final PropertyIgnoreReader ignoreReader = new PropertyIgnoreReader(element);
@@ -51,17 +51,15 @@ final class FieldReader {
5151
this.deserialize = isParam || !jsonCreatorPresent && !isMethod && ignoreReader.deserialize();
5252

5353
final var fieldName = element.getSimpleName().toString();
54-
final var publicField =
55-
!isMethod && !isParam && element.getModifiers().contains(Modifier.PUBLIC);
54+
final var publicField = !isMethod && !isParam && element.getModifiers().contains(Modifier.PUBLIC);
5655
final var type = isMethod ? ((ExecutableElement) element).getReturnType() : element.asType();
5756

58-
this.property =
59-
new FieldProperty(type, raw, unmapped, genericTypeParams, publicField, fieldName);
57+
this.property = new FieldProperty(type, raw, unmapped, genericTypeParams, publicField, fieldName);
6058
this.propertyName =
61-
PropertyPrism.getOptionalOn(element)
62-
.map(PropertyPrism::value)
63-
.map(Util::escapeQuotes)
64-
.orElse(namingConvention.from(fieldName));
59+
PropertyPrism.getOptionalOn(element)
60+
.map(PropertyPrism::value)
61+
.map(Util::escapeQuotes)
62+
.orElse(namingConvention.from(fieldName));
6563

6664
initAliases(element);
6765
}
@@ -74,12 +72,12 @@ public void readParam(VariableElement element) {
7472

7573
private void initAliases(Element element) {
7674
var alias =
77-
AliasPrism.getOptionalOn(element)
75+
AliasPrism.getOptionalOn(element)
76+
.map(a -> Util.escapeQuotes(a.value()))
77+
.orElse(
78+
JsonAliasPrism.getOptionalOn(element)
7879
.map(a -> Util.escapeQuotes(a.value()))
79-
.orElse(
80-
JsonAliasPrism.getOptionalOn(element)
81-
.map(a -> Util.escapeQuotes(a.value()))
82-
.orElse(Collections.emptyList()));
80+
.orElse(Collections.emptyList()));
8381

8482
aliases.addAll(alias);
8583
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/MethodReader.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ public boolean isProtected() {
5454
}
5555

5656
public String creationString() {
57-
var shortName =
58-
Util.shortName(((TypeElement) element.getEnclosingElement()).getQualifiedName().toString());
59-
57+
var shortName = Util.shortName(((TypeElement) element.getEnclosingElement()).getQualifiedName().toString());
6058
if (element.getKind() == ElementKind.CONSTRUCTOR) {
6159
return String.format("new %s(", shortName);
6260
}
63-
6461
return String.format("%s.%s(", shortName, element.getSimpleName());
6562
}
6663

@@ -92,8 +89,5 @@ public VariableElement element() {
9289
return element;
9390
}
9491

95-
public VariableElement getElement() {
96-
return element;
97-
}
9892
}
9993
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/TypeReader.java

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import javax.lang.model.element.*;
44
import javax.lang.model.type.TypeMirror;
55
import javax.lang.model.util.ElementFilter;
6-
7-
import io.avaje.jsonb.generator.MethodReader.MethodParam;
8-
96
import java.util.*;
7+
import java.util.function.Supplier;
108
import java.util.stream.Collectors;
119

1210
import static io.avaje.jsonb.generator.APContext.*;
@@ -51,64 +49,58 @@ final class TypeReader {
5149

5250
private boolean optional;
5351

54-
/** Set when the type is known to extend Throwable */
52+
/**
53+
* Set when the type is known to extend Throwable
54+
*/
5555
private boolean extendsThrowable;
5656

5757
private final boolean hasJsonCreator;
5858

59-
TypeReader(
60-
TypeElement baseType,
61-
TypeElement mixInType,
62-
NamingConvention namingConvention,
63-
String typePropertyKey) {
59+
TypeReader(TypeElement baseType, TypeElement mixInType, NamingConvention namingConvention, String typePropertyKey) {
6460
this.baseType = baseType;
6561
this.genericTypeParams = initTypeParams(baseType);
6662
Optional<ExecutableElement> jsonCreator = Optional.empty();
6763
if (mixInType == null) {
68-
6964
this.mixInFields = new HashMap<>();
7065
} else {
71-
7266
jsonCreator =
73-
ElementFilter.methodsIn(mixInType.getEnclosedElements()).stream()
74-
.filter(CreatorPrism::isPresent)
75-
.findFirst();
67+
ElementFilter.methodsIn(mixInType.getEnclosedElements()).stream()
68+
.filter(CreatorPrism::isPresent)
69+
.findFirst();
7670
this.mixInFields =
77-
mixInType.getEnclosedElements().stream()
78-
.filter(e -> e.getKind() == ElementKind.FIELD)
79-
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
71+
mixInType.getEnclosedElements().stream()
72+
.filter(e -> e.getKind() == ElementKind.FIELD)
73+
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
8074
}
8175
this.namingConvention = namingConvention;
8276
this.hasJsonAnnotation = JsonPrism.isPresent(baseType) || importedJson(baseType).isPresent();
8377
this.subTypes = new TypeSubTypeReader(baseType);
8478
this.typePropertyKey = typePropertyKey;
8579

86-
jsonCreator =
87-
jsonCreator.or(
88-
() ->
89-
baseType.getEnclosedElements().stream()
90-
.filter(CreatorPrism::isPresent)
91-
.map(ExecutableElement.class::cast)
92-
.findFirst());
93-
94-
constructor =
95-
jsonCreator
96-
.map(
97-
ex -> {
98-
var mods = ex.getModifiers();
99-
if (ex.getKind() != ElementKind.CONSTRUCTOR
100-
&& !mods.contains(Modifier.STATIC)
101-
&& !mods.contains(Modifier.PUBLIC))
102-
logError(
103-
ex,
104-
"@Json.Creator can only be placed on contructors and static factory methods");
105-
return new MethodReader(ex).read();
106-
})
107-
.orElse(null);
80+
jsonCreator = jsonCreator.or(baseJsonCreator(baseType));
81+
constructor = jsonCreator
82+
.map(TypeReader::readJsonCreator)
83+
.orElse(null);
10884

10985
this.hasJsonCreator = jsonCreator.isPresent();
11086
}
11187

88+
private static MethodReader readJsonCreator(ExecutableElement ex) {
89+
var mods = ex.getModifiers();
90+
if (ex.getKind() != ElementKind.CONSTRUCTOR && !mods.contains(Modifier.STATIC) && !mods.contains(Modifier.PUBLIC)) {
91+
logError(ex, "@Json.Creator can only be placed on contructors and static factory methods");
92+
}
93+
return new MethodReader(ex).read();
94+
}
95+
96+
private static Supplier<Optional<? extends ExecutableElement>> baseJsonCreator(TypeElement baseType) {
97+
return () ->
98+
baseType.getEnclosedElements().stream()
99+
.filter(CreatorPrism::isPresent)
100+
.map(ExecutableElement.class::cast)
101+
.findFirst();
102+
}
103+
112104
private List<String> initTypeParams(TypeElement beanType) {
113105
if (beanType.getTypeParameters().isEmpty()) {
114106
return Collections.emptyList();
@@ -142,14 +134,12 @@ void read(TypeElement type) {
142134

143135
if (hasJsonCreator) {
144136
for (var param : constructor.getParams()) {
145-
146137
var name = param.name();
147138
var element = param.element();
148-
var matchingField =
149-
localFields.stream().filter(f -> f.propertyName().equals(name)).findFirst();
150-
151-
matchingField.ifPresentOrElse(
152-
f -> f.readParam(element), () -> readField(element, localFields));
139+
var matchingField = localFields.stream()
140+
.filter(f -> f.propertyName().equals(name))
141+
.findFirst();
142+
matchingField.ifPresentOrElse(f -> f.readParam(element), () -> readField(element, localFields));
153143
}
154144
}
155145

@@ -186,13 +176,13 @@ private void readField(Element element, List<FieldReader> localFields) {
186176
if (includeField(element)) {
187177
final var frequency = frequency(element.getSimpleName().toString());
188178
localFields.add(
189-
new FieldReader(
190-
element,
191-
namingConvention,
192-
currentSubType,
193-
genericTypeParams,
194-
frequency,
195-
hasJsonCreator));
179+
new FieldReader(
180+
element,
181+
namingConvention,
182+
currentSubType,
183+
genericTypeParams,
184+
frequency,
185+
hasJsonCreator));
196186
}
197187
}
198188

jsonb-inject-plugin/src/main/java/io/avaje/jsonb/inject/DefaultJsonbProvider.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,32 @@
44
import io.avaje.jsonb.Jsonb;
55
import io.avaje.jsonb.stream.BufferRecycleStrategy;
66

7-
/** Plugin for avaje inject that provides a default Jsonb instance. */
7+
/**
8+
* Plugin for avaje inject that provides a default Jsonb instance.
9+
*/
810
public final class DefaultJsonbProvider implements io.avaje.inject.spi.Plugin {
911

1012
@Override
1113
public Class<?>[] provides() {
12-
return new Class<?>[] {Jsonb.class};
14+
return new Class<?>[]{Jsonb.class};
1315
}
1416

1517
@Override
1618
public void apply(BeanScopeBuilder builder) {
17-
builder.provideDefault(
18-
null,
19-
Jsonb.class,
20-
() -> {
21-
var props = builder.propertyPlugin();
19+
builder.provideDefault(null, Jsonb.class, () -> {
20+
var props = builder.propertyPlugin();
2221

23-
return Jsonb.builder()
24-
.failOnUnknown(props.equalTo("jsonb.deserialize.failOnUnknown", "true"))
25-
.mathTypesAsString(props.equalTo("jsonb.serialize.mathTypesAsString", "true"))
26-
.serializeEmpty(props.notEqualTo("jsonb.serialize.empty", "false"))
27-
.serializeNulls(props.equalTo("jsonb.serialize.nulls", "true"))
28-
.bufferRecycling(
29-
props
30-
.get("jsonb.bufferRecycling")
31-
.map(BufferRecycleStrategy::valueOf)
32-
.orElse(BufferRecycleStrategy.HYBRID_POOL))
33-
.build();
34-
});
22+
return Jsonb.builder()
23+
.failOnUnknown(props.equalTo("jsonb.deserialize.failOnUnknown", "true"))
24+
.mathTypesAsString(props.equalTo("jsonb.serialize.mathTypesAsString", "true"))
25+
.serializeEmpty(props.notEqualTo("jsonb.serialize.empty", "false"))
26+
.serializeNulls(props.equalTo("jsonb.serialize.nulls", "true"))
27+
.bufferRecycling(
28+
props
29+
.get("jsonb.bufferRecycling")
30+
.map(BufferRecycleStrategy::valueOf)
31+
.orElse(BufferRecycleStrategy.HYBRID_POOL))
32+
.build();
33+
});
3534
}
3635
}

0 commit comments

Comments
 (0)