Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static PropertyNamingStrategy instances in POJOPropertiesCollector._findNamingStrategy() #4109

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,59 @@ public abstract class PropertyNamingStrategies
*/
public static final PropertyNamingStrategy LOWER_DOT_CASE = LowerDotCaseStrategy.INSTANCE;

/**
* Returns a default instance of standard {@link PropertyNamingStrategy} which is statically defined in
* {@link PropertyNamingStrategies}. Otherwise, returns null.
*
* @param type type of {@link PropertyNamingStrategy}
* @return a default instance of standard {@link PropertyNamingStrategy}, or null
*/
public static PropertyNamingStrategy getDefaultInstanceOf(Class<? extends PropertyNamingStrategy> type) {
// PropertyNamingStrategy
if (type == PropertyNamingStrategy.SnakeCaseStrategy.class) {
return PropertyNamingStrategies.SNAKE_CASE;
}
if (type == PropertyNamingStrategy.UpperCamelCaseStrategy.class) {
return PropertyNamingStrategies.UPPER_CAMEL_CASE;
}
if (type == PropertyNamingStrategy.KebabCaseStrategy.class) {
return PropertyNamingStrategies.KEBAB_CASE;
}
if (type == PropertyNamingStrategy.LowerDotCaseStrategy.class) {
return PropertyNamingStrategies.LOWER_DOT_CASE;
}
if (type == PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class) {
return PropertyNamingStrategies.SNAKE_CASE;
}
if (type == PropertyNamingStrategy.PascalCaseStrategy.class) {
return PropertyNamingStrategies.UPPER_CAMEL_CASE;
}

// PropertyNamingStrategies
if (type == PropertyNamingStrategies.SnakeCaseStrategy.class) {
return PropertyNamingStrategies.SNAKE_CASE;
}
if (type == PropertyNamingStrategies.UpperSnakeCaseStrategy.class) {
return PropertyNamingStrategies.UPPER_SNAKE_CASE;
}
if (type == PropertyNamingStrategies.LowerCamelCaseStrategy.class) {
return PropertyNamingStrategies.LOWER_CAMEL_CASE;
}
if (type == PropertyNamingStrategies.UpperCamelCaseStrategy.class) {
return PropertyNamingStrategies.UPPER_CAMEL_CASE;
}
if (type == PropertyNamingStrategies.LowerCaseStrategy.class) {
return PropertyNamingStrategies.LOWER_CASE;
}
if (type == PropertyNamingStrategies.KebabCaseStrategy.class) {
return PropertyNamingStrategies.KEBAB_CASE;
}
if (type == PropertyNamingStrategies.LowerDotCaseStrategy.class) {
return PropertyNamingStrategies.LOWER_DOT_CASE;
}
return null;
}

/*
/**********************************************************************
/* Public base class for simple implementations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,13 @@ private PropertyNamingStrategy _findNamingStrategy()
return pns;
}
}

Copy link
Member

@JooHyukKim JooHyukKim Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Plz check Line 1448
PropertyNamingStrategy pns = hi.namingStrategyInstance(_config, _classDef, namingClass);

Possibly, at line 1448 (above) HandlerInstantiator is also doing what we are trying to prevent to happen here. namingStrategyInstance() JavaDoc says....

/**
  * Method called to construct a NamingStrategy instance used for specified
  * class.
  *
  * @since 2.1
  */

Copy link
Contributor Author

@takezoe takezoe Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandlerInstantiator is given through configuration and it can return any instance of PropertyNamingStrategy. I think we can't rescue if HandlerInstantiator is used.

PropertyNamingStrategy defaultInstance = PropertyNamingStrategies
.getDefaultInstanceOf((Class<? extends PropertyNamingStrategy>) namingClass);
if (defaultInstance != null) {
return defaultInstance;
}

return (PropertyNamingStrategy) ClassUtil.createInstance(namingClass,
_config.canOverrideAccessModifiers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,53 @@ public void testNamingStrategiesHandlingNullAndEmpty() {
assertEquals(" ", namingStrategy.translate(" "));
}
}

public void testGetDefaultInstance() {
// PropertyNamingStrategy
assertSame(
PropertyNamingStrategies.SNAKE_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.SnakeCaseStrategy.class));
assertSame(
PropertyNamingStrategies.UPPER_CAMEL_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.UpperCamelCaseStrategy.class));
assertSame(
PropertyNamingStrategies.KEBAB_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.KebabCaseStrategy.class));
assertSame(
PropertyNamingStrategies.LOWER_DOT_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.LowerDotCaseStrategy.class));
assertSame(
PropertyNamingStrategies.SNAKE_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class));
assertSame(
PropertyNamingStrategies.UPPER_CAMEL_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.PascalCaseStrategy.class));

// PropertyNamingStrategies
assertSame(
PropertyNamingStrategies.SNAKE_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.SnakeCaseStrategy.class));
assertSame(
PropertyNamingStrategies.UPPER_SNAKE_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.UpperSnakeCaseStrategy.class));
assertSame(
PropertyNamingStrategies.LOWER_CAMEL_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.LowerCamelCaseStrategy.class));
assertSame(
PropertyNamingStrategies.UPPER_CAMEL_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.UpperCamelCaseStrategy.class));
assertSame(
PropertyNamingStrategies.LOWER_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.LowerCaseStrategy.class));
assertSame(
PropertyNamingStrategies.KEBAB_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.KebabCaseStrategy.class));
assertSame(
PropertyNamingStrategies.LOWER_DOT_CASE,
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategies.LowerDotCaseStrategy.class));

// others
assertNull(
PropertyNamingStrategies.getDefaultInstanceOf(PropertyNamingStrategy.class));
}
}