JAVA Reflection??????????
???????????? ???????[ 2014/7/4 13:44:17 ] ??????????????? java
??????????????
import java.lang.reflect.*;
public class ReflectTester {
public Object copy(Object object) throws Exception{
//???????????
Class classType=object.getClass();
System.out.println("Class:"+classType.getName());
//???????????????????μ????
Object objectCopy=classType.getConstructor(new Class[]{}).
newInstance(new Object[]{});
//???????????????
Field fields[]=classType.getDeclaredFields();
for(int i=0; i<fields.length;i++){
Field field=fields[i];
String fieldName=field.getName();
String firstLetter=fieldName.substring(0??1).toUpperCase();
//??ú????????? getXXX()??????????
String getMethodName="get"+firstLetter+fieldName.substring(1);
//??ú????????? setXXX()??????????
String setMethodName="set"+firstLetter+fieldName.substring(1);
//??ú????????? getXXX()????
Method getMethod=classType.getMethod(getMethodName??new Class[]{});
//??ú????????? setXXX()????
Method setMethod=classType.getMethod(setMethodName??new Class[]{field.getType()});
//?????????? getXXX()????
Object value=getMethod.invoke(object??new Object[]{});
System.out.println(fieldName+":"+value);
//??????????? setXXX()????
setMethod.invoke(objectCopy??new Object[]{value});
}
return objectCopy;
}
public static void main(String[] args) throws Exception{
Customer customer=new Customer("Tom"??21);
customer.setId(new Long(1));
Customer customerCopy=(Customer)new ReflectTester().copy(customer);
System.out.println("Copy information:"+customerCopy.getName()+
“ "+ customerCopy.getAge());
}
}
|
???????÷????????÷???
class Customer{
//Customer ??????? JavaBean
private Long id;
private String name;
private int age;
public Customer(){}
public Customer(String name??int age){
this.name=name;
this.age=age;
}
public Long getId(){return id;}
public void setId(Long id){this.id=id;}
public String getName(){return name;}
public void setName(String name){this.name=name;}
public int getAge(){return age;}
public void setAge(int age){this.age=age;}
}
|
import java.lang.reflect.*;
public class InvokeTester {
public int add(int param1??int param2){ return param1+param2; }
public String echo(String msg){ return "echo:"+msg; }
public static void main(String[] args) throws Exception{
Class classType=InvokeTester.class;
Object invokeTester=classType.newInstance();
//???? InvokeTester ????? add()????
Method addMethod=classType.getMethod("add"??new Class[]{int.class??int.class});
Object result=addMethod.invoke(invokeTester??
new Object[]{new Integer(100)??new Integer(200)});
System.out.println((Integer)result);
//???? InvokeTester ????? echo()????
Method echoMethod=classType.getMethod("echo"??new Class[]{String.class});
result=echoMethod.invoke(invokeTester??new Object[]{"Hello"});
System.out.println((String)result);
}
}
|
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????