Skip to content

Commit 4339e81

Browse files
authored
Remove unused code (#1020)
1 parent f1dd9ab commit 4339e81

File tree

3 files changed

+37
-79
lines changed

3 files changed

+37
-79
lines changed

implementation/src/main/java/io/smallrye/config/ConfigMappingContext.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,17 @@ private Converter<?> getConverter(final Property property) {
132132
}
133133

134134
@SuppressWarnings("unchecked")
135-
public <T> Converter<T> getKeyConverter(Class<?> enclosingType, String field, int degree) {
135+
public <T> Converter<T> getKeyConverter(final Class<?> enclosingType, final MapProperty mapProperty) {
136136
List<Map<Class<?>, Map<String, Converter<?>>>> list = this.keyConvertersByDegreeTypeAndField;
137-
while (list.size() <= degree) {
137+
while (list.size() < mapProperty.getLevels()) {
138138
list.add(new IdentityHashMap<>());
139139
}
140-
Map<Class<?>, Map<String, Converter<?>>> map = list.get(degree);
140+
Map<Class<?>, Map<String, Converter<?>>> map = list.get(mapProperty.getLevels() - 1);
141141
return (Converter<T>) map
142142
.computeIfAbsent(enclosingType, x -> new HashMap<>())
143-
.computeIfAbsent(field, x -> {
144-
ConfigMappingInterface ci = getConfigurationInterface(enclosingType);
145-
Property property = ci.getProperty(field);
146-
MapProperty mapProperty;
147-
if (property.isMap()) {
148-
mapProperty = property.asMap();
149-
} else if (property.isCollection()) {
150-
mapProperty = property.asCollection().getElement().asMap();
151-
} else {
152-
throw new IllegalStateException();
153-
}
154-
155-
while (degree + 1 < mapProperty.getLevels()) {
156-
mapProperty = mapProperty.getValueProperty().asMap();
157-
}
143+
.computeIfAbsent(mapProperty.getMemberName(), x -> {
158144
if (mapProperty.hasKeyConvertWith()) {
159-
return getConverterInstance(mapProperty.getKeyConvertWith());
145+
return ConfigMappingContext.this.getConverterInstance(mapProperty.getKeyConvertWith());
160146
} else {
161147
// todo: replace with generic converter lookup
162148
Class<?> valueRawType = mapProperty.getKeyRawType();

implementation/src/main/java/io/smallrye/config/ConfigMappingInterface.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ public String getPropertyName() {
197197
return hasPropertyName() && !propertyName.isEmpty() ? propertyName : method.getName();
198198
}
199199

200+
public String getMemberName() {
201+
return method.getName();
202+
}
203+
200204
public boolean hasPropertyName() {
201205
return propertyName != null;
202206
}

implementation/src/main/java/io/smallrye/config/ConfigMappingProvider.java

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,20 @@ private void processEagerGroup(
166166
}
167167

168168
Class<?> type = group.getInterfaceType();
169-
HashSet<String> usedProperties = new HashSet<>();
170169
for (int i = 0; i < group.getPropertyCount(); i++) {
171170
Property property = group.getProperty(i);
172-
String memberName = property.getMethod().getName();
173171
ArrayDeque<String> propertyPath = new ArrayDeque<>(currentPath);
174-
if (usedProperties.add(memberName)) {
175-
// process by property type
176-
if (!property.isParentPropertyName()) {
177-
NameIterator ni = new NameIterator(property.hasPropertyName() ? property.getPropertyName()
178-
: propertyName(property, group, namingStrategy));
179-
while (ni.hasNext()) {
180-
propertyPath.add(ni.getNextSegment());
181-
ni.next();
182-
}
172+
// process by property type
173+
if (!property.isParentPropertyName()) {
174+
NameIterator ni = new NameIterator(property.hasPropertyName() ? property.getPropertyName()
175+
: propertyName(property, group, namingStrategy));
176+
while (ni.hasNext()) {
177+
propertyPath.add(ni.getNextSegment());
178+
ni.next();
183179
}
184-
processProperty(propertyPath, matchActions, defaultValues, namingStrategy, group, getEnclosingFunction, type,
185-
memberName, property);
186180
}
181+
processProperty(propertyPath, matchActions, defaultValues, namingStrategy, group, getEnclosingFunction, type,
182+
property);
187183
}
188184
}
189185

@@ -195,14 +191,13 @@ private void processProperty(
195191
final ConfigMappingInterface group,
196192
final BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingFunction,
197193
final Class<?> type,
198-
final String memberName,
199194
final Property property) {
200195

201196
if (property.isOptional()) {
202197
// switch to lazy mode
203198
Property nestedProperty = property.asOptional().getNestedProperty();
204199
processOptionalProperty(currentPath, matchActions, defaultValues, namingStrategy, group, getEnclosingFunction, type,
205-
memberName, nestedProperty);
200+
nestedProperty);
206201
} else if (property.isGroup()) {
207202
processEagerGroup(currentPath, matchActions, defaultValues, namingStrategy, property.asGroup().getGroupType(),
208203
new GetOrCreateEnclosingGroupInGroup(getEnclosingFunction, group, property.asGroup(), currentPath));
@@ -245,7 +240,7 @@ private void processProperty(
245240
CollectionProperty collectionProperty = property.asCollection();
246241
currentPath.addLast(currentPath.removeLast() + "[*]");
247242
processProperty(currentPath, matchActions, defaultValues, namingStrategy, group, getEnclosingFunction, type,
248-
memberName, collectionProperty.getElement());
243+
collectionProperty.getElement());
249244
}
250245
}
251246

@@ -257,7 +252,6 @@ private void processOptionalProperty(
257252
final ConfigMappingInterface group,
258253
final BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingFunction,
259254
final Class<?> type,
260-
final String memberName,
261255
final Property property) {
262256

263257
if (property.isGroup()) {
@@ -266,8 +260,7 @@ private void processOptionalProperty(
266260
GetOrCreateEnclosingGroupInGroup matchAction = new GetOrCreateEnclosingGroupInGroup(
267261
getEnclosingFunction, group, nestedGroup, currentPath);
268262
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, nestedGroup.getGroupType(),
269-
matchAction,
270-
new HashSet<>());
263+
matchAction);
271264
} else if (property.isLeaf()) {
272265
LeafProperty leafProperty = property.asLeaf();
273266
if (leafProperty.hasDefaultValue()) {
@@ -286,7 +279,7 @@ private void processOptionalProperty(
286279
CollectionProperty collectionProperty = property.asCollection();
287280
currentPath.addLast(currentPath.removeLast() + "[*]");
288281
processProperty(currentPath, matchActions, defaultValues, namingStrategy, group, getEnclosingFunction, type,
289-
memberName, collectionProperty.getElement());
282+
collectionProperty.getElement());
290283
}
291284
}
292285

@@ -296,8 +289,7 @@ private void processLazyGroupInGroup(
296289
final Map<String, String> defaultValues,
297290
final NamingStrategy namingStrategy,
298291
final ConfigMappingInterface group,
299-
final BiConsumer<ConfigMappingContext, NameIterator> matchAction,
300-
final HashSet<String> usedProperties) {
292+
final BiConsumer<ConfigMappingContext, NameIterator> matchAction) {
301293
int pc = group.getPropertyCount();
302294
int pathLen = currentPath.size();
303295
for (int i = 0; i < pc; i++) {
@@ -310,19 +302,17 @@ private void processLazyGroupInGroup(
310302
ni.next();
311303
}
312304
}
313-
if (usedProperties.add(String.join(".", String.join(".", currentPath), property.getMethod().getName()))) {
314-
boolean optional = property.isOptional();
315-
processLazyPropertyInGroup(currentPath, matchActions, defaultValues, matchAction, usedProperties,
316-
namingStrategy, group, optional, property);
317-
}
305+
boolean optional = property.isOptional();
306+
processLazyPropertyInGroup(currentPath, matchActions, defaultValues, matchAction, namingStrategy, group, optional,
307+
property);
318308
while (currentPath.size() > pathLen) {
319309
currentPath.removeLast();
320310
}
321311
}
322312
int sc = group.getSuperTypeCount();
323313
for (int i = 0; i < sc; i++) {
324314
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, group.getSuperType(i),
325-
matchAction, usedProperties);
315+
matchAction);
326316
}
327317
}
328318

@@ -331,7 +321,6 @@ private void processLazyPropertyInGroup(
331321
final KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> matchActions,
332322
final Map<String, String> defaultValues,
333323
final BiConsumer<ConfigMappingContext, NameIterator> matchAction,
334-
final HashSet<String> usedProperties,
335324
final NamingStrategy namingStrategy,
336325
final ConfigMappingInterface group,
337326
final boolean optional,
@@ -344,15 +333,15 @@ private void processLazyPropertyInGroup(
344333
: new ConsumeOneAndThenFn<>(new GetNestedEnclosing(matchAction)),
345334
group, nestedGroup, currentPath);
346335
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, nestedGroup.getGroupType(),
347-
nestedEnclosingFunction, new HashSet<>());
336+
nestedEnclosingFunction);
348337
} else if (property.isGroup()) {
349338
GroupProperty asGroup = property.asGroup();
350339
GetOrCreateEnclosingGroupInGroup nestedEnclosingFunction = new GetOrCreateEnclosingGroupInGroup(
351340
property.isParentPropertyName() ? new GetNestedEnclosing(matchAction)
352341
: new ConsumeOneAndThenFn<>(new GetNestedEnclosing(matchAction)),
353342
group, asGroup, currentPath);
354343
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, asGroup.getGroupType(),
355-
nestedEnclosingFunction, usedProperties);
344+
nestedEnclosingFunction);
356345
} else if (property.isLeaf() || property.isPrimitive()
357346
|| optional && property.asOptional().getNestedProperty().isLeaf()) {
358347
BiConsumer<ConfigMappingContext, NameIterator> actualAction = property.isParentPropertyName() ? matchAction
@@ -398,8 +387,8 @@ private void processLazyPropertyInGroup(
398387
CollectionProperty collectionProperty = optional ? property.asOptional().getNestedProperty().asCollection()
399388
: property.asCollection();
400389
currentPath.addLast(currentPath.removeLast() + "[*]");
401-
processLazyPropertyInGroup(currentPath, matchActions, defaultValues, matchAction, usedProperties, namingStrategy,
402-
group, false, collectionProperty.getElement());
390+
processLazyPropertyInGroup(currentPath, matchActions, defaultValues, matchAction, namingStrategy, group, false,
391+
collectionProperty.getElement());
403392
}
404393
}
405394

@@ -539,7 +528,7 @@ private void processLazyMapValue(
539528
GetOrCreateEnclosingGroupInMap ef = new GetOrCreateEnclosingGroupInMap(getEnclosingMap, mapProperty, keyUnnamed,
540529
enclosingGroup, property.asGroup(), currentPath);
541530
processLazyGroupInGroup(currentPath, matchActions, defaultValues, namingStrategy, property.asGroup().getGroupType(),
542-
ef, new HashSet<>());
531+
ef);
543532
} else if (property.isCollection()) {
544533
CollectionProperty collectionProperty = property.asCollection();
545534
Property element = collectionProperty.getElement();
@@ -561,8 +550,8 @@ private void addAction(
561550
KeyMap<BiConsumer<ConfigMappingContext, NameIterator>> current = matchActions.findOrAdd(currentPath);
562551
Property previous = properties.put(String.join(".", currentPath), property);
563552
if (current.hasRootValue() && current.getRootValue() != action && previous != null && !previous.equals(property)) {
564-
throw ConfigMessages.msg.ambiguousMapping(String.join(".", currentPath), property.getMethod().toString(),
565-
previous.getMethod().toString());
553+
throw ConfigMessages.msg.ambiguousMapping(String.join(".", currentPath), property.getMemberName(),
554+
previous.getMemberName());
566555
}
567556
current.putRootValue(action);
568557
}
@@ -678,7 +667,7 @@ static class GetOrCreateEnclosingGroupInGroup
678667
public ConfigMappingObject apply(final ConfigMappingContext context, final NameIterator ni) {
679668
ConfigMappingObject ourEnclosing = delegate.apply(context, ni);
680669
Class<?> enclosingType = enclosingGroup.getInterfaceType();
681-
String key = indexName(enclosedGroup.getMethod().getName(), groupPath, ni);
670+
String key = indexName(enclosedGroup.getMemberName(), groupPath, ni);
682671
ConfigMappingObject val = (ConfigMappingObject) context.getEnclosedField(enclosingType, key, ourEnclosing);
683672
context.applyNamingStrategy(
684673
namingStrategy(enclosedGroup.getGroupType().getNamingStrategy(), enclosingGroup.getNamingStrategy()));
@@ -789,8 +778,7 @@ private MapKey mapKey(final ConfigMappingContext context, final NameIterator ni,
789778
mapPath.next();
790779
String pathKey = mapPath.getAllPreviousSegments();
791780
mapPath.previous();
792-
Converter<?> converterKey = context.getKeyConverter(enclosingGroup.getInterfaceType(),
793-
enclosingMap.getMethod().getName(), enclosingMap.getLevels() - 1);
781+
Converter<?> converterKey = context.getKeyConverter(enclosingGroup.getInterfaceType(), enclosingMap);
794782

795783
// This will be the key to use to store the value in the map
796784
String nameKey = normalizeIfIndexed(rawKey);
@@ -876,7 +864,7 @@ static class GetOrCreateEnclosingMapInGroup implements BiFunction<ConfigMappingC
876864
if (consumeName)
877865
ni.next();
878866
Class<?> enclosingType = enclosingGroup.getInterfaceType();
879-
String key = indexName(enclosedGroup.getMethod().getName(), groupPath, ni);
867+
String key = indexName(enclosedGroup.getMemberName(), groupPath, ni);
880868
Map<?, ?> val = (Map<?, ?>) context.getEnclosedField(enclosingType, key, ourEnclosing);
881869
context.applyNamingStrategy(enclosingGroup.getNamingStrategy());
882870
if (val == null) {
@@ -893,26 +881,6 @@ public void accept(final ConfigMappingContext context, final NameIterator ni) {
893881
}
894882
}
895883

896-
static class GetFieldOfEnclosing implements BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> {
897-
private final BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingFunction;
898-
private final Class<?> type;
899-
private final String memberName;
900-
901-
GetFieldOfEnclosing(final BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> getEnclosingFunction,
902-
final Class<?> type, final String memberName) {
903-
this.getEnclosingFunction = getEnclosingFunction;
904-
this.type = type;
905-
this.memberName = memberName;
906-
}
907-
908-
@Override
909-
public ConfigMappingObject apply(final ConfigMappingContext context, final NameIterator ni) {
910-
ConfigMappingObject outer = getEnclosingFunction.apply(context, ni);
911-
// eagerly populated groups will always exist
912-
return (ConfigMappingObject) context.getEnclosedField(type, memberName, outer);
913-
}
914-
}
915-
916884
// To recursively create Optional nested groups
917885
static class GetNestedEnclosing implements BiFunction<ConfigMappingContext, NameIterator, ConfigMappingObject> {
918886
private final BiConsumer<ConfigMappingContext, NameIterator> matchAction;

0 commit comments

Comments
 (0)