你可以利用java反射机制就可以处理.如果我有空就帮你写写看.
我简单写了一下
package action;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
/**
* @param args
*/
//这是测试主方法
public static void main(String[] args) {
Test test = new Test();
CacheNumber ch = (CacheNumber) test.createObj(CacheNumber.class,
"capacity=1,count=2,name=wang,montey=21.3");
System.out.println(ch.getCapacity());
System.out.println(ch.getName());
System.out.println(ch.getCount());
}
public Object createObj(Class cl, String keyAndValue) {
Object te = null;
try {
Class cla = Class.forName(cl.getName());
te = cla.newInstance();
String[] valueAndName = keyAndValue.split(",");
for (int i = 0; i < valueAndName.length; i++) {
Field[] field = cla.getDeclaredFields();
for (int k = 0; k < field.length; k++) {
Class[] paraTypes = new Class[] { field[k].getType() };
String name = valueAndName[i].substring(0, valueAndName[i]
.indexOf("="));
String value = valueAndName[i].substring(valueAndName[i]
.indexOf("=") + 1, valueAndName[i].length());
if (field[k].getName().equals(name)) {
String fieldSetter = "set"
+ name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Method method = cla.getMethod(fieldSetter, paraTypes);
Object[] values = null;
if (field[i].getType()==int.class) {
values = new Integer[] { Integer.valueOf(value) };
}
if (field[i].getType()==String.class) {
values = new String[] { value };
}
if (field[i].getType()==double.class) {
values = new Double[] { Double.valueOf(value) };
}
if(field[i].getType()==short.class){
values = new Short[]{Short.valueOf(value)};
}
if(field[i].getType()==long.class){
values = new Long[]{Long.valueOf(value)};
}
method.invoke(te, values);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
} catch (InstantiationException e) {
e.printStackTrace();
}
return te;
}
}
//这是你的CacheNumber我又加了几个类型的属性
package action;
public class CacheNumber {
private int capacity;
private int count;
private String name;
private double montey;
public double getMontey() {
return montey;
}
public void setMontey(double montey) {
this.montey = montey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
// 默认构造器
public CacheNumber() {
}
public CacheNumber(int capacity, int count) {
this.capacity = capacity;
this.count = count;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public static void main(String[] args) {
}
}
写的很简单,你看哪块不合适再改一改吧,思路就是这么个思路.
写了一个,既然是用于hibernate保存,那对象应该提供标准的set和get方法,并提供默认public的构造方法:
-------------Reflect.java------------------------
//package com.color.encoding;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Reflect {
/**
*
* @param c
* @param fieldsAndValuelike"a=??,b=??,c=??"
* @author color4you
* @return
*/
public Object createObj(Class c, String fieldsAndValue) {
Object obj = null;
Constructor con = null;
String t[] = null;
String[] values = null;
String[] keys = null;
Class[] clazz = null;
if(fieldsAndValue != null){
t = fieldsAndValue.split(",");
}
else{
t = new String[]{};
}
values = new String[t.length];
keys =new String[t.length];
clazz= new Class[t.length];
for (int i = 0; i < t.length; i++) {
String[] keyAndValue = t[i].split("=");
keys[i] = keyAndValue[0];
values[i] = keyAndValue[1];
clazz[i] = this.getFieldType(c, keys[i]);
}
try {
// 先查询是否有按参数输入类型的构造器
con = c.getConstructor(clazz);
Object[] o = new Object[t.length];
for (int i = 0; i < keys.length; i++) {
o[i] = this.getPropertyByClass(clazz[i], values[i]);
}
try {
obj = con.newInstance(o);
return obj;
} catch (Exception e) {
// 如果没有,查询默认构造器
// 此构造器是必须存在的,其访问级别为public
// 并且所有属性提供标准,get和set方法
try {
con = c.getConstructor();
obj = con.newInstance();
} catch (Exception e2) {
e2.printStackTrace();
}
for (int i = 0; i < t.length; i++) {
try {
String[] keyAndValue = t[i].split("=");
Method m = c.getMethod(this
.getSetMethodByField(keyAndValue[0]),
new Class[] { this.getFieldType(c,
keyAndValue[0]) });
m.invoke(obj, new Object[] { this.getPropertyByClass(
this.getFieldType(c, keyAndValue[0]),
keyAndValue[1]) });
} catch (NoSuchMethodException ex) {
System.out.println("未查寻到有此set方法:"
+ t[i].substring(0, t[i].indexOf("=")));
} catch (Exception e1) {
e.printStackTrace();
}
}
return obj;
}
} catch (Exception e) {
System.out.println("------没有找到相应的构造器------");
e.printStackTrace();
} finally {
System.out.println("---------执行完毕----------");
}
return obj;
}
/**
* 根据类型,返回此类型的值
*
* @param c
* @param value
* @return
* @throws ParseException
*/
public Object getPropertyByClass(Class c, String value) {
if (c.equals(int.class)) {
return Integer.parseInt(value);
} else if (c.equals(Long.class)) {
return Long.parseLong(value);
} else if (c.equals(short.class)) {
return (short) Integer.parseInt(value);
} else if (c.equals(char.class)) {
return (char) Integer.parseInt(value);
} else if (c.equals(double.class) || c.equals(Double.class)) {
return Double.parseDouble(value);
} else if (c.equals(Date.class)) {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
try {
return f.parse(value);
} catch (ParseException e) {
System.out.println("解析时间类型出错:" + value);
e.printStackTrace();
}
}
return null;
}
/**
* 根据字段得到其数据类型
*
* @param c
* @param field
* @return
* @throws Exception
* @throws Exception
*/
public Class getFieldType(Class c, String field) {
Field f = null;
try {
f = c.getDeclaredField(field);
} catch (SecurityException e) {
System.exit(-1);
e.printStackTrace();
} catch (NoSuchFieldException e) {
System.out.println("字段不存在:" + field);
System.exit(-1);
e.printStackTrace();
}
return f.getType();
}
/**
* 根据字段查询其set方法
*
* @param field
* @return
*/
public String getSetMethodByField(String field) {
return "set" + field.substring(0, 1).toUpperCase() + field.substring(1);
}
public static void main(String[] args) throws Exception, Exception {
Class clazz = CacheNumber.class;
Reflect r = new Reflect();
Object o = r.createObj(clazz, "capacity=2,count=3");
if (o instanceof CacheNumber) {
CacheNumber c = (CacheNumber) o;
System.out.println(c.getCapacity());
System.out.println(c.getCount());
}
Class clazz2 = Demo.class;
Object o2 = r.createObj(clazz2,null);
if(o2 instanceof Demo){
Demo ops = (Demo)o2;
ops.main(new String[]{});
}
}
}
----------测试类---------------
//package com.color.encoding;
public class CacheNumber {
private int capacity;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
//默认构造器
public CacheNumber(){
System.out.println("----call default constructor:"+this.getClass().getName());
}
// 程序先会调用这个构造器,如果不存在,
// 再调用默认构造器,所以默认构造器必须存在。
public CacheNumber(int capacity,int count){
System.out.println("---call appointed constructor:"+this.getClass().getName());
this.capacity = capacity;
this.count = count;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity,int count) {
this.capacity = capacity;
this.count = count;
}
public static void main(String[] args) {
}
}
--------------测试类-----------------
//package com.color.encoding;
public class Demo {
public Demo(){
System.out.println("---call default method:"+this.getClass().getName());
}
public static void main(String[] args){
System.out.println("---hello,call demo main method!---");
}
}
恩,用反射机制可以做到,下午写给你个小例子吧。
很简单的,不过你的输入参数最好改一下。
我只看明白了一点,不知道它说的什么意思1
没太明白你的意思