一、什么是反射?
反射,一种计算机处理方式。是程序可以访问、检测和修改它本身状态或行为的一种能力。
二、反射的作用
①反编译:把.class文件翻译成.java文件
②通过反射机制访问java对象的属性,方法,构造方法等。
三、方法介绍
代码实现:
实体类:
package com.sun.entity;
import javax.servlet.http.HttpServlet;
public class Person extends HttpServlet implements Runnable{
private Integer pid;
public String pname;
private String psex;
public Person() {
super();
}
public Person(Integer pid, String pname, String psex) {
super();
this.pid = pid;
this.pname = pname;
this.psex = psex;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPsex() {
return psex;
}
public void setPsex(String psex) {
this.psex = psex;
}
public void run() {
}
}
调用方法翻译
package com.sun.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.junit.Test;
import com.sun.diy.ClassLoaderDIY;
public class HomeWork {
@Test
public void testA() throws SecurityException, NoSuchFieldException{
Class clazz=null;
//使用自己的类加载器(也可使用自带的)
try {
clazz=Class.forName("com.sun.entity.Person",true,new ClassLoaderDIY());
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer content=new StringBuffer();
//=Person.class;
//类的全限定名
System.out.println(clazz.getName());
//获取继承的类,和实现的接口
String superClass=clazz.getSuperclass().getName();
String superClassName=clazz.getSuperclass().getSimpleName();
System.out.println(superClassName+":"+superClass);
//获取实现的接口
Class classInteface[]=clazz.getInterfaces();
String Strinterface="";
for (Class c : classInteface) {
Strinterface=c.getSimpleName();
}
//获取属性(所有属性)
String fields="";
Field field[]=clazz.getDeclaredFields();
for (Field field2 : field) {
System.out.println("修饰符:"+field2.getModifiers()+"数据类型"+clazz.getTypeParameters()+"属性名:"+field2.getName());
}
//获取单个属性的修饰符
//获取单个属性的数据类型
Field id=clazz.getDeclaredField("pid");
System.out.println("id:"+id.getType().getSimpleName());
Field name=clazz.getField("pname");
System.out.println("name:"+name.getType().getSimpleName());
Field sex=clazz.getDeclaredField("psex");
System.out.println("sex:"+sex.getType().getSimpleName());
//打印内容
content.append("---------------\n");
content.append("package "+clazz.getName()+";\n");
content.append("import "+superClass+";\n");
content.append("public class "+clazz.getSimpleName()+" extends "+superClassName+" implements "+Strinterface+"{\n");
content.append(Modifier.toString(field[0].getModifiers())+" "+id.getType().getSimpleName()+" "+field[0].getName()+";\n");
content.append(Modifier.toString(field[1].getModifiers())+" "+name.getType().getSimpleName()+" "+field[1].getName()+";\n");
content.append(Modifier.toString(field[2].getModifiers())+" "+sex.getType().getSimpleName()+" "+field[2].getName()+";\n");
content.append("public "+clazz.getSimpleName()+"(");
//获取该类的构造方法
Method[] constructors=clazz.getDeclaredMethods();
for (Method method : constructors) {
System.out.println("method "+Modifier.toString(method.getModifiers())+" "+method.getReturnType().getSimpleName()+" "+method.getName()+"()");
Class[] clazzType=method.getParameterTypes();
for (Class cc : clazzType) {
System.out.println(" "+cc.getSimpleName());
content.append(cc.getSimpleName()+" a ");
}
}
/* for (Constructor con : constructors) {
System.out.println("con:"+con.getDeclaredAnnotations());
}*/
content.append("){\n");
content.append("this."+field[0].getName()+"="+field[0].getName()+";\n");
content.append("this."+field[1].getName()+"="+field[1].getName()+";\n");
content.append("this."+field[2].getName()+"="+field[2].getName()+";\n}\n");
content.append("public "+clazz.getSimpleName()+"(){\n}");
System.out.println(content);
//将content写成.java文件
/* File file=new File(clazz.getSimpleName()+".java");
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write(content.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
*/
}
}
1万+

被折叠的 条评论
为什么被折叠?



