mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-05-31 18:03:52 +08:00
refactor: 优化命名
This commit is contained in:
@@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
@@ -15,7 +15,7 @@ import java.util.Map;
|
||||
* <p>
|
||||
* <ul>
|
||||
* <li>
|
||||
* 实体类继承此类,或者实现{@link Extensible}接口.
|
||||
* 实体类继承此类,或者实现{@link Extendable}接口.
|
||||
* </li>
|
||||
* <li>
|
||||
* 使用{@link org.hswebframework.web.crud.configuration.TableMetadataCustomizer}自定义表结构
|
||||
@@ -32,12 +32,12 @@ import java.util.Map;
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class ExtensibleEntity<PK> extends GenericEntity<PK> implements Extensible {
|
||||
public class ExtendableEntity<PK> extends GenericEntity<PK> implements Extendable {
|
||||
|
||||
private Map<String, Object> extensions;
|
||||
|
||||
/**
|
||||
* 默认不序列化扩展属性,会由{@link ExtensibleEntity#extensions()},{@link JsonAnyGetter}平铺到json中.
|
||||
* 默认不序列化扩展属性,会由{@link ExtendableEntity#extensions()},{@link JsonAnyGetter}平铺到json中.
|
||||
*
|
||||
* @return 扩展属性
|
||||
*/
|
||||
@@ -6,22 +6,22 @@ import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ExtensibleEntityTest {
|
||||
public class ExtendableEntityTest {
|
||||
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
public void testJson() {
|
||||
ExtensibleEntity<String> entity = new ExtensibleEntity<>();
|
||||
ExtendableEntity<String> entity = new ExtendableEntity<>();
|
||||
entity.setId("test");
|
||||
entity.setExtension("extName", "test");
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
String json = mapper.writerFor(ExtensibleEntity.class).writeValueAsString(entity);
|
||||
String json = mapper.writerFor(ExtendableEntity.class).writeValueAsString(entity);
|
||||
|
||||
System.out.println(json);
|
||||
ExtensibleEntity<String> decoded = mapper.readerFor(ExtensibleEntity.class).readValue(json);
|
||||
ExtendableEntity<String> decoded = mapper.readerFor(ExtendableEntity.class).readValue(json);
|
||||
assertNotNull(decoded.getId());
|
||||
|
||||
assertEquals(entity.getId(), decoded.getId());
|
||||
@@ -4,13 +4,10 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hswebframework.web.api.crud.entity.ExtensibleEntity;
|
||||
import org.hswebframework.web.api.crud.entity.GenericEntity;
|
||||
import org.hswebframework.web.api.crud.entity.ExtendableEntity;
|
||||
import org.hswebframework.web.crud.annotation.EnableEntityEvent;
|
||||
import org.hswebframework.web.crud.generator.Generators;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Getter
|
||||
@@ -19,7 +16,7 @@ import javax.persistence.Table;
|
||||
@AllArgsConstructor(staticName = "of")
|
||||
@NoArgsConstructor
|
||||
@EnableEntityEvent
|
||||
public class TestEntity extends ExtensibleEntity<String> {
|
||||
public class TestEntity extends ExtendableEntity<String> {
|
||||
|
||||
@Column(length = 32)
|
||||
private String name;
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package org.hswebframework.web.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@AllArgsConstructor
|
||||
class ExtensibleToBeanCopier implements Copier {
|
||||
class ExtendableToBeanCopier implements Copier {
|
||||
|
||||
private final Copier copier;
|
||||
|
||||
@Override
|
||||
public void copy(Object source, Object target, Set<String> ignore, Converter converter) {
|
||||
copier.copy(source, target, ignore, converter);
|
||||
FastBeanCopier.copy(((Extensible) source).extensions(), target);
|
||||
FastBeanCopier.copy(((Extendable) source).extensions(), target);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
package org.hswebframework.web.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@AllArgsConstructor
|
||||
class ExtensibleToMapCopier implements Copier {
|
||||
class ExtendableToMapCopier implements Copier {
|
||||
|
||||
private final Copier copier;
|
||||
|
||||
@Override
|
||||
public void copy(Object source, Object target, Set<String> ignore, Converter converter) {
|
||||
copier.copy(source, target, ignore, converter);
|
||||
ExtensibleUtils.copyToMap((Extensible) source, ignore, (Map<String, Object>) target);
|
||||
ExtendableUtils.copyToMap((Extendable) source, ignore, (Map<String, Object>) target);
|
||||
//移除map中的extensions
|
||||
((Map<?, ?>) target).remove("extensions");
|
||||
}
|
||||
@@ -2,16 +2,16 @@ package org.hswebframework.web.bean;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ExtensibleUtils {
|
||||
public class ExtendableUtils {
|
||||
|
||||
public static void copyFromMap(Map<String, Object> source,
|
||||
Set<String> ignore,
|
||||
Extensible target) {
|
||||
Extendable target) {
|
||||
ClassDescription def = ClassDescriptions.getDescription(target.getClass());
|
||||
|
||||
for (Map.Entry<String, Object> entry : source.entrySet()) {
|
||||
@@ -23,7 +23,7 @@ public class ExtensibleUtils {
|
||||
|
||||
}
|
||||
|
||||
public static void copyToMap(Extensible target,
|
||||
public static void copyToMap(Extendable target,
|
||||
Set<String> ignore,
|
||||
Map<String, Object> source) {
|
||||
if (CollectionUtils.isNotEmpty(ignore)) {
|
||||
@@ -8,7 +8,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.beanutils.BeanUtilsBean;
|
||||
import org.apache.commons.beanutils.ConvertUtilsBean;
|
||||
import org.apache.commons.beanutils.PropertyUtilsBean;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
import org.hswebframework.utils.time.DateFormatter;
|
||||
import org.hswebframework.web.dict.EnumDict;
|
||||
import org.hswebframework.web.proxy.Proxy;
|
||||
@@ -180,8 +180,8 @@ public final class FastBeanCopier {
|
||||
if (tartName.startsWith("package ")) {
|
||||
tartName = tartName.substring("package ".length());
|
||||
}
|
||||
boolean targetIsExtensible = Extensible.class.isAssignableFrom(target);
|
||||
boolean sourceIsExtensible = Extensible.class.isAssignableFrom(source);
|
||||
boolean targetIsExtendable = Extendable.class.isAssignableFrom(target);
|
||||
boolean sourceIsExtendable = Extendable.class.isAssignableFrom(source);
|
||||
boolean targetIsMap = Map.class.isAssignableFrom(target);
|
||||
boolean sourceIsMap = Map.class.isAssignableFrom(source);
|
||||
|
||||
@@ -201,12 +201,12 @@ public final class FastBeanCopier {
|
||||
.create(Copier.class, new Class[]{source, target})
|
||||
.addMethod(method);
|
||||
Copier copier = proxy.newInstance();
|
||||
if (sourceIsExtensible && targetIsMap) {
|
||||
copier = new ExtensibleToMapCopier(copier);
|
||||
} else if (sourceIsMap && targetIsExtensible) {
|
||||
copier = new MapToExtensibleCopier(copier);
|
||||
}else if(sourceIsExtensible){
|
||||
copier = new ExtensibleToBeanCopier(copier);
|
||||
if (sourceIsExtendable && targetIsMap) {
|
||||
copier = new ExtendableToMapCopier(copier);
|
||||
} else if (sourceIsMap && targetIsExtendable) {
|
||||
copier = new MapToExtendableCopier(copier);
|
||||
}else if(sourceIsExtendable){
|
||||
copier = new ExtendableToBeanCopier(copier);
|
||||
}
|
||||
return copier;
|
||||
} catch (Exception e) {
|
||||
@@ -246,8 +246,8 @@ public final class FastBeanCopier {
|
||||
|
||||
Map<String, ClassProperty> targetProperties = null;
|
||||
|
||||
boolean targetIsExtensible = Extensible.class.isAssignableFrom(target);
|
||||
boolean sourceIsExtensible = Extensible.class.isAssignableFrom(source);
|
||||
boolean targetIsExtendable = Extendable.class.isAssignableFrom(target);
|
||||
boolean sourceIsExtendable = Extendable.class.isAssignableFrom(source);
|
||||
boolean targetIsMap = Map.class.isAssignableFrom(target);
|
||||
boolean sourceIsMap = Map.class.isAssignableFrom(source);
|
||||
//源类型为Map
|
||||
@@ -273,12 +273,12 @@ public final class FastBeanCopier {
|
||||
ClassProperty targetProperty = targetProperties.get(sourceProperty.getName());
|
||||
if (targetProperty == null) {
|
||||
//复制到拓展对象
|
||||
if (targetIsExtensible && !sourceIsExtensible && !sourceIsMap) {
|
||||
if (targetIsExtendable && !sourceIsExtendable && !sourceIsMap) {
|
||||
code.append("if(!ignore.contains(\"").append(sourceProperty.getName()).append("\")){\n\t");
|
||||
if (!sourceProperty.isPrimitive()) {
|
||||
code.append("if($$__source.").append(sourceProperty.getReadMethod()).append("!=null){\n");
|
||||
}
|
||||
code.append("\t\t((org.hswebframework.ezorm.core.Extensible)$$__target).setExtension(")
|
||||
code.append("\t\t((org.hswebframework.ezorm.core.Extendable)$$__target).setExtension(")
|
||||
.append("\"").append(sourceProperty.name).append("\",")
|
||||
.append("$$__source.").append(sourceProperty.getReadMethod())
|
||||
.append(");");
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package org.hswebframework.web.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.Extendable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@AllArgsConstructor
|
||||
class MapToExtensibleCopier implements Copier {
|
||||
class MapToExtendableCopier implements Copier {
|
||||
|
||||
private final Copier copier;
|
||||
|
||||
@@ -15,7 +15,7 @@ class MapToExtensibleCopier implements Copier {
|
||||
public void copy(Object source, Object target, Set<String> ignore, Converter converter) {
|
||||
copier.copy(source, target, ignore, converter);
|
||||
|
||||
ExtensibleUtils.copyFromMap((Map<String, Object>) source, ignore, (Extensible) target);
|
||||
ExtendableUtils.copyFromMap((Map<String, Object>) source, ignore, (Extendable) target);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
import org.hswebframework.ezorm.core.DefaultExtensible;
|
||||
import org.hswebframework.ezorm.core.Extensible;
|
||||
import org.hswebframework.ezorm.core.DefaultExtendable;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -27,12 +26,12 @@ public class FastBeanCopierTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testToExtensible() {
|
||||
public void testToExtendable() {
|
||||
Source source = new Source();
|
||||
source.setName("test");
|
||||
source.setAge(123);
|
||||
source.setColor(Color.RED);
|
||||
ExtensibleEntity e = FastBeanCopier.copy(source, new ExtensibleEntity());
|
||||
ExtendableEntity e = FastBeanCopier.copy(source, new ExtendableEntity());
|
||||
|
||||
Assert.assertEquals(source.getName(), e.getName());
|
||||
Assert.assertEquals(source.getAge(), e.getExtension("age"));
|
||||
@@ -41,7 +40,7 @@ public class FastBeanCopierTest {
|
||||
Map<String, Object> map = FastBeanCopier.copy(e, new HashMap<>());
|
||||
System.out.println(map);
|
||||
|
||||
ExtensibleEntity t = FastBeanCopier.copy(map, new ExtensibleEntity());
|
||||
ExtendableEntity t = FastBeanCopier.copy(map, new ExtendableEntity());
|
||||
Assert.assertEquals(e.getName(), t.getName());
|
||||
|
||||
System.out.println(e.extensions());
|
||||
@@ -51,9 +50,9 @@ public class FastBeanCopierTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromExtensible() {
|
||||
public void testFromExtendable() {
|
||||
Source source = new Source();
|
||||
ExtensibleEntity e = FastBeanCopier.copy(source, new ExtensibleEntity());
|
||||
ExtendableEntity e = FastBeanCopier.copy(source, new ExtendableEntity());
|
||||
e.setName("test");
|
||||
e.setExtension("age",123);
|
||||
FastBeanCopier.copy(e, source);
|
||||
@@ -63,13 +62,13 @@ public class FastBeanCopierTest {
|
||||
|
||||
}
|
||||
@Test
|
||||
public void testMapToExtensible() {
|
||||
public void testMapToExtendable() {
|
||||
Source source = new Source();
|
||||
source.setName("test");
|
||||
source.setAge(123);
|
||||
source.setColor(Color.RED);
|
||||
Map<String, Object> map = FastBeanCopier.copy(source, new HashMap<>());
|
||||
ExtensibleEntity e = FastBeanCopier.copy(map, new ExtensibleEntity());
|
||||
ExtendableEntity e = FastBeanCopier.copy(map, new ExtendableEntity());
|
||||
Assert.assertEquals(source.getName(), e.getName());
|
||||
Assert.assertEquals(source.getAge(), e.getExtension("age"));
|
||||
Assert.assertEquals(source.getColor(), e.getExtension("color"));
|
||||
@@ -78,7 +77,7 @@ public class FastBeanCopierTest {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class ExtensibleEntity extends DefaultExtensible {
|
||||
public static class ExtendableEntity extends DefaultExtendable {
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user