Skip to content
Merged
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
2 changes: 2 additions & 0 deletions hsweb-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ ValidationException 的 details 以 property/message/detail 字段 Map 列表导

可在 `META-INF/services/org.hswebframework.web.bean.ExceptionConverter` 注册自定义双向实现(`toMap` / `fromMap`)。首次异常转换时使用线程上下文 ClassLoader 发现的首个实现,每个运行环境应只注册一个;无实现时回退 `DefaultExceptionConverter`。实例按 copier 的 ClassLoader 复用,自定义实现应无状态或线程安全。toMap 按字段提供延迟 Supplier,由 copier 统一过滤、读取及转换;继承默认实现并覆盖 `getDetail(Throwable)` 可精简原始异常的栈,自定义栈处理需保留循环引用处理。携带 RemoteExceptionMetadata 的异常直接沿用远端 detail,不再调用此栈渲染方法。fromMap 应返回与目标声明兼容的异常,copier 拒绝 null 或不兼容的 SPI 返回值。

Map 泛型转换保持浅复制语义,仅转换与声明不符的键和值。TreeMap 的非空键样本均与目标键类型兼容时保留比较器,null 键不影响该判断;键类型转换后、空 Map 或仅含 null 键且目标声明了具体键类型时,目标 Map 保留原遍历顺序,不继承无法确认兼容的比较器。目标键类型为 Object 时键不转换,保留原比较器。

访问器回归由 `FastBeanCopierBeanTest`、`FastBeanCopierReadOnlyTest`、`FastBeanCopierReadOnceTest`、`FastBeanCopierTransientTest` 和 `FastBeanCopierTest` 覆盖,包括只读源属性、单次取值、导出排除规则、目标可写边界、不同访问器风格、继承泛型、忽略属性、record、全局配置隔离及静态访问器排除。`FastBeanCopierExceptionTest`、`FastBeanCopierExceptionRestoreTest`、`FastBeanCopierExceptionSpiTest` 及 `RemoteExceptionI18nTest` 覆盖异常描述、循环 cause、受控还原、JSON 往返、国际化回退和重译、值转换及双向 SPI 发现。

约定: 如果属性类实现了`Cloneable`接口,在复制的时候将调用`clone`方法.所以如果你实现了`Cloneable`接口,就必须重写`clone`方法并且为`public`修饰的.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,14 +1037,14 @@ public <T> T convert(Object source, Class<T> targetClass, Class[] genericType) {
if (targetClass == Map.class) {
if (source instanceof Map) {
Map<?, ?> original = (Map<?, ?>) source;
Map<Object, Object> result = (Map<Object, Object>) copyMap(original);
if (genericType != null && genericType.length >= 2
&& (genericType[0] != Object.class || genericType[1] != Object.class)) {
result.clear();
Map<Object, Object> result = newMap(original, genericType[0]);
original.forEach((key, value) -> result.put(
convertMapElement(key, genericType[0]), convertMapElement(value, genericType[1])));
return (T) result;
}
return (T) result;
return (T) copyMap(original);
}
if (source instanceof Collection) {
Map<Object, Object> map = new LinkedHashMap<>();
Expand Down Expand Up @@ -1076,6 +1076,27 @@ private Object convertMapElement(Object value, Class<?> type) {
return value == null || type.isInstance(value) ? value : convert(value, type, EMPTY_CLASS_ARRAY);
}

private Map<Object, Object> newMap(Map<?, ?> source, Class<?> keyType) {
if (source instanceof TreeMap<?, ?> sorted) {
// 比较器只适用于原键域。键转换后,Map 目标按原迭代顺序接收,不能沿用原比较器。
Set<?> keys = source.keySet();
// null 不参与转换;至少需要一个非空键样本才能确认比较器的键域。
if (keyType == Object.class
|| keys.stream().anyMatch(Objects::nonNull)
&& keys.stream().allMatch(key -> key == null || keyType.isInstance(key))) {
return new TreeMap<>((Comparator<Object>) sorted.comparator());
}
return Maps.newLinkedHashMapWithExpectedSize(source.size());
}
if (source instanceof ConcurrentHashMap) {
return new ConcurrentHashMap<>(source.size());
}
if (source instanceof LinkedHashMap) {
return Maps.newLinkedHashMapWithExpectedSize(source.size());
}
return Maps.newHashMapWithExpectedSize(source.size());
}

private Map<?, ?> copyMap(Map<?, ?> map) {
if (map instanceof TreeMap) {
return new TreeMap<>((SortedMap<?, ?>) map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -59,6 +63,72 @@ public void converterRetainsSortedAndConcurrentMapImplementations() {
new Class[]{String.class, Item.class}) instanceof ConcurrentHashMap);
}

@Test
public void convertedTreeMapKeysDoNotReuseTheOriginalKeyComparator() {
Comparator<String> comparator = (left, right) -> right.compareTo(left);
TreeMap<String, Object> source = new TreeMap<>(comparator);
source.put("10", Map.of("name", "ten"));
source.put("2", Map.of("name", "two"));

Map<Integer, Item> result = FastBeanCopier.copy(Map.of("entries", source), Envelope.class).entries();

assertTrue(result instanceof LinkedHashMap);
assertEquals(List.of(2, 10), new ArrayList<>(result.keySet()));
assertEquals("two", result.get(2).name());
assertEquals("ten", result.get(10).name());
assertEquals(List.of("2", "10"), new ArrayList<>(source.keySet()));
assertTrue(source.get("2") instanceof Map);
assertSame(comparator, source.comparator());
}

@Test
public void compatibleTreeMapKeysIncludingNullKeepTheirComparator() {
Comparator<Integer> comparator = Comparator.nullsFirst(Comparator.reverseOrder());
TreeMap<Integer, Object> source = new TreeMap<>(comparator);
source.put(null, Map.of("name", "none"));
source.put(2, Map.of("name", "two"));

Map<Integer, Item> result = FastBeanCopier.copy(Map.of("entries", source), Envelope.class).entries();

assertTrue(result instanceof TreeMap);
assertSame(comparator, ((TreeMap<Integer, Item>) result).comparator());
assertEquals("none", result.get(null).name());
result.put(3, new Item("three"));
assertEquals(Arrays.asList(null, 3, 2), new ArrayList<>(result.keySet()));
assertFalse(source.containsKey(3));
}

@Test
public void emptyAndNullOnlyMapsDoNotRetainAnUnknownKeyComparator() {
Comparator<String> comparator = Comparator.nullsFirst(String::compareTo);
TreeMap<String, Object> empty = new TreeMap<>(comparator);
TreeMap<String, Object> nullOnly = new TreeMap<>(comparator);
nullOnly.put(null, Map.of("name", "none"));

for (TreeMap<String, Object> source : List.of(empty, nullOnly)) {
Map<Integer, Item> result = FastBeanCopier.copy(Map.of("entries", source), Envelope.class).entries();
assertTrue(result instanceof LinkedHashMap);
result.put(1, new Item("one"));
assertFalse(source.containsKey("1"));
}
assertEquals("none", FastBeanCopier.copy(Map.of("entries", nullOnly), Envelope.class).entries().get(null).name());
}

@Test
public void objectKeyConversionRetainsTheComparatorWithoutKeySamples() {
Comparator<String> comparator = Comparator.nullsFirst(String::compareTo);
TreeMap<String, Object> source = new TreeMap<>(comparator);
source.put(null, Map.of("name", "none"));

Map<?, ?> result = FastBeanCopier.DEFAULT_CONVERT.convert(source, Map.class,
new Class[]{Object.class, Item.class});

assertTrue(result instanceof TreeMap);
assertSame(comparator, ((TreeMap<?, ?>) result).comparator());
assertEquals("none", ((Item) result.get(null)).name());
assertTrue(source.get(null) instanceof Map);
}

public record Item(String name) { }
public record Envelope(Map<Integer, Item> entries) { }
public record Fields(Map<String, Object> fields) { }
Expand Down
Loading