反射从配置文件中读取
程序员文章站
2022-03-13 10:53:59
...
className=teacher.TT con_A_count=3 con_Args1=int con_Args2=String con_Args3=int con_obj1=12 con_obj2=zheng con_obj3=22 me_Aa_name=test me_Ab_count=3 me_Args1=int me_Args2=String me_Args3=int me_obj1=22 me_obj2=郑云飞 me_obj3=22
package hashMap;
import java.io.FileInputStream;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
* @author 郑云飞
* 内容:反射从配置文件中读取信息
* date:2010年9月27日PM
*/
class ReflectTest
{
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception
{
Properties pp=new Properties();
List<String> ls=new ArrayList<String>();
pp.load(new FileInputStream("propers.ini"));//加载配置文件内容
Iterator<String> it1=(Iterator<String>) pp.propertyNames();
while(it1.hasNext())
{
ls.add(it1.next());//将配置文件里的键存放的容器中
}
Collections.sort(ls);//对容器进行自然排序
String[] s = new String[ls.size()];
for(int i=0;i<ls.size();i++)
{
byte []data=pp.getProperty(ls.get(i)).getBytes("iso8859-1");//设置字符编码
ls.set(i, new String(data,"gb2312"));//为容器ls赋值
s[i]=ls.get(i);//将容器ls复制到字符串数组s中
}
Class<?> cl = Class.forName(s[0]);//获得要调用的类名
Object o = getObject(cl, s);//获得对象
invoke(cl, o, s);//通过对象调用方法
}
//用反射产生对象并返回该对象
public static Object getObject(Class<?> cl,String[] args) throws Exception
{
int con_Count = Integer.parseInt(args[1]);
Object o = null;
if(con_Count==0)
{
o = cl.newInstance();
}
else
{
Class<?>[] con_Args = new Class[con_Count];
Object[] obj_Args = new Object[con_Count];
Object[] oo = null;
for(int i=0;i<con_Count;i++)
{
oo = turn(args[2+i],args[2+i+con_Count]);
con_Args[i] = (Class<?>)oo[0];
obj_Args[i] = oo[1];
}
Constructor<?> con = cl.getDeclaredConstructor(con_Args);
o = con.newInstance(obj_Args);
}
return o;
}
//用反射执行某一方法并返回结果
public static Object invoke(Class<?> cl,Object o,String[] args) throws Exception
{
int con_Count = Integer.parseInt(args[1]);
String methodName = args[2+2*con_Count];
int me_Count = Integer.parseInt(args[3+2*con_Count]);
Class<?>[] me_Args = new Class[me_Count];
Object[] me_Obj_Args = new Object[me_Count];
Object[] oo = null;
for(int i=0;i<me_Count;i++)
{
oo = turn(args[4+2*con_Count+i],args[4+2*con_Count+i+me_Count]);
me_Args[i] = (Class<?>)oo[0];
me_Obj_Args[i] = oo[1];
}
Method m = cl.getDeclaredMethod(methodName,me_Args);
Object res = m.invoke(o,me_Obj_Args);
//System.out.println("方案执行结果:"+res);
return res;
}
public static Object[] turn(String cType,String oArg)
{
Object[] o = new Object[2];
if(cType.equals("int"))
{
o[0] = int.class;
o[1] = Integer.parseInt(oArg);
}
if(cType.equals("String"))
{
o[0] = String.class;
o[1] = oArg;
}
if(cType.equals("float"))
{
o[0] = float.class;
o[1] = Float.parseFloat(oArg);
}
if(cType.equals("char"))
{
o[0] = char.class;
o[1] = oArg.charAt(0);
}
if(cType.equals("boolean"))
{
o[0] = boolean.class;
o[1] = Boolean.parseBoolean(oArg);
}
return o;
}
}
class TT
{
int age;
String name;
int id;
public TT(int age,String name,int id)
{
this.age=age;
this.name=name;
this.id=id;
}
public static void test(int age,String name,int id)
{
System.out.println("从配置文件中读取到的方法的信息为:\n"+"年龄="+age+"\t姓名="+name+" 编号="+id);
}
@Override
public String toString()
{
// TODO Auto-generated method stub
return "年龄:"+age+"姓名:"+name+"编号:"+id;
}
}
上一篇: 测试你的OO水平