-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from HubSpot/jh/rm-naming-strategy-base
Remove references to PropertyNamingStrategyBase
- Loading branch information
Showing
6 changed files
with
117 additions
and
124 deletions.
There are no files selected for viewing
110 changes: 44 additions & 66 deletions
110
src/main/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingStrategyWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,75 @@ | ||
package com.hubspot.jackson.datatype.protobuf; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies.NamingBase; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy.PropertyNamingStrategyBase; | ||
import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
import com.fasterxml.jackson.databind.introspect.AnnotatedField; | ||
import com.fasterxml.jackson.databind.introspect.AnnotationMap; | ||
import com.fasterxml.jackson.databind.introspect.TypeResolutionContext; | ||
import com.google.common.base.CaseFormat; | ||
import java.lang.reflect.Method; | ||
import com.google.protobuf.Message; | ||
import java.lang.reflect.Field; | ||
|
||
@SuppressWarnings("serial") | ||
public class PropertyNamingStrategyWrapper extends PropertyNamingStrategyBase { | ||
public class PropertyNamingStrategyWrapper { | ||
|
||
private static final PropertyNamingStrategyBase SNAKE_TO_CAMEL = new SnakeToCamelNamingStrategy(); | ||
private static final PropertyNamingStrategyBase NO_OP = new NoOpNamingStrategy(); | ||
private static final PropertyNamingStrategy SNAKE_TO_CAMEL = new SnakeToCamelNamingStrategy(); | ||
private static final PropertyNamingStrategy NO_OP = new NoOpNamingStrategy(); | ||
|
||
private final PropertyNamingStrategyBase delegate; | ||
private final Class<?> messageType; | ||
private final MapperConfig<?> mapperConfig; | ||
private final PropertyNamingStrategy delegate; | ||
|
||
public PropertyNamingStrategyWrapper(PropertyNamingStrategy delegate) { | ||
if (delegate instanceof PropertyNamingStrategyBase) { | ||
this.delegate = (PropertyNamingStrategyBase) delegate; | ||
} else if (NamingBaseAdapter.extendsNamingBase(delegate)) { | ||
this.delegate = new NamingBaseAdapter(delegate); | ||
} else if (delegate == PropertyNamingStrategy.LOWER_CAMEL_CASE) { | ||
public PropertyNamingStrategyWrapper( | ||
Class<? extends Message> messageType, | ||
MapperConfig<?> mapperConfig | ||
) { | ||
this.messageType = messageType; | ||
this.mapperConfig = mapperConfig; | ||
|
||
if (mapperConfig.getPropertyNamingStrategy() == null) { | ||
this.delegate = SNAKE_TO_CAMEL; | ||
} else if ( | ||
mapperConfig.getPropertyNamingStrategy() == | ||
PropertyNamingStrategies.LOWER_CAMEL_CASE | ||
) { | ||
this.delegate = NO_OP; | ||
} else { | ||
this.delegate = SNAKE_TO_CAMEL; | ||
this.delegate = mapperConfig.getPropertyNamingStrategy(); | ||
} | ||
} | ||
|
||
@Override | ||
public String translate(String fieldName) { | ||
return delegate.translate(fieldName); | ||
AnnotatedField annotatedField = null; | ||
try { | ||
Field field = messageType.getDeclaredField(fieldName + "_"); | ||
annotatedField = | ||
new AnnotatedField( | ||
new TypeResolutionContext.Empty(mapperConfig.getTypeFactory()), | ||
field, | ||
new AnnotationMap() | ||
); | ||
} catch (ReflectiveOperationException e) { | ||
// ignored | ||
} | ||
|
||
return delegate.nameForField(mapperConfig, annotatedField, fieldName); | ||
} | ||
|
||
private static class SnakeToCamelNamingStrategy extends PropertyNamingStrategyBase { | ||
private static class SnakeToCamelNamingStrategy extends NamingBase { | ||
|
||
@Override | ||
public String translate(String fieldName) { | ||
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName); | ||
} | ||
} | ||
|
||
private static class NoOpNamingStrategy extends PropertyNamingStrategyBase { | ||
private static class NoOpNamingStrategy extends NamingBase { | ||
|
||
@Override | ||
public String translate(String fieldName) { | ||
return fieldName; | ||
} | ||
} | ||
|
||
private static class NamingBaseAdapter extends PropertyNamingStrategyBase { | ||
|
||
private static final Class<?> NAMING_BASE = tryToLoadNamingBase(); | ||
private static final Method TRANSLATE_METHOD = tryToLoadTranslateMethod(NAMING_BASE); | ||
|
||
private final PropertyNamingStrategy delegate; | ||
|
||
private NamingBaseAdapter(PropertyNamingStrategy delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public static boolean extendsNamingBase(PropertyNamingStrategy namingStrategy) { | ||
return NAMING_BASE != null && NAMING_BASE.isInstance(namingStrategy); | ||
} | ||
|
||
@Override | ||
public String translate(String fieldName) { | ||
try { | ||
return (String) TRANSLATE_METHOD.invoke(delegate, fieldName); | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException("Unable to invoke translate method", e); | ||
} | ||
} | ||
|
||
private static Class<?> tryToLoadNamingBase() { | ||
try { | ||
return Class.forName( | ||
"com.fasterxml.jackson.databind.PropertyNamingStrategies$NamingBase" | ||
); | ||
} catch (ClassNotFoundException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static Method tryToLoadTranslateMethod(Class<?> namingBase) { | ||
if (namingBase == null) { | ||
return null; | ||
} else { | ||
try { | ||
return namingBase.getMethod("translate", String.class); | ||
} catch (NoSuchMethodException e) { | ||
throw new RuntimeException( | ||
"Unable to find translate method on class: " + namingBase | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.