您的位置:首页 > Web前端 > Node.js

Java XML解析,,Node直接转为对象。考虑了一般的类,简单类型,数组,还未考虑List,Map

2012-04-23 15:28 751 查看
反射类

package com.supermap.services.components.tilecache.convert;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

import com.supermap.services.components.commontypes.OutputFormat;

public class ReflectionUtil {

/**
* 给属性赋值[默认包括了字段]
* @param obj
* @param proName
* @param value
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void setPropertyValue(Object obj,String proName,Object value) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
BeanInfo beanInfo= Introspector.getBeanInfo(obj.getClass());
for(PropertyDescriptor prop : beanInfo.getPropertyDescriptors()){
if(prop.getName().equals(proName)){
Class<?> propType =prop.getReadMethod().getReturnType();
Object porpvalue = getValue(propType, value);
prop.getWriteMethod().invoke(obj, porpvalue);
return ;
}
}

for(java.lang.reflect.Field field : obj.getClass().getFields()){
if( field.getName().equals(proName)){
Object filedValue= getValue(field.getType(),value);
field.set(obj, filedValue);
return ;
}
}
}

/**
* 得到属性的类别
* @param obj
* @param proName
* @return
* @throws IntrospectionException
*/
public static Class<?> getPropertyType(Object obj,String proName) throws IntrospectionException{
BeanInfo beanInfo= Introspector.getBeanInfo(obj.getClass());
for(PropertyDescriptor prop : beanInfo.getPropertyDescriptors()){
if(prop.getName().equals(proName)){
return prop.getReadMethod().getReturnType();
}
}
for(java.lang.reflect.Field field : obj.getClass().getFields()){
if( field.getName().equals(proName)){
return field.getType();
}
}
return null;
}

/**
* 把obj转成type类型
* @param type
* @param obj
* @return
*/
public static Object getValue(Class<?> type,Object obj){
String className = type.getName();
if(obj.getClass() == type){
return obj;
}
if(type .equals(Double.class) ||className=="double"){
return Double.parseDouble(obj.toString());
}
if(type==Float.class||className=="float"){
return Float.parseFloat(obj.toString());
}
if(type==Integer.class||className=="int"){
return Integer.parseInt(obj.toString());
}
if(type.equals( String.class)||className=="string"){
return obj.toString();
}
if(type.equals(Boolean.class)||className=="boolean"){
return Boolean.parseBoolean(obj.toString());
}
if(type.isEnum()){
Class<?>[] params = new Class<?>[1];
params[0] = String.class;
try {
return type.getDeclaredMethod("valueOf", params).invoke(null, obj.toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//if(type.equals(Enum))
return null;
}

public static void main(String[] argc){
OutputFormat format = OutputFormat.BINARY;
//OutputFormat.valueOf(name)
//format.valueOf(name)
OutputFormat myEnum= (OutputFormat) getValue(format.getClass(),"BINARY");
System.out.println(format.toString());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐