import java.lang.reflect.*;
class Stu{
private String name;
private int age;
public boolean flag =true ;
public Stu(){}
public Stu(String name,int age){
this .setName(name);
this .setAge(age);
}
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;
}
public void study(){
System.out .println("i can study" );
}
private void speak(String languge,int x){
System.out .println("I can speak :" +languge+" " +x);
}
public String toString(){
return this .name+" : " +this .age;
}
}
public class ReflectThree {
public static void classForInstance() throws Exception{
@SuppressWarnings ("unchecked" )
Class<Stu> stuClass = (Class<Stu>)Class.forName("GeneIterAdaptReflect.com.Stu" );
Stu stu1 = stuClass.newInstance();
stu1.setName("丽丽" );
System.out .println(stu1.getName());
@SuppressWarnings ("unchecked" )
Constructor<Stu>[] constructor = (Constructor<Stu>[])
stuClass.getConstructors();
Stu stu2 = constructor[1 ].newInstance("qian" ,36 );
System.out .println(stu2.toString());
}
public static void methodInfo() throws Exception{
@SuppressWarnings ("unchecked" )
Class<Stu> stuClass = (Class<Stu>)
Class.forName("GeneIterAdaptReflect.com.Stu" );
Method[] methods = stuClass.getDeclaredMethods();
for (Method m:methods){
StringBuilder s = new StringBuilder();
int x = m.getModifiers();
String modifyString = Modifier.toString(x);
Class<?> returnType = m.getReturnType();
Class<?>[] paraClass = m.getParameterTypes();
if (paraClass.length==0 ){
s.append("" );
}
else {
for (int i=0 ;i<paraClass.length;i++){
s.append(paraClass[i].getSimpleName()+" " );
}
}
System.out .println(modifyString+"
" +returnType.getSimpleName()+" " +m.getName()
+"(" +s+")" );
}
}
public static void fieldInfo() throws Exception{
Class<Stu> stuClass = (Class<Stu>)
Class.forName("GeneIterAdaptReflect.com.Stu" );
Field[] fields = stuClass.getDeclaredFields();
for (int i=0 ;i<fields.length;i++){
System.out .println(Modifier.toString(fields[i].
getModifiers())+" " +fields[i].getName());
}
}
public static void callSpeakMethod()throws Exception{
Class<Stu> stuClass = (Class<Stu>) Class.forName
("GeneIterAdaptReflect.com.Stu" );
Constructor<Stu>[] constructor=
(Constructor<Stu>[]) stuClass.getConstructors();
Stu stu3 = constructor[1 ].newInstance("qian" ,26 );
Class<?>[] paraClass =new Class[]
{String.class ,int .class };
Method privateMethod =
stuClass.getDeclaredMethod("speak" ,paraClass);
privateMethod.setAccessible(true );
privateMethod.invoke(stu3, "English French" ,2 );
}
public static void setter(Object obj,String fieldName,Class<?> paraType,Object value) throws Exception{
名 比如 name;要转换成标准形式;
String firstChar =
fieldName.substring(0 , 1 ).toUpperCase();
String methodName = firstChar+fieldName.substring(1 );
obj.getClass().getDeclaredMethod
("set" +methodName, paraType).invoke(obj, value);
}
public static Object getter(Object obj,String fieldName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
String firstChar =
fieldName.substring(0 , 1 ).toUpperCase();
String methodName = firstChar+fieldName.substring(1 );
return obj.getClass().getDeclaredMethod("get" +methodName).invoke(obj);
}
public static void main(String[] args) throws Exception {
Class<Stu> stuClass = (Class<Stu>) Class.forName("GeneIterAdaptReflect.com.Stu" );
Stu stuObj = stuClass.newInstance();
setter(stuObj,"name" ,String.class ,"zhangsan" );
String getString =(String)getter(stuObj,"name" );
System.out .println(getString);
}
}