Skip to content

Commit e8e3251

Browse files
authored
Replace all non-word characters with underscores. (#113)
1 parent b46ffb1 commit e8e3251

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

extensions/src/main/java/org/mapstruct/extensions/spring/converter/ConversionServiceAdapterGenerator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Locale;
1313
import java.util.Optional;
14+
import java.util.regex.Pattern;
1415
import java.util.stream.Stream;
1516

1617
public class ConversionServiceAdapterGenerator extends AdapterRelatedGenerator {
@@ -25,6 +26,7 @@ public class ConversionServiceAdapterGenerator extends AdapterRelatedGenerator {
2526
ClassName.get("org.springframework.core.convert", "TypeDescriptor");
2627
private static final ClassName COMPONENT_ANNOTATION_CLASS_NAME =
2728
ClassName.get("org.springframework.stereotype", "Component");
29+
private static final Pattern NON_WORD_PATTERN = Pattern.compile("\\W");
2830

2931
public ConversionServiceAdapterGenerator(final Clock clock) {
3032
super(clock);
@@ -58,13 +60,12 @@ private List<FieldSpec> buildTypeDescriptorFields(ConversionServiceAdapterDescri
5860
}
5961

6062
private String fieldName(TypeName typeName) {
61-
String fieldName = "TYPE_DESCRIPTOR_" + typeName.toString()
62-
.replace('.', '_')
63-
.replace('<', '_')
64-
.replace('>', '_')
65-
.replace("[]", "_ARRAY")
63+
String fieldName = "TYPE_DESCRIPTOR_" + NON_WORD_PATTERN.matcher(typeName.toString())
64+
.replaceAll("_")
6665
.toUpperCase(Locale.ROOT);
66+
6767
if (fieldName.lastIndexOf('_') == fieldName.length() - 1) {
68+
// drop trailing underscore
6869
return fieldName.substring(0, fieldName.length() - 1);
6970
} else {
7071
return fieldName;

0 commit comments

Comments
 (0)