<span style="font-size:18px;">反射
一种java语言的自省技术(自我认识的技术)
Class 实力的创建
a. Class c=Class.forName();
b. Class c=对象名.getClass();</span>
<span style="color:#ff0000;"><span style="white-space:pre"> </span><span style="font-size:24px;">这里是一段关于反射的代码 内容主要是 用反射的方法 得到一个学生类的基本属性 但实际上 这段代码可以用来获得各种类的基本属性,和方法 虽然代码比较长但是灵活性比较大 eg:s。getName()虽然短,方便 但是比较固定。</span></span>
</pre><pre name="code" class="java">
<span style="font-size:18px;">import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Reflect {
/**
* @param args
* @return
* @return
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
Class c=Class.forName("com.reflect.Student");
System.out.println(c);
Object obj=c.newInstance();<span style="color:#33cc00;">//利用反射 调用这个类的无参数的构造方法,创建一个对象</span>
System.out.println("新创建出来的对象"+obj);
<span style="color:#33cc00;">//取出属性名</span>
List<String> propertyNames=getFieldNames(c);
if(propertyNames==null|| propertyNames.size()<=0){
return;
}
for(String propertyName:propertyNames){
<span style="color:#009900;">//在属性名前面加set</span>
String setMethodName=getMethodName("set",propertyName);<span style="background-color: rgb(51, 204, 0);">//找名字</span>
Method toInvokeMethod=getInvokeMethod(setMethodName,c);<span style="color:#33ff33;">//在c中找到这个方法 Method对象</span>
if(toInvokeMethod.getParameterTypes()[0]<span style="color:#33cc00;">/*返回参数类型【0】的意思是Class【】中有多个参数【0】是第一个*/</span>.getName().equals("java.lang.Integer")||toInvokeMethod.getParameterTypes()[0].getName().equals("int")){</span>
<span style="font-size:18px;">
toInvokeMethod.invoke(obj, 66);<span style="color:#33cc00;">//包装类型 和 基本类型都要考虑到</span></span>
<span style="font-size:18px;">
}else if(toInvokeMethod.getParameterTypes()[0].getName().equals("java.lang.Double")||toInvokeMethod.getParameterTypes()[0].getName().equals("double")){
toInvokeMethod.invoke(obj, 66.0);
}else{
<span style="color:#33cc00;">//激活他</span>
toInvokeMethod.invoke(obj, "张瑞宁");<span style="color:#33cc00;">//相当与 s.setName()</span>
}
<span style="background-color: rgb(51, 204, 0);">//取get方法</span>
String getMethodName=getMethodName("get",propertyName);//
toInvokeMethod=getInvokeMethod(getMethodName,c);
System.out.println(toInvokeMethod.invoke(obj));
}
<span style="color:#33cc00;">//在属性名子前面加上set=》setname</span>
<span style="color:#33cc00;">//取出c中</span>
System.out.println("*****************************************");
//StringBuffer sb=new StringBuffer("get");
com.reflect.Student s=new com.reflect.Student();
s.setName("zrn");
System.out.println(s.getName());
}
<span style="color:#33cc00;">//取出所有的属性名</span>
private static List<String> getFieldNames(Class c) {
List<String> propertyNames=new ArrayList<String>();
Field[] fs=c.getDeclaredFields();
for(Field con:fs){
propertyNames.add(con.getName());<span style="color:#33cc00;">//取出每一个属性名存到 list里面</span>
}
return propertyNames;
}
<span style="color:#33cc00;">//拼接方法名 prefix:方法名的前缀 set/get propertyName :属性名</span>
<span style="color:#33cc00;">//返回值 setName/getName</span>
private static String getMethodName(String prefix,String propertyName) {
/*注释的方法不太灵活 比较固定 */
//List<String> getMethodNames=new ArrayList<String>();
//StringBuffer sb=new StringBuffer("get");
//sb.append(propertyName.substring(0,1).toUpperCase()+propertyName.substring(1));
//String methodName=sb.toString();
String suffix=propertyName.substring(0,1).toUpperCase()+propertyName.substring(1);
return prefix+suffix;
}
<span style="color:#33cc00;">//根据方法在Class对象中查找到 方法对象Method</span>
private static Method getInvokeMethod(String methodName,Class c){
Method[] ms=c.getMethods();
if(ms!=null&&ms.length>0){
for(Method m:ms){
if(m.getName().equals(methodName)){
return m;
}
}
}
return null;
}
}
</span>
<span style="font-size:18px;">下面是反射的另一个案例,这个案例是把一个类中的属性 构造方法 返回值类型等大致还原,所以类名我取名黑客,这个案例与上面的案例对比来看 效果更佳(用拼音来做类名是我的都比习惯 用英文的时候我老是把名字取得和sun写的java中的名字重复 报错!)</span>
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Heike{
public static void main(String[] args)throws Exception{
Class c=Class.forName("com.pingan.bean.Ensurance");
Field [] fs=c.getDeclaredFields();
// 获取类的属性;
for(Field con: fs){
System.out.println(getModifierString(con.getModifiers())+"\t"+con.getName()+";");
}
//获取类的构造函数;
Constructor[] cs=c.getConstructors();
for(Constructor con:cs){
System.out.print(getModifierString(con.getModifiers())+"\t"+con.getName()+"(");
Class[] ptcs=con.getParameterTypes();
if(ptcs!=null&&ptcs.length>0){
for(Class ptc: ptcs){
System.out.print(ptc.getName()+"$");
}
}
System.out.print(") {}");
System.out.println();
}
//获取类的方法;
Method [] ms=c.getDeclaredMethods();
for(Method con: ms){
Class c5=con.getReturnType();
System.out.print(getModifierString(con.getModifiers())+"\t"+c5+"\t"+con.getName()+"\t"+"(");
Class[] ptcs=con.getParameterTypes();
if(ptcs!=null&&ptcs.length>0){
for(Class ptc: ptcs){
System.out.print(ptc.getName()+" $");
}
}
System.out.print(") {}");
System.out.println();
}
}
public static String getModifierString(int value){
String mstr="";
switch(value){
case 1: mstr="public";break;
case 2: mstr="private";break;
}
return mstr;
}
}