From b0860f8556c84abf27cb255bc273f1bad78358e9 Mon Sep 17 00:00:00 2001 From: smallchill Date: Fri, 28 Dec 2018 16:34:37 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springblade/core/log/model/LogApiVo.java | 4 +- .../core/log/model/LogErrorVo.java | 4 +- .../core/log/model/LogUsualVo.java | 4 +- ...adeBeanCopier.java => BaseBeanCopier.java} | 47 +++++++++++++++---- .../springblade/core/tool/utils/BeanUtil.java | 6 +-- 5 files changed, 46 insertions(+), 19 deletions(-) rename blade-core-tool/src/main/java/org/springblade/core/tool/support/{BladeBeanCopier.java => BaseBeanCopier.java} (77%) diff --git a/blade-core-log/src/main/java/org/springblade/core/log/model/LogApiVo.java b/blade-core-log/src/main/java/org/springblade/core/log/model/LogApiVo.java index 5262426..092d745 100644 --- a/blade-core-log/src/main/java/org/springblade/core/log/model/LogApiVo.java +++ b/blade-core-log/src/main/java/org/springblade/core/log/model/LogApiVo.java @@ -1,11 +1,11 @@ /** * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). *

- * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/blade-core-log/src/main/java/org/springblade/core/log/model/LogErrorVo.java b/blade-core-log/src/main/java/org/springblade/core/log/model/LogErrorVo.java index 437ffd2..d7f11ba 100644 --- a/blade-core-log/src/main/java/org/springblade/core/log/model/LogErrorVo.java +++ b/blade-core-log/src/main/java/org/springblade/core/log/model/LogErrorVo.java @@ -1,11 +1,11 @@ /** * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). *

- * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/blade-core-log/src/main/java/org/springblade/core/log/model/LogUsualVo.java b/blade-core-log/src/main/java/org/springblade/core/log/model/LogUsualVo.java index a908997..728f5d4 100644 --- a/blade-core-log/src/main/java/org/springblade/core/log/model/LogUsualVo.java +++ b/blade-core-log/src/main/java/org/springblade/core/log/model/LogUsualVo.java @@ -1,11 +1,11 @@ /** * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). *

- * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/lgpl.html *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/support/BladeBeanCopier.java b/blade-core-tool/src/main/java/org/springblade/core/tool/support/BaseBeanCopier.java similarity index 77% rename from blade-core-tool/src/main/java/org/springblade/core/tool/support/BladeBeanCopier.java rename to blade-core-tool/src/main/java/org/springblade/core/tool/support/BaseBeanCopier.java index 7b389a5..a8d590c 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/support/BladeBeanCopier.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/support/BaseBeanCopier.java @@ -1,3 +1,18 @@ +/** + * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springblade.core.tool.support; import org.springblade.core.tool.utils.BeanUtil; @@ -21,22 +36,29 @@ import java.util.Map; * * @author L.cm */ -public abstract class BladeBeanCopier { +public abstract class BaseBeanCopier { private static final BeanCopierKey KEY_FACTORY = (BeanCopierKey) KeyFactory.create(BeanCopierKey.class); private static final Type CONVERTER = TypeUtils.parseType("org.springframework.cglib.core.Converter"); - private static final Type BEAN_COPIER = TypeUtils.parseType(BladeBeanCopier.class.getName()); + private static final Type BEAN_COPIER = TypeUtils.parseType(BaseBeanCopier.class.getName()); private static final Signature COPY = new Signature("copy", Type.VOID_TYPE, new Type[]{Constants.TYPE_OBJECT, Constants.TYPE_OBJECT, CONVERTER}); private static final Signature CONVERT = TypeUtils.parseSignature("Object convert(Object, Class, Object)"); interface BeanCopierKey { + /** + * 实例化 + * @param source + * @param target + * @param useConverter + * @return + */ Object newInstance(String source, String target, boolean useConverter); } - public static BladeBeanCopier create(Class source, Class target, boolean useConverter) { - return BladeBeanCopier.create(source, target, null, useConverter); + public static BaseBeanCopier create(Class source, Class target, boolean useConverter) { + return BaseBeanCopier.create(source, target, null, useConverter); } - public static BladeBeanCopier create(Class source, Class target, ClassLoader classLoader, boolean useConverter) { + public static BaseBeanCopier create(Class source, Class target, ClassLoader classLoader, boolean useConverter) { Generator gen; if (classLoader == null) { gen = new Generator(); @@ -49,10 +71,16 @@ public abstract class BladeBeanCopier { return gen.create(); } + /** + * 拷贝 + * @param from + * @param to + * @param converter + */ abstract public void copy(Object from, Object to, Converter converter); public static class Generator extends AbstractClassGenerator { - private static final Source SOURCE = new Source(BladeBeanCopier.class.getName()); + private static final Source SOURCE = new Source(BaseBeanCopier.class.getName()); private final ClassLoader classLoader; private Class source; private Class target; @@ -97,9 +125,9 @@ public abstract class BladeBeanCopier { return ReflectUtils.getProtectionDomain(source); } - public BladeBeanCopier create() { + public BaseBeanCopier create() { Object key = KEY_FACTORY.newInstance(source.getName(), target.getName(), useConverter); - return (BladeBeanCopier) super.create(key); + return (BaseBeanCopier) super.create(key); } @Override @@ -120,7 +148,7 @@ public abstract class BladeBeanCopier { // 2018.12.27 by L.cm 支持链式 bean PropertyDescriptor[] getters = BeanUtil.getBeanGetters(source); PropertyDescriptor[] setters = BeanUtil.getBeanSetters(target); - Map names = new HashMap<>(); + Map names = new HashMap<>(16); for (PropertyDescriptor getter : getters) { names.put(getter.getName(), getter); } @@ -167,7 +195,6 @@ public abstract class BladeBeanCopier { } private static boolean compatible(PropertyDescriptor getter, PropertyDescriptor setter) { - // TODO: allow automatic widening conversions? return setter.getPropertyType().isAssignableFrom(getter.getPropertyType()); } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java index a5811a4..7c23532 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/BeanUtil.java @@ -17,7 +17,7 @@ package org.springblade.core.tool.utils; import org.springblade.core.tool.support.BeanProperty; -import org.springblade.core.tool.support.BladeBeanCopier; +import org.springblade.core.tool.support.BaseBeanCopier; import org.springframework.beans.BeansException; import org.springframework.cglib.beans.BeanGenerator; import org.springframework.cglib.beans.BeanMap; @@ -109,7 +109,7 @@ public class BeanUtil extends org.springframework.beans.BeanUtils { * @return T */ public static T copy(Object source, Class clazz) { - BladeBeanCopier copier = BladeBeanCopier.create(source.getClass(), clazz, false); + BaseBeanCopier copier = BaseBeanCopier.create(source.getClass(), clazz, false); T to = newInstance(clazz); copier.copy(source, to, null); @@ -125,7 +125,7 @@ public class BeanUtil extends org.springframework.beans.BeanUtils { * @param targetBean 需要赋值的对象 */ public static void copy(Object source, Object targetBean) { - BladeBeanCopier copier = BladeBeanCopier + BaseBeanCopier copier = BaseBeanCopier .create(source.getClass(), targetBean.getClass(), false); copier.copy(source, targetBean, null);