一种java拷贝属性的方法
来源:互联网 发布:python多线程selenium 编辑:程序博客网 时间:2024/05/18 01:51
- <pre code_snippet_id="129671" snippet_file_name="blog_20131226_1_6439347" name="code" class="java">import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- public @interface Convert {
- public String from();
- public String to();
- }
- /**
- * 将fromObject对象转换为toObject对象,fromObject必须在对应的get方法上有Convert注解
- */
- public static void convertTo(Object fromObject, Object toObject) {
- if (fromObject != null) {
- Method[] methods = fromObject.getClass().getMethods();
- for (Method method : methods) {
- if (method.isAnnotationPresent(Convert.class)) {
- Convert annotation = method.getAnnotation(Convert.class);
- String express = annotation.to();
- Object value;
- try {
- value = method.invoke(fromObject);
- if (value != null) {
- logger.debug(fromObject.getClass().getName() + " " + express + " is not null");
- Ognl.setValue(express, toObject, value);
- }
- } catch (OgnlException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert ognl exception:", e);
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- logger.error(fromObject.getClass().getName() + " " + express + " convert argument exception:", e);
- } catch (IllegalAccessException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert access exception:", e);
- } catch (InvocationTargetException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert invocation exception:", e);
- }
- }
- }
- }
- }
- /**
- * 将fromObject对象转换为toObject对象,toObject必须在对应的set方法上有Convert注解
- */
- public static void convert(Object fromObject, Object toObject) {
- if (fromObject != null) {
- Method[] methods = toObject.getClass().getMethods();
- for (Method method : methods) {
- if (method.isAnnotationPresent(Convert.class)) {
- Convert annotation = method.getAnnotation(Convert.class);
- String express = annotation.from();
- Object value;
- try {
- value = Ognl.getValue(express, fromObject);
- if (value != null) {
- logger.debug(fromObject.getClass().getName() + " " + express + " is not null");
- method.invoke(toObject, value);
- }
- } catch (OgnlException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert ognl exception:", e);
- } catch (IllegalArgumentException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert argument exception:", e);
- } catch (IllegalAccessException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert access exception:", e);
- } catch (InvocationTargetException e) {
- logger.error(fromObject.getClass().getName() + " " + express + " convert invocation exception:", e);
- }
- }
- }
- }
- }
- </pre><br>
- <br>
- <pre></pre>
0 0
- 一种java拷贝属性的方法
- 一种java拷贝属性的方法
- ArrayList深拷贝的一种实现方法
- 深拷贝的一种实现方法
- ArrayList深拷贝的一种实现方法
- ArrayList深拷贝的一种实现方法
- 一种查询表详细属性的方法
- 【转】ArrayList深拷贝的一种实现方法
- 将数组一次性拷贝到vector的一种方法
- 类属性拷贝方法
- java 属性拷贝
- Java的clone方法--深拷贝和浅拷贝
- java Object的clone方法,浅拷贝与深拷贝
- 类似BeanUtils.copyProperties拷贝对象属性值的方法
- iOS:一种直接修改frame的某个属性的方法
- 【Java】 数组拷贝的通用方法
- Java数组拷贝的四种方法
- Java实现文件拷贝的方法
- Qt中MainWindow类实例
- openmq-2
- 往~/.bashrc 文件添加JAVA_HOME导致 该文件错误,sudo等指令无法使用
- angularjs 学习 指令学习一
- Java 7 Fork/Join 并行计算框架概览
- 一种java拷贝属性的方法
- Git系列一——Git简介
- 关于java.lang.NoSuchMethodError的分析,产生的原因及处理方式
- 数学工具的物理涵义
- typedef使用注意
- HTML5学习(一)
- 存储过程
- 20151130正则第一部分(grep)
- 【Android导航 小细节 大成功】项目感悟