Skip to content

Commit 8cb5d7b

Browse files
committed
refactoring
1 parent 3a900fb commit 8cb5d7b

File tree

8 files changed

+85
-79
lines changed

8 files changed

+85
-79
lines changed

core/src/main/java/net/jbock/coerce/BasicInfo.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010

1111
public final class BasicInfo {
1212

13+
public final boolean repeatable;
14+
1315
public final TypeMirror returnType;
1416

1517
private final String paramName;
1618

17-
private BasicInfo(TypeMirror returnType, String paramName) {
19+
private BasicInfo(boolean repeatable, TypeMirror returnType, String paramName) {
20+
this.repeatable = repeatable;
1821
this.returnType = returnType;
1922
this.paramName = paramName;
2023
}
2124

22-
static BasicInfo create(TypeMirror returnType, String paramName) {
23-
return new BasicInfo(returnType, snakeToCamel(paramName));
25+
static BasicInfo create(boolean repeatable, TypeMirror returnType, String paramName) {
26+
return new BasicInfo(repeatable, returnType, snakeToCamel(paramName));
2427
}
2528

2629
public String paramName() {

core/src/main/java/net/jbock/coerce/Coercion.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import com.squareup.javapoet.FieldSpec;
55
import com.squareup.javapoet.ParameterSpec;
66
import com.squareup.javapoet.TypeName;
7+
import net.jbock.compiler.TypeTool;
78

9+
import javax.lang.model.type.TypeKind;
810
import javax.lang.model.type.TypeMirror;
911
import java.util.Optional;
12+
import java.util.stream.Collectors;
1013

1114
public final class Coercion {
1215

@@ -20,7 +23,7 @@ public final class Coercion {
2023
private final CodeBlock initMapper;
2124

2225
// helper.build
23-
private final CodeBlock initCollector;
26+
private final Optional<CodeBlock> initCollector;
2427

2528
// impl constructor
2629
private final CodeBlock extract;
@@ -31,32 +34,38 @@ public final class Coercion {
3134
// impl
3235
private final FieldSpec field;
3336

37+
private final boolean isDefaultCollector;
38+
3439
private Coercion(
3540
Optional<ParameterSpec> collectorParam,
3641
Optional<CodeBlock> mapExpr,
3742
CodeBlock initMapper,
38-
CodeBlock initCollector,
43+
Optional<CodeBlock> initCollector,
3944
CodeBlock extract,
4045
TypeMirror paramType,
41-
FieldSpec field) {
46+
FieldSpec field,
47+
boolean isDefaultCollector) {
4248
this.collectorParam = collectorParam;
4349
this.mapExpr = mapExpr;
4450
this.initMapper = initMapper;
4551
this.initCollector = initCollector;
4652
this.extract = extract;
4753
this.paramType = paramType;
4854
this.field = field;
55+
this.isDefaultCollector = isDefaultCollector;
4956
}
5057

5158
public static Coercion create(
5259
Optional<ParameterSpec> collectorParam,
5360
Optional<CodeBlock> mapExpr,
5461
CodeBlock initMapper,
55-
CodeBlock initCollector,
62+
TypeMirror mapperReturnType,
63+
Optional<CodeBlock> initCollector,
5664
CodeBlock extract,
5765
TypeMirror paramType,
5866
BasicInfo basicInfo) {
59-
return new Coercion(collectorParam, mapExpr, initMapper, initCollector, extract, paramType, basicInfo.fieldSpec());
67+
boolean isDefaultCollector = isDefaultCollector(initCollector, paramType, mapperReturnType);
68+
return new Coercion(collectorParam, mapExpr, initMapper, initCollector, extract, paramType, basicInfo.fieldSpec(), isDefaultCollector);
6069
}
6170

6271
/**
@@ -70,7 +79,10 @@ public CodeBlock initMapper() {
7079
return initMapper;
7180
}
7281

73-
public CodeBlock initCollector() {
82+
public Optional<CodeBlock> initCollector() {
83+
if (skipMapCollect()) {
84+
return Optional.empty();
85+
}
7486
return initCollector;
7587
}
7688

@@ -86,26 +98,41 @@ public CodeBlock extract() {
8698
return extract;
8799
}
88100

89-
90101
public Optional<ParameterSpec> collectorParam() {
102+
if (skipMapCollect()) {
103+
return Optional.empty();
104+
}
91105
return collectorParam;
92106
}
93107

94108
public Optional<CodeBlock> collectExpr() {
95-
if (isDefaultCollector()) {
96-
return Optional.of(CollectorInfo.standardCollectorInit());
109+
if (skipMapCollect()) {
110+
return Optional.empty();
111+
}
112+
if (isDefaultCollector) {
113+
return Optional.of(CodeBlock.of("$T.toList()", Collectors.class));
97114
}
98115
if (!collectorParam.isPresent()) {
99116
return Optional.empty();
100117
}
101-
return Optional.of(CodeBlock.builder().add("$N", collectorParam.get()).build());
118+
return Optional.of(CodeBlock.of("$N", collectorParam.get()));
102119
}
103120

104121
public boolean skipMapCollect() {
105-
return !mapExpr.isPresent() && isDefaultCollector();
122+
return !mapExpr.isPresent() && isDefaultCollector;
106123
}
107124

108-
public boolean isDefaultCollector() {
109-
return initCollector.equals(CollectorInfo.standardCollectorInit());
125+
private static boolean isDefaultCollector(
126+
Optional<CodeBlock> initCollector,
127+
TypeMirror paramType,
128+
TypeMirror mapperReturnType) {
129+
if (initCollector.isPresent()) {
130+
return false;
131+
}
132+
if (mapperReturnType.getKind() != TypeKind.DECLARED) {
133+
return false;
134+
}
135+
TypeTool tool = TypeTool.get();
136+
return tool.isSameType(paramType, tool.listOf(mapperReturnType));
110137
}
111138
}

core/src/main/java/net/jbock/coerce/CoercionProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private Coercion handle(
6464
TypeElement collectorClass,
6565
boolean repeatable,
6666
boolean optional) throws TmpException, SearchHintException {
67-
BasicInfo basicInfo = BasicInfo.create(sourceMethod.getReturnType(), paramName);
67+
BasicInfo basicInfo = BasicInfo.create(repeatable, sourceMethod.getReturnType(), paramName);
6868
boolean auto = mapperClass == null;
6969
if (repeatable) {
7070
if (auto) {
@@ -131,8 +131,7 @@ private Coercion handleRepeatable(
131131
CollectorInfo collectorInfo = collectorInfo(sourceMethod, collectorClass);
132132
MapperClassValidator.checkReturnType(mapperClass, collectorInfo.inputType);
133133
ParameterSpec mapperParam = ParameterSpec.builder(TypeName.get(mapperClass.asType()), snakeToCamel(paramName) + "Mapper").build();
134-
MapperClassValidator.checkReturnType(mapperClass, collectorInfo.inputType);
135-
return MapperCoercion.create(OptionalInfo.simple(collectorInfo.inputType), collectorInfo, mapperParam, mapperClass.asType(), basicInfo);
134+
return MapperCoercion.create(OptionalInfo.simple(collectorInfo.inputType), collectorInfo.collectorType(), mapperParam, mapperClass.asType(), basicInfo);
136135
}
137136

138137
// repeatable without mapper
@@ -149,7 +148,7 @@ private Coercion handleRepeatableAuto(
149148
throw TmpException.create(String.format("Define a mapper for %s", collectorInfo.inputType));
150149
}
151150
OptionalInfo optionalInfo = OptionalInfo.simple(collectorInfo.inputType);
152-
return coercion.getCoercion(basicInfo, optionalInfo, Optional.of(collectorInfo));
151+
return coercion.getCoercion(basicInfo, optionalInfo, collectorInfo.collectorType());
153152
}
154153

155154
private CoercionFactory checkEnum(TypeMirror mirror) throws TmpException {

core/src/main/java/net/jbock/coerce/CollectorInfo.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package net.jbock.coerce;
22

3-
import com.squareup.javapoet.CodeBlock;
4-
53
import javax.lang.model.type.TypeMirror;
64
import java.util.Optional;
7-
import java.util.stream.Collectors;
85

96
public class CollectorInfo {
107

11-
private static final CodeBlock STANDARD_INIT = CodeBlock.builder().add("$T.toList()", Collectors.class).build();
12-
13-
public final TypeMirror inputType;
8+
final TypeMirror inputType;
149

1510
private final Optional<TypeMirror> collectorType;
1611

@@ -27,20 +22,6 @@ static CollectorInfo listCollector(TypeMirror inputType) {
2722
return new CollectorInfo(inputType, Optional.empty());
2823
}
2924

30-
public CodeBlock collectorInit() {
31-
if (!collectorType.isPresent()) {
32-
return STANDARD_INIT;
33-
}
34-
return CodeBlock.builder()
35-
.add("new $T().get()", collectorType.get())
36-
.build();
37-
}
38-
39-
static CodeBlock standardCollectorInit() {
40-
return STANDARD_INIT;
41-
}
42-
43-
// visible for testing
4425
Optional<TypeMirror> collectorType() {
4526
return collectorType;
4627
}

core/src/main/java/net/jbock/coerce/mappers/CoercionFactory.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.squareup.javapoet.WildcardTypeName;
99
import net.jbock.coerce.BasicInfo;
1010
import net.jbock.coerce.Coercion;
11-
import net.jbock.coerce.CollectorInfo;
1211
import net.jbock.coerce.OptionalInfo;
1312
import net.jbock.compiler.TypeTool;
1413

@@ -57,19 +56,19 @@ TypeMirror paramType() {
5756
public final Coercion getCoercion(
5857
BasicInfo basicInfo,
5958
OptionalInfo optionalInfo,
60-
Optional<CollectorInfo> collectorInfo) {
61-
return getCoercion(basicInfo, optionalInfo, collectorInfo, mapExpr(), initMapper());
59+
Optional<TypeMirror> collectorType) {
60+
return getCoercion(basicInfo, optionalInfo, collectorType, mapExpr(), initMapper());
6261
}
6362

6463
private Coercion getCoercion(
6564
BasicInfo basicInfo,
6665
OptionalInfo optionalInfo,
67-
Optional<CollectorInfo> collectorInfo,
66+
Optional<TypeMirror> collectorType,
6867
Optional<CodeBlock> mapExpr,
6968
CodeBlock initMapper) {
7069
CodeBlock extract;
7170
TypeMirror paramType;
72-
if (optionalInfo.optional || collectorInfo.isPresent()) {
71+
if (optionalInfo.optional || basicInfo.repeatable) {
7372
paramType = basicInfo.returnType;
7473
ParameterSpec param = ParameterSpec.builder(TypeName.get(paramType), basicInfo.paramName()).build();
7574
extract = CodeBlock.of("$T.requireNonNull($N)", Objects.class, param);
@@ -79,30 +78,33 @@ private Coercion getCoercion(
7978
extract = extract(param);
8079
}
8180
return Coercion.create(
82-
collectorParam(basicInfo, collectorInfo),
81+
collectorParam(basicInfo, mapperReturnType, collectorType),
8382
mapExpr,
8483
initMapper,
85-
initCollector(collectorInfo),
84+
mapperReturnType,
85+
initCollector(collectorType),
8686
extract,
8787
paramType,
8888
basicInfo);
8989
}
9090

91-
private CodeBlock initCollector(
92-
Optional<CollectorInfo> collectorInfo) {
93-
if (!collectorInfo.isPresent()) {
94-
return CodeBlock.builder().build();
91+
private Optional<CodeBlock> initCollector(
92+
Optional<TypeMirror> collectorType) {
93+
if (!collectorType.isPresent()) {
94+
return Optional.empty();
9595
}
96-
return collectorInfo.get().collectorInit();
96+
return Optional.of(CodeBlock.of(
97+
"new $T().get()", collectorType.get()));
9798
}
9899

99100
private Optional<ParameterSpec> collectorParam(
100101
BasicInfo basicInfo,
101-
Optional<CollectorInfo> collectorInfo) {
102-
if (!collectorInfo.isPresent()) {
102+
TypeMirror mapperReturnType,
103+
Optional<TypeMirror> collectorType) {
104+
if (!collectorType.isPresent()) {
103105
return Optional.empty();
104106
}
105-
TypeName t = TypeName.get(collectorInfo.get().inputType);
107+
TypeName t = TypeName.get(mapperReturnType);
106108
TypeName a = WildcardTypeName.subtypeOf(Object.class);
107109
TypeName r = TypeName.get(basicInfo.returnType);
108110
return Optional.of(ParameterSpec.builder(ParameterizedTypeName.get(

core/src/main/java/net/jbock/coerce/mappers/MapperCoercion.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.squareup.javapoet.TypeName;
88
import net.jbock.coerce.BasicInfo;
99
import net.jbock.coerce.Coercion;
10-
import net.jbock.coerce.CollectorInfo;
1110
import net.jbock.coerce.OptionalInfo;
1211

1312
import javax.lang.model.type.TypeMirror;
@@ -28,15 +27,6 @@ private MapperCoercion(TypeMirror mapperReturnType, ParameterSpec mapperParam, T
2827
this.mapperType = mapperType;
2928
}
3029

31-
public static Coercion create(
32-
OptionalInfo optionalInfo,
33-
CollectorInfo collectorInfo,
34-
ParameterSpec mapperParam,
35-
TypeMirror mapperType,
36-
BasicInfo basicInfo) {
37-
return create(optionalInfo, Optional.of(collectorInfo), mapperParam, mapperType, basicInfo);
38-
}
39-
4030
public static Coercion create(
4131
OptionalInfo optionalInfo,
4232
ParameterSpec mapperParam,
@@ -45,14 +35,14 @@ public static Coercion create(
4535
return create(optionalInfo, Optional.empty(), mapperParam, mapperType, basicInfo);
4636
}
4737

48-
private static Coercion create(
38+
public static Coercion create(
4939
OptionalInfo optionalInfo,
50-
Optional<CollectorInfo> collectorInfo,
40+
Optional<TypeMirror> collectorType,
5141
ParameterSpec mapperParam,
5242
TypeMirror mapperType,
5343
BasicInfo basicInfo) {
5444
return new MapperCoercion(optionalInfo.baseType, mapperParam, mapperType)
55-
.getCoercion(basicInfo, optionalInfo, collectorInfo);
45+
.getCoercion(basicInfo, optionalInfo, collectorType);
5646
}
5747

5848
@Override

core/src/main/java/net/jbock/compiler/Helper.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,10 @@ private MethodSpec buildMethod() {
274274
if (!initMapper.isEmpty()) {
275275
spec.addStatement(initMapper);
276276
}
277-
if (param.paramType == REPEATABLE && !param.coercion().isDefaultCollector()) {
277+
if (param.coercion().collectorParam().isPresent() &&
278+
param.coercion().initCollector().isPresent()) {
278279
ParameterSpec collectorParam = param.coercion().collectorParam().get();
279-
spec.addStatement("$T $N = $L", collectorParam.type, collectorParam, param.coercion().initCollector());
280+
spec.addStatement("$T $N = $L", collectorParam.type, collectorParam, param.coercion().initCollector().get());
280281
}
281282
}
282283

@@ -291,16 +292,15 @@ private MethodSpec buildMethod() {
291292

292293
private CodeBlock extractExpression(Param param) {
293294
CodeBlock.Builder builder = param.paramType.extractExpression(this, param).toBuilder();
294-
if (param.paramType == REPEATABLE && !param.coercion().skipMapCollect()) {
295+
boolean collected = param.paramType == REPEATABLE && !param.coercion().skipMapCollect();
296+
if (collected) {
295297
builder.add(".stream()");
296-
if (param.coercion().mapExpr().isPresent()) {
297-
builder.add(".map($L)", param.coercion().mapExpr().get());
298-
}
299-
builder.add(".collect($L)", param.coercion().collectExpr().orElseThrow(IllegalStateException::new));
300-
} else if (!param.flag) {
301-
if (param.coercion().mapExpr().isPresent()) {
302-
builder.add(".map($L)", param.coercion().mapExpr().get());
303-
}
298+
}
299+
param.coercion().mapExpr().ifPresent(expr ->
300+
builder.add(".map($L)", expr));
301+
if (collected) {
302+
param.coercion().collectExpr().ifPresent(expr ->
303+
builder.add(".collect($L)", expr));
304304
}
305305
if (param.required()) {
306306
builder.add("\n.orElseThrow(() -> new $T($L))", IllegalArgumentException.class,

core/src/main/java/net/jbock/compiler/TypeTool.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public TypeMirror optionalOf(Class<?> type) {
192192
getTypeElement(type).asType());
193193
}
194194

195+
public TypeMirror listOf(TypeMirror type) {
196+
return types.getDeclaredType(getTypeElement(List.class), type);
197+
}
198+
195199
public List<? extends TypeMirror> getDirectSupertypes(TypeMirror mirror) {
196200
return types.directSupertypes(mirror);
197201
}

0 commit comments

Comments
 (0)