`
topbox163
  • 浏览: 51852 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

反射 reflect 根据方法名得到方法,并根据不同的数据类型参数和不同的返回值类型

阅读更多

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RefMethod {
private String uid;
private String uname;

public RefMethod() {
super();
}

public RefMethod(String uid, String uname) {
super();
this.uid = uid;
this.uname = uname;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getUname() {
return uname;
}

public void setUname(String uname) {
this.uname = uname;
}

public static Object reflect(Object obj, String methodName, Class[] argumentsType, Object[] argumentsValue){
try {
Class myClass = obj.getClass();
Method method = myClass.getMethod(methodName, argumentsType);
Object resultValue = method.invoke(obj, argumentsValue);
return resultValue;
} 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();
}
return null;
}
public  void testOne(){
System.out.println("testOne方法:无参数无返回值");
}
public  Double testFour(){
System.out.print("testFour方法:无参数有返回值:");
return 33.00;
}
public void testTwo(Integer x){ //这是包装类型
System.out.println("testTwo方法:有参数无返回值:"+"\t"+x);
}
public String testThree(String x){ //这是包装类型
return "testThree方法:有参数有返回值:"+"\t"+x;
}
public String testFive(String x,Integer y, Date d, Double e, Float f, Timestamp ts){ //这是包装类型
return "testFive方法:多个参数有返回值:"+"\t"+x+"\t"+y+"\t"+d+"\t"+e+"\t"+f+"\t"+ts;
}
public void testSix(int x){
System.out.println("testSix方法:一个基本数据类型参数无返回值:"+"\t"+x);
}
public RefMethod testSeven(RefMethod obj){
System.out.print("testSeven方法:一个自定义数据类型参数有返回值:"+"\t"+obj.getUid()+"\t"+obj.getUname()+"\t");
return obj;
}

public void testEight(List<RefMethod> listObj){
for (RefMethod obj : listObj) {
System.out.println("testEight方法:一个List 范型参数无返回值:"+"\t"+obj.getUid()+"\t"+obj.getUname()+"\t");
}
}
public void testNine(List listObj){
for (Object obj : listObj) {
RefMethod rf = (RefMethod) obj;
System.out.println("testNine方法:一个List数据类型参数无返回值:"+"\t"+rf.getUid()+"\t"+rf.getUname()+"\t");
}
}
public void testTen(Map map){
Set keys = map.keySet();
for (Object k : keys) {
String myKey = (String) k;
System.out.println("testTen方法:一个Map数据类型参数无返回值:"+"\t"+map.get(myKey));
}
}
public static void main(String[] args) {

Object resultValue = null;
List li = new ArrayList();
li.add(new RefMethod("001","JAVA"));
li.add(new RefMethod("002","J2EE"));
li.add(new RefMethod("003","JSP"));
Map rmMap = new HashMap();
rmMap.put("上海", "属于中国");
rmMap.put("号百", "属于电信");
rmMap.put("彩铃", "属于中国电信");
// RefMethod.reflect(new RefMethod(),"testOne",new Class[]{},new Object[]{});
// RefMethod.reflect(new RefMethod(),"testTwo",new Class[]{new Integer(0).getClass()},new Object[]{new Integer(56)});
// resultValue = RefMethod.reflect(new RefMethod(),"testThree",new Class[]{new String().getClass()},new Object[]{new String("计算机软件")});
// resultValue = RefMethod.reflect(new RefMethod(),"testFour",new Class[]{},new Object[]{});

/* resultValue = RefMethod.reflect(new RefMethod(),
"testFive",
new Class[]{
new String().getClass(),
new Integer(0).getClass(),
new Date().getClass(),
new Double(0).getClass(),
new Float(0).getClass(),
new Timestamp(0).getClass()},
new Object[]{
new String("String类型"),
new Integer(100000),
new Date(),
new Double(20.00),
new Float(50.0),
new Timestamp(System.currentTimeMillis())});*/

// resultValue = RefMethod.reflect(new RefMethod(),"testSix",new Class[]{int.class},new Object[]{44});

/*RefMethod obj = new RefMethod();
obj.setUid("10010");
obj.setUname("上海电信");
RefMethod resultObj = (RefMethod) RefMethod.reflect(new RefMethod(),"testSeven",new Class[]{RefMethod.class},new Object[]{obj});
System.out.println("返回结果:"+resultObj.getUid()+"\t"+resultObj.getUname());*/

// RefMethod.reflect(new RefMethod(),"testEight",new Class[]{List.class},new Object[]{li});
// RefMethod.reflect(new RefMethod(),"testNine",new Class[]{List.class},new Object[]{li});
RefMethod.reflect(new RefMethod(),"testTen",new Class[]{Map.class},new Object[]{rmMap});



System.out.println("返回结果:"+resultValue);


}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics