xml与反射应用(一)

感觉先交个作业能来点儿斗志,先写一部分哈哈

我们的目的是:将下面的xml文件解析得到类名,方法名,类型和值(不知type、value的类型及个数),并且对value进行加法操作

<funs>
	<fun class="zhengshu" method="add">
		<para type="int" value="32"></para>
		<para type="int" value="2"></para>
	</fun>
</funs>

经过(超级超级艰辛的过程)分析得到,首先我们完成一个para类,实现对type,value的类型,由String类型转换成我们需要的类型。
para是解决这个问题最基础的一个类,他的作用很单一很绝对,仅仅是将xml文件内的para标签下的内容进行处理

  • 关于将String类型转换成我们需要的类型可以用HashMap,也可以用逻辑语句,这里最终用了逻辑语句,当中也给出了HashMap的操作。
  • 初步版本仅仅处理了type为八大基本类型这一情况
	//操作前
	private String type;
	private String value;
	//操作后
	private Class<?> types;
	private Object values;
	
package com.mec.aboutTry;

import java.util.HashMap;

public class para {
	private String type;
	private String value;
	private Class<?> types;
	private Object values;
	
	public para() {
	}	
//	HashMap<String, Class> typeMap = new HashMap<> ();
//	这里是提前的HashMap版本。
//	@SuppressWarnings("rawtypes")
//	private final static HashMap<String, Class> typeMap;
//	static {
//		typeMap = new HashMap<>();
//		typeMap.put("int", int.class);
//		typeMap.put("double", double.class);
//		typeMap.put("char", char.class);
//		typeMap.put("short", short.class);
//		typeMap.put("boolean", boolean.class);
//		typeMap.put("byte", byte.class);
//		typeMap.put("long", long.class);
//	}
	//type 的string类型变成type类型
	public void tStringToType(String type) {
		if(type.equals("int")) {
			this.types = int.class;
		}
		if(type.equals("double")) {
			this.types = double.class;
		}
		if(type.equals("short")) {
			this.types = short.class;
		}
		if(type.equals("long")) {
			this.types = long.class;
		}
		if(type.equals("boolean")) {
			this.types = boolean.class;
		}
		if(type.equals("byte")) {
			this.types = byte.class;
		}
		if(type.equals("char")) {
			this.types = char.class;
		}
		if(type.equals("long")) {
			this.types = long.class;
		}
	}
	
	
	//value的string类型变成type类型
	public void vStringToType(String value) {
		if(type.equals("int")) {
			this.values = int.class;
		}
		if(type.equals("double")) {
			this.values = double.class;
		}
		if(type.equals("short")) {
			this.values = short.class;
		}
		if(type.equals("long")) {
			this.values = long.class;
		}
		if(type.equals("boolean")) {
			this.values = boolean.class;
		}
		if(type.equals("byte")) {
			this.values = byte.class;
		}
		if(type.equals("char")) {
			this.values = char.class;
		}
		if(type.equals("long")) {
			this.types = long.class;
		}		
		
	}
	
	
	public String toString() {
		return "para [type=" + types + ", value=" + values + "]";
	}
//////////////////////////////////////////////////////////////////////////////////////////用
	public Class<?> getType() {
		return types;
	}

	public void setType(String type) {
		tStringToType(type);
		this.type = type;
	}

	public Object getValue() {
		return values;
	}

	public void setValue(String value) {
		vStringToType(value);
		this.value = value;
	}
///////////////////////////////////////////////////////////////
	public Class<?> getTypes() {
		return types;
	}

	public void setTypes(Class<?> types) {
		this.types = types;
	}

	public Object getValues() {
		return values;
	}

	public void setValues(Object values) {
		this.values = values;
	}
	    

}

由HashMap实现的操作如下
而且这里处理了当type不是八大基本类型的操作

package com.mec.xml_reflect.core;

import java.util.HashMap;
import java.util.Map;

public class Para {
	private static final Map<String, Class<?>> typeMap;
	
	static {
		typeMap = new HashMap<>();
		typeMap.put("int", int.class);
		typeMap.put("string", String.class);
		typeMap.put("byte", byte.class);
		typeMap.put("char", char.class);
		typeMap.put("short", short.class);
		typeMap.put("long", long.class);
		typeMap.put("float", float.class);
		typeMap.put("double", double.class);
		typeMap.put("boolean", boolean.class);
	}
	
	private Class<?> type;
	private String name;
	private Object value;
	
	public Para() {
	}

	public void setType(String typeName) {
		Class<?> paraType = typeMap.get(typeName);
		if (paraType == null) {
			try {
				paraType = Class.forName(typeName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		type = paraType;
	}
	
	public Class<?> getType() {
		return type;
	}
	
	public void setValue(String str) {
		value = parseValue(str);
	}
	
	public Object getValue() {
		return value;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private Object parseValue(String str) {
	//对不属于八大基本类型的type,返回null
		Object result = null;

		if (type.equals(String.class)) {
			return str;
		}
		if (type.equals(char.class)) {
			return str.charAt(0);
		}
		if (type.equals(short.class)) {
			return Short.valueOf(str);
		}
		if (type.equals(long.class)) {
			return Long.valueOf(str);
		}
		if (type.equals(float.class)) {
			return Float.valueOf(str);
		}
		if (type.equals(double.class)) {
			return Double.valueOf(str);
		}
		if (type.equals(boolean.class)) {
			return Boolean.valueOf(str);
		}
		if (type.equals(int.class)) {
			return Integer.valueOf(str);
		}
		
		return result;
	}
	
}

好嘞先收摊,未完尽快续…

发布了26 篇原创文章 · 获赞 4 · 访问量 2392

猜你喜欢

转载自blog.csdn.net/weixin_43257196/article/details/96571888
今日推荐