@@ -64,7 +64,7 @@ private Coercion handle(
64
64
TypeElement collectorClass ,
65
65
boolean repeatable ,
66
66
boolean optional ) throws TmpException , SearchHintException {
67
- BasicInfo basicInfo = BasicInfo .create (repeatable , sourceMethod .getReturnType (), paramName );
67
+ BasicInfo basicInfo = BasicInfo .create (repeatable , optional , sourceMethod .getReturnType (), paramName );
68
68
boolean auto = mapperClass == null ;
69
69
if (repeatable ) {
70
70
if (auto ) {
@@ -87,38 +87,37 @@ private Coercion handleSingleAuto(
87
87
boolean optional ,
88
88
BasicInfo basicInfo ) throws TmpException , SearchHintException {
89
89
TypeMirror returnType = sourceMethod .getReturnType ();
90
- OptionalInfo optionalInfo = findOptionalInfo (returnType , optional );
91
- CoercionFactory enumCoercion = checkEnum (optionalInfo .baseType );
92
- if (enumCoercion != null ) {
93
- return enumCoercion .getCoercion (basicInfo , optionalInfo , Optional .empty ());
94
- }
95
- CoercionFactory factory = StandardCoercions .get (optionalInfo .baseType );
96
- if (factory == null ) {
97
- throw SearchHintException .create ("Unknown parameter type. Define a custom mapper." );
98
- }
99
- if (factory .handlesOptionalPrimitive () && !optional ) {
90
+ Optional <TypeMirror > optionalInfo = findOptionalInfo (returnType , optional );
91
+ CoercionFactory factory = findCoercion (optionalInfo .orElse (returnType ));
92
+ if (optionalInfo .isPresent () && !optional ) {
100
93
throw TmpException .create ("Declare this parameter optional." );
101
94
}
102
- if (optional && ! factory . handlesOptionalPrimitive () && ! optionalInfo . optional ) {
103
- throw TmpException .create ("Wrap the parameter type in Optional" );
95
+ if (! optionalInfo . isPresent () && optional ) {
96
+ throw TmpException .create ("Wrap the parameter type in Optional. " );
104
97
}
105
98
return factory .getCoercion (basicInfo , optionalInfo , Optional .empty ());
106
99
}
107
100
108
- // mapper but not repeatable
101
+ // simple mapper
109
102
private Coercion handleSingle (
110
103
ExecutableElement sourceMethod ,
111
104
String paramName ,
112
105
TypeElement mapperClass ,
113
106
BasicInfo basicInfo ,
114
107
boolean optional ) throws TmpException {
108
+ TypeMirror returnType = sourceMethod .getReturnType ();
115
109
ParameterSpec mapperParam = ParameterSpec .builder (TypeName .get (mapperClass .asType ()), snakeToCamel (paramName ) + "Mapper" ).build ();
116
- OptionalInfo optionalInfo = findOptionalInfo (sourceMethod .getReturnType (), optional );
117
- if (optional && !optionalInfo .optional ) {
118
- throw TmpException .create ("Wrap the parameter type in Optional" );
110
+ Optional <TypeMirror > optionalInfo = findOptionalInfo (returnType , optional );
111
+ if (optionalInfo .isPresent () && !optional ) {
112
+ throw TmpException .create ("Declare this parameter optional." );
113
+ }
114
+ if (!optionalInfo .isPresent () && optional ) {
115
+ throw TmpException .create ("Wrap the parameter type in Optional." );
119
116
}
120
- MapperClassValidator .checkReturnType (mapperClass , optionalInfo .baseType );
121
- return MapperCoercion .create (optionalInfo , mapperParam , mapperClass .asType (), basicInfo );
117
+ TypeMirror boxedReturnType = TypeTool .get ().box (returnType );
118
+ TypeMirror mapperReturnType = optionalInfo .orElse (boxedReturnType );
119
+ MapperClassValidator .checkReturnType (mapperClass , mapperReturnType );
120
+ return MapperCoercion .create (mapperReturnType , optionalInfo , Optional .empty (), mapperParam , mapperClass .asType (), basicInfo );
122
121
}
123
122
124
123
// repeatable with mapper
@@ -131,42 +130,47 @@ private Coercion handleRepeatable(
131
130
CollectorInfo collectorInfo = collectorInfo (sourceMethod , collectorClass );
132
131
MapperClassValidator .checkReturnType (mapperClass , collectorInfo .inputType );
133
132
ParameterSpec mapperParam = ParameterSpec .builder (TypeName .get (mapperClass .asType ()), snakeToCamel (paramName ) + "Mapper" ).build ();
134
- return MapperCoercion .create (OptionalInfo . simple ( collectorInfo .inputType ), collectorInfo .collectorType (), mapperParam , mapperClass .asType (), basicInfo );
133
+ return MapperCoercion .create (collectorInfo .inputType , Optional . empty ( ), collectorInfo .collectorType (), mapperParam , mapperClass .asType (), basicInfo );
135
134
}
136
135
137
136
// repeatable without mapper
138
137
private Coercion handleRepeatableAuto (
139
138
ExecutableElement sourceMethod ,
140
139
TypeElement collectorClass ,
141
- BasicInfo basicInfo ) throws TmpException {
140
+ BasicInfo basicInfo ) throws TmpException , SearchHintException {
142
141
CollectorInfo collectorInfo = collectorInfo (sourceMethod , collectorClass );
143
- CoercionFactory coercion = StandardCoercions .get (collectorInfo .inputType );
144
- if (coercion == null ) {
145
- coercion = checkEnum (collectorInfo .inputType );
142
+ CoercionFactory coercion = findCoercion (collectorInfo .inputType );
143
+ return coercion .getCoercion (basicInfo , Optional .empty (), collectorInfo .collectorType ());
144
+ }
145
+
146
+ private CoercionFactory findCoercion (TypeMirror mirror ) throws TmpException , SearchHintException {
147
+ CoercionFactory standardCoercion = StandardCoercions .get (mirror );
148
+ if (standardCoercion != null ) {
149
+ return standardCoercion ;
146
150
}
147
- if (coercion == null || coercion .handlesOptionalPrimitive ()) {
148
- throw TmpException .create (String .format ("Define a mapper for %s" , collectorInfo .inputType ));
151
+ boolean isEnum = isEnumType (mirror );
152
+ if (!isEnum ) {
153
+ throw SearchHintException .create ("Unknown parameter type. Define a custom mapper." );
149
154
}
150
- OptionalInfo optionalInfo = OptionalInfo .simple (collectorInfo .inputType );
151
- return coercion .getCoercion (basicInfo , optionalInfo , collectorInfo .collectorType ());
155
+ return EnumCoercion .create (mirror );
152
156
}
153
157
154
- private CoercionFactory checkEnum (TypeMirror mirror ) throws TmpException {
158
+ private boolean isEnumType (TypeMirror mirror ) throws TmpException {
155
159
TypeTool tool = TypeTool .get ();
156
160
List <? extends TypeMirror > supertypes = tool .getDirectSupertypes (mirror );
157
161
if (supertypes .isEmpty ()) {
158
162
// not an enum
159
- return null ;
163
+ return false ;
160
164
}
161
165
TypeMirror superclass = supertypes .get (0 );
162
166
if (!tool .isSameErasure (superclass , tool .asType (Enum .class ))) {
163
167
// not an enum
164
- return null ;
168
+ return false ;
165
169
}
166
170
if (tool .isPrivateType (mirror )) {
167
171
throw TmpException .create ("The enum may not be private." );
168
172
}
169
- return EnumCoercion . create ( mirror ) ;
173
+ return true ;
170
174
}
171
175
172
176
private CollectorInfo collectorInfo (
0 commit comments