首页 > 编程知识 正文

java什么是类,java强制类型转换int

时间:2023-05-03 23:00:35 阅读:170736 作者:3253

packagecom.dongl.utils; importorg.dozer.dozerbeanmapper; importorg.spring framework.beans.beans exception; importorg.spring framework.beans.fatalbeanexception; importorg.spring framework.util.assert; importorg.spring framework.util.string utils; import Java.beans.property descriptor; import Java.lang.reflect.invocationtargetexception; importjava.lang.reflect.Method; import Java.lang.reflect.modifier; import java.util.*; /***@authorD-L

* @Classname BeanUtils

* @版本1.0

* @如何在不使用参数的情况下实例化* @Description对象并复制属性值

*完成实体类之间的转换

map-完成实体类

*实体类-映射

基于Dozer变换对象的类型

为bean的特定属性赋值

* @Date 2020/8/5*/

publicclassbeanutilsextendsorg.spring framework.beans.beanutils {/* * *维护着Dozer的单个实例,并通过重新创建DozerMapper来消耗资源. */

privatestaticdozerbeanmapperdozer=newdozerbeanmapper (; /**通过实例化目标对象而不使用任何参数并复制属性,将POJO对象转换为相应的对象

*

*@paramsource源对象

*@paramtype目标类型

*@return转换的对象*

publicstatictconvert (对象源,Classtype ) {

target=实例类(type ); if(source==null ) {returntarget;

}

copyproperties(source,target ); 返回目标;

}/***通过实例化目标对象而不使用任何参数并复制属性,按顺序将两个POJO对象转换为相应的对象

*如果原始对象具有相同的属性,则将复盖属性值

*@paramsource1原始对象1

*@paramsource2二元对象2

*@paramtype目标类型

*@param

*@return返回变换后的对象*

publicstatictconverts (对象源1,对象源2,Classtype ) {

target=实例类(type ); source1==null source2==null ) {returntarget;

}

复制属性(source 1,target );

复制属性(source 2,target ); 返回目标;

}/***通过实例化目标对象而不使用任何参数并复制属性,将POJO对象转换为相应的对象。 某些属性可以忽略

*@paramsource源对象

*@paramtype目标类型

*@paramignoreProperties要忽略的属性列表

*@return转换的对象*

publicstatictconvert (对象源,Classtype,String[] ignoreProperties ) {

target=实例类(type );

copyproperties(source,target,ignoreProperties ); 返回目标;

}/***通过实例化目标对象而不使用任何参数并复制属性,将POJO对象批量转换为指定类型的对象

*@paramsources List原始对象列表

*@paramtype目标类型

*@returnList目标类型的对象列表*

publicstaticlistconvert (list sources,Classtype ) {

List items=new ArrayList (; if(sources==null ) {returnitems;

}

迭代器=sourc

es.iterator();while(iter.hasNext()) {

items.add(convert(iter.next(), type));

}returnitems;

}/*** 通过无参数实例化目标对象和复制属性,将POJO对象批量转换成指定类型的对象

*

*@paramsources

* List 原对象列表

*@paramtype

* 目标类型

*@paramignoreProperties

* 忽略的属性列表

*@returnList 目标类型对象列表*/

public static List convert(List sources, Classtype,

Class>editable, String[] ignoreProperties) {

List items = new ArrayList();if (sources == null) {returnitems;

}

Iterator iter =sources.iterator();while(iter.hasNext()) {

items.add(convert(iter.next(), type, ignoreProperties));

}returnitems;

}/*** 将Map的属性设置到bean中

*

*@parambean

*@paramproperties

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public static void populate(Object bean, Mapproperties)throwsIllegalAccessException, InvocationTargetException {

org.apache.commons.beanutils.BeanUtils.populate(bean, properties);

}/*** 将Bean转化为Map对象

*

*@parambean

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Map, ?>describe(Object bean)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {returnorg.apache.commons.beanutils.BeanUtils.describe(bean);

}/*** 对bean的特定属性赋值

*

*@parambean

*@paramname

*@paramvalue

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public static voidcopyProperty(Object bean, String name, Object value)throwsIllegalAccessException, InvocationTargetException {

org.apache.commons.beanutils.BeanUtils.copyProperty(bean, name, value);

}/*** 获取bean的特定属性

*

*@parambean

*@paramname

*@throwsNoSuchMethodException

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public staticString getProperty(Object bean, String name)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {returnorg.apache.commons.beanutils.BeanUtils.getProperty(bean, name);

}/*** 基于Dozer转换对象的类型.

*

*@paramsource

*@paramdestinationClass

*@return

*/

public static T map(Object source, ClassdestinationClass) {returndozer.map(source, destinationClass);

}/*** 基于Dozer转换List中对象的类型.

*

*@paramsourceList

*@paramdestinationClass

*@return

*/

public static List map(ListsourceList,

ClassdestinationClass) {

List destinationList = new ArrayList();for(Object sourceObject : sourceList) {

T destinationObject=dozer.map(sourceObject, destinationClass);

destinationList.add(destinationObject);

}returndestinationList;

}/*** 复制对象到一个已经存在的对象

*

*@paramsource

* 源对象

*@paramtarget

* 目标对象*/

public static voidmapProperties(Object source, Object target) {

dozer.map(source, target);

}/*** 复制对象的指定属性

*@paramsource

*@paramtarget

*@paramnames

*@throwsBeansException*/

public static voidcopyPropertiesByNames(Object source, Object target,

String names)throwsBeansException {

Assert.notNull(source,"Source must not be null");

Assert.notNull(target,"Target must not be null");

Assert.hasText(names,"names must not be empty");

String[] keys= names.split(",");for(String name : keys) {

name=name.trim();

PropertyDescriptor targetPd=getPropertyDescriptor(

target.getClass(), name);

PropertyDescriptor sourcePd=getPropertyDescriptor(

source.getClass(), name);if (sourcePd != null && sourcePd.getReadMethod() != null

&& targetPd != null && targetPd.getWriteMethod() != null) {try{

Method readMethod=sourcePd.getReadMethod();if (!Modifier.isPublic(readMethod.getDeclaringClass()

.getModifiers())) {

readMethod.setAccessible(true);

}

Object value=readMethod.invoke(source);

Method writeMethod=targetPd.getWriteMethod();if (!Modifier.isPublic(writeMethod.getDeclaringClass()

.getModifiers())) {

writeMethod.setAccessible(true);

}

writeMethod.invoke(target, value);

}catch(Throwable ex) {throw newFatalBeanException("Could not copy properties from source to target",

ex);

}

}

}

}/*** 将Bean的指定属性集转化为Map对象

*

*@parambean

*@paramnames

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Mapdescribe(Object bean, String names)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {

String[] attrs= StringUtils.split(names, ",");for (int i = 0; i < attrs.length; i++) {

attrs[i]=attrs[i].trim();

}returndescribe(bean, attrs);

}/*** 将Bean的指定属性集转化为Map对象

*

*@parambean

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Mapdescribe(Object bean, String[] names)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {

Assert.notNull(bean,"Source must not be null");

Assert.notEmpty(names,"names must not be empty");

Map map = new HashMap();for(String name : names) {

map.put(name, getProperty(bean, name));

}returnmap;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。