Skip to content

Commit 960f4f6

Browse files
committed
Bit more work on #2800 to provide some config settings regarding 1st char of base name
1 parent 62ae580 commit 960f4f6

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/DefaultAccessorNamingStrategy.java

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,35 +245,140 @@ public static class Provider
245245
protected final String _getterPrefix;
246246
protected final String _isGetterPrefix;
247247

248+
protected final boolean _allowLowerCaseFirstChar;
249+
protected final boolean _allowNonLetterFirstChar;
250+
248251
public Provider() {
249252
this("set", JsonPOJOBuilder.DEFAULT_WITH_PREFIX,
250-
"get", "is");
253+
"get", "is",
254+
true, true);
255+
}
256+
257+
protected Provider(Provider p,
258+
String setterPrefix, String withPrefix,
259+
String getterPrefix, String isGetterPrefix)
260+
{
261+
this(setterPrefix, withPrefix, getterPrefix, isGetterPrefix,
262+
p._allowLowerCaseFirstChar, p._allowNonLetterFirstChar);
263+
}
264+
265+
protected Provider(Provider p,
266+
boolean allowLowerCaseFirstChar, boolean allowNonLetterFirstChar)
267+
{
268+
this(p._setterPrefix, p._withPrefix,
269+
p._getterPrefix, p._isGetterPrefix,
270+
allowLowerCaseFirstChar, allowNonLetterFirstChar);
251271
}
252272

253-
public Provider(String setterPrefix, String withPrefix,
254-
String getterPrefix, String isGetterPrefix) {
273+
protected Provider(String setterPrefix, String withPrefix,
274+
String getterPrefix, String isGetterPrefix,
275+
boolean allowLowerCaseFirstChar, boolean allowNonLetterFirstChar)
276+
{
255277
_setterPrefix = setterPrefix;
256278
_withPrefix = withPrefix;
257279
_getterPrefix = getterPrefix;
258280
_isGetterPrefix = isGetterPrefix;
281+
_allowLowerCaseFirstChar = allowLowerCaseFirstChar;
282+
_allowNonLetterFirstChar = allowNonLetterFirstChar;
259283
}
260-
261-
public Provider withSetterPrefix(String p) {
262-
return new Provider(p, _withPrefix, _getterPrefix, _isGetterPrefix);
284+
285+
286+
/**
287+
* Mutant factory for changing the prefix used for "setter"
288+
* methods
289+
*
290+
* @param prefix Prefix to use; or empty String {@code ""} to not use
291+
* any prefix (meaning signature-compatible method name is used as
292+
* the property basename (and subject to name mangling)),
293+
* or {@code null} to prevent name-based detection.
294+
*
295+
* @return Provider instance with specified setter-prefix
296+
*/
297+
public Provider withSetterPrefix(String prefix) {
298+
return new Provider(this,
299+
prefix, _withPrefix, _getterPrefix, _isGetterPrefix);
263300
}
264301

265-
public Provider withBuilderPrefix(String p) {
266-
return new Provider(_setterPrefix, p, _getterPrefix, _isGetterPrefix);
302+
/**
303+
* Mutant factory for changing the prefix used for Builders
304+
* (from default {@link JsonPOJOBuilder#DEFAULT_WITH_PREFIX})
305+
*
306+
* @param prefix Prefix to use; or empty String {@code ""} to not use
307+
* any prefix (meaning signature-compatible method name is used as
308+
* the property basename (and subject to name mangling)),
309+
* or {@code null} to prevent name-based detection.
310+
*
311+
* @return Provider instance with specified with-prefix
312+
*/
313+
public Provider withBuilderPrefix(String prefix) {
314+
return new Provider(this,
315+
_setterPrefix, prefix, _getterPrefix, _isGetterPrefix);
267316
}
268317

269-
public Provider withGetterPrefix(String p) {
270-
return new Provider(_setterPrefix, _withPrefix, p, _isGetterPrefix);
318+
/**
319+
* Mutant factory for changing the prefix used for "getter"
320+
* methods
321+
*
322+
* @param prefix Prefix to use; or empty String {@code ""} to not use
323+
* any prefix (meaning signature-compatible method name is used as
324+
* the property basename (and subject to name mangling)),
325+
* or {@code null} to prevent name-based detection.
326+
*
327+
* @return Provider instance with specified getter-prefix
328+
*/
329+
public Provider withGetterPrefix(String prefix) {
330+
return new Provider(this,
331+
_setterPrefix, _withPrefix, prefix, _isGetterPrefix);
271332
}
272333

273-
public Provider withIsGetterPrefix(String p) {
274-
return new Provider(_setterPrefix, _withPrefix, _getterPrefix, p);
334+
/**
335+
* Mutant factory for changing the prefix used for "is-getter"
336+
* methods (getters that return boolean/Boolean value).
337+
*
338+
* @param prefix Prefix to use; or empty String {@code ""} to not use
339+
* any prefix (meaning signature-compatible method name is used as
340+
* the property basename (and subject to name mangling)).
341+
* or {@code null} to prevent name-based detection.
342+
*
343+
* @return Provider instance with specified is-getter-prefix
344+
*/
345+
public Provider withIsGetterPrefix(String prefix) {
346+
return new Provider(this,
347+
_setterPrefix, _withPrefix, _getterPrefix, prefix);
275348
}
276349

350+
/**
351+
* Mutant factory for changing the rules regarding which characters
352+
* are allowed as the first character of property base name, after
353+
* checking and removing prefix.
354+
*<p>
355+
* For example, consider "getter" method candidate (no arguments, has return
356+
* type) named {@code getValue()} is considered, with "getter-prefix"
357+
* defined as {@code get}, then base name is {@code Value} and the
358+
* first character to consider is {@code V}. Upper-case letters are
359+
* always accepted so this is fine.
360+
* But with similar settings, method {@code get_value()} would only be
361+
* recognized as getter if {@code allowNonLetterFirstChar} is set to
362+
* {@code true}: otherwise it will not be considered a getter-method.
363+
* Similarly "is-getter" candidate method with name {@code island()}
364+
* would only be considered if {@code allowLowerCaseFirstChar} is set
365+
* to {@code true}.
366+
*
367+
* @param allowLowerCaseFirstChar Whether base names that start with lower-case
368+
* letter (like {@code "a"} or {@code "b"}) are accepted as valid or not:
369+
* consider difference between "setter-methods" {@code setValue()} and {@code setvalue()}.
370+
* @param allowNonLetterFirstChar Whether base names that start with non-letter
371+
* character (like {@code "_"} or number {@code 1}) are accepted as valid or not:
372+
* consider difference between "setter-methods" {@code setValue()} and {@code set_value()}.
373+
*
374+
* @return Provider instance with specified is-getter-prefix
375+
*/
376+
public Provider withFirstCharAcceptance(boolean allowLowerCaseFirstChar,
377+
boolean allowNonLetterFirstChar) {
378+
return new Provider(this,
379+
allowLowerCaseFirstChar, allowNonLetterFirstChar);
380+
}
381+
277382
@Override
278383
public AccessorNamingStrategy forPOJO(MapperConfig<?> config, AnnotatedClass targetClass)
279384
{

src/test/java/com/fasterxml/jackson/databind/introspect/AccessorNamingStrategyTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.json.JsonMapper;
77

88
// for [databind#2800]
9+
@SuppressWarnings("serial")
910
public class AccessorNamingStrategyTest extends BaseMapTest
1011
{
1112
@JsonPropertyOrder({ "X", "x", "Z", "z" }) // since our naming strategy casings vary
@@ -71,7 +72,6 @@ public String modifyFieldName(AnnotatedField field, String name) {
7172
}
7273
}
7374

74-
@SuppressWarnings("serial")
7575
static class AccNaming2800Provider extends DefaultAccessorNamingStrategy.Provider
7676
{
7777
@Override
@@ -80,7 +80,6 @@ public AccessorNamingStrategy forPOJO(MapperConfig<?> config, AnnotatedClass val
8080
}
8181
}
8282

83-
@SuppressWarnings("serial")
8483
static class BaseNamingProvider extends DefaultAccessorNamingStrategy.Provider
8584
{
8685
@Override

0 commit comments

Comments
 (0)