Skip to content

Commit 8e0c2a5

Browse files
committed
Improve placeholder replacer for custom holders and Collection entries
1 parent 5e925e4 commit 8e0c2a5

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/main/java/net/elytrium/serializer/language/reader/YamlReader.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ public void readSerializableObject(@Nullable Field owner, Object holder, Class<?
134134
if (!Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers) && !Modifier.isTransient(modifiers)
135135
&& node.getAnnotation(Final.class) == null && node.getType().getAnnotation(Final.class) == null
136136
&& node.getAnnotation(Transient.class) == null && node.getType().getAnnotation(Transient.class) == null) {
137-
Placeholders.removePlaceholders(node.get(holder));
137+
138+
Object value = node.get(holder);
139+
if (this.config.isRegisterPlaceholdersForCollectionEntries() && value instanceof Collection<?> collection) {
140+
for (Object entry : collection) {
141+
Placeholders.removePlaceholders(entry);
142+
}
143+
}
144+
145+
Placeholders.removePlaceholders(value);
138146
this.readNode(holder, node);
139147
this.updatePlaceholders(holder, node);
140148
} else {
@@ -193,9 +201,9 @@ private void updatePlaceholders(Object holder, Field node) throws ReflectiveOper
193201
for (Object entry : collection) {
194202
Placeholders.addPlaceholders(entry, replacer, placeholders.wrapWithBraces(), placeholders.value());
195203
}
196-
} else {
197-
Placeholders.addPlaceholders(value, replacer, placeholders.wrapWithBraces(), placeholders.value());
198204
}
205+
206+
Placeholders.addPlaceholders(value, replacer, placeholders.wrapWithBraces(), placeholders.value());
199207
}
200208
}
201209

src/main/java/net/elytrium/serializer/placeholders/Placeholders.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import java.lang.reflect.Array;
2121
import java.lang.reflect.ParameterizedType;
2222
import java.lang.reflect.Type;
23+
import java.util.ArrayList;
24+
import java.util.Collection;
2325
import java.util.HashMap;
26+
import java.util.List;
2427
import java.util.Map;
2528
import java.util.stream.Stream;
2629

@@ -29,13 +32,26 @@ public class Placeholders {
2932

3033
private static final Map<Integer, Placeholderable<?, ?>> PLACEHOLDERS = new HashMap<>();
3134

32-
public static <T, P> T replace(T value, Object... values) {
33-
var placeholderable = (Placeholderable<T, P>) Placeholders.PLACEHOLDERS.get(System.identityHashCode(value));
34-
return placeholderable.replacer.replace(value, placeholderable.placeholders, values);
35+
public static <T, P, R> R replace(T value, Object... values) {
36+
return Placeholders.replaceFor(value, value, values);
37+
}
38+
39+
public static <H, T, P, R> R replaceFor(H holder, T value, Object... values) {
40+
var placeholderable = (Placeholderable<T, P>) Placeholders.PLACEHOLDERS.get(System.identityHashCode(holder));
41+
if (holder instanceof Collection<?> collection) {
42+
List<T> list = new ArrayList<>(collection.size());
43+
for (Object entry : collection) {
44+
list.add(placeholderable.replacer.replace((T) entry, placeholderable.placeholders, values));
45+
}
46+
47+
return (R) list;
48+
} else {
49+
return (R) placeholderable.replacer.replace(value, placeholderable.placeholders, values);
50+
}
3551
}
3652

3753
public static void addPlaceholders(Object value, PlaceholderReplacer<?, ?> replacer, String... placeholders) {
38-
Placeholders.addPlaceholders(System.identityHashCode(value), replacer, placeholders);
54+
Placeholders.addPlaceholders(value, replacer, true, placeholders);
3955
}
4056

4157
public static void addPlaceholders(Object value, PlaceholderReplacer<?, ?> replacer, boolean wrapWithBraces, String... placeholders) {

src/test/java/net/elytrium/serializer/SerializerTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ void placeholdersTest() {
5757
Placeholders.addPlaceholders(stringWithPlaceholders, new DefaultPlaceholderReplacer(), "placeholder3", "PLACEHOLDER1", "{PLACEHOLDER2}");
5858
Assertions.assertEquals("2 3 1", Placeholders.replace(stringWithPlaceholders, "1", "2", "3"));
5959
Placeholders.removePlaceholders(stringWithPlaceholders);
60+
61+
List<String> listWithPlaceholders = Arrays.asList("{PLACEHOLDER1} {PLACEHOLDER2}", "{placeholder3}");
62+
Placeholders.addPlaceholders(listWithPlaceholders, new DefaultPlaceholderReplacer(), "placeholder3", "PLACEHOLDER1", "{PLACEHOLDER2}");
63+
List<String> list = Placeholders.replace(listWithPlaceholders, "1", "2", "3");
64+
Assertions.assertEquals("2 3", list.get(0));
65+
Assertions.assertEquals("1", list.get(1));
66+
Placeholders.removePlaceholders(listWithPlaceholders);
6067
}
6168

6269
@Test

0 commit comments

Comments
 (0)