一、什么叫反射?
public class Demo1 {
public static void main(String[] args) throws Exception{
Class c1 = Class.forName("com.dwr.reflection.User");
System.out.println(c1);
Class c2 = Class.forName("com.dwr.reflection.User");
System.out.println(c1.hashCode());
System.out.println(c2.hashCode());
}
}
class User{
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
二、测试class类的创建方式有哪些
public class Demo2 {
public static void main(String[] args) throws Exception{
Person person = new Student();
System.out.println(person.name);
Class c1 = person.getClass();
System.out.println(c1);
Class c2 = Class.forName("com.dwr.reflection.Student");
System.out.println(c2);
Class c3 = Student.class;
System.out.println(c3);
Class c4 = Integer.TYPE;
System.out.println(c4);
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
}
class Person{
public String name;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Student extends Person{
public Student(){
this.name = "student";
}
}
class Teacher extends Person{
public Teacher(){
this.name = "Teacher";
}
}
三、类加载内存分析 加载->链接->初始化
public class Demo5 {
public static void main(String[] args) {
A a = new A();
System.out.println(A.m);
}
}
class A{
static {
System.out.println("A类的静态代码块初始化");
m = 100;
}
static int m = 1;
public A() {
System.out.println("A类的无参构造器初始化");
}
}
四、分析类的初始化
public class Demo6 {
static {
System.out.println("MAIN类被加载");
}
public static void main(String[] args) throws ClassNotFoundException {
}
}
class Father{
static int fatherkey = 1;
static {
System.out.println("Father类被加载");
}
}
class Son extends Father{
static {
System.out.println("Son类被加载");
childkey = 1000;
}
static int childkey = 2;
static final int M = 666;
}
五、获得类的信息
public class Demo7 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class.forName("com.dwr.reflection.User");
System.out.println(c1.getName());
System.out.println(c1.getSimpleName());
Field[] fields = c1.getFields();
for (Field field : fields) {
System.out.println(field);
}
fields = c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
System.out.println(c1.getDeclaredField("name"));
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println("正常的" + method);
}
methods = c1.getDeclaredMethods();
for (Method method : methods) {
System.out.println("getDeclaredMethods" + method);
}
Method getName = c1.getMethod("getName", null);
System.out.println(getName);
System.out.println(c1.getConstructors());
System.out.println(c1.getDeclaredConstructors());
System.out.println(c1.getDeclaredConstructor(String.class, int.class));
}
}
六、动态的创建对象,通过反射
public class Demo8 {
public static void main(String[] args) throws Exception {
Class c1 = Class.forName("com.dwr.reflection.User");
User user =(User) c1.newInstance();
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class);
User user2 = (User)constructor.newInstance("新名字", 23);
User user3 = (User) c1.newInstance();
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(user3,"dwr");
System.out.println(user3.getName());
User user4 = (User) c1.newInstance();
Field name = c1.getDeclaredField("name");
name.setAccessible(true);
name.set(user4, "dwr2");
System.out.println(user4.getName());
}
}
七、性能对比分析
public class Demo9 {
public static void test1(){
User user = new User();
long startTime = System.currentTimeMillis();
for(int i = 0;i<100000000;i++){
user.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通方式执行1亿次" + (endTime - startTime) + "ms");
}
public static void test2() {
User user = new User();
Class c1 = user.getClass();
try {
Method getName = c1.getDeclaredMethod("getName", null);
long startTime = System.currentTimeMillis();
for(int i = 0;i<100000000;i++){
getName.invoke(user, null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方式执行1亿次" + (endTime - startTime) + "ms");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void test3() {
User user = new User();
Class c1 = user.getClass();
try {
Method getName = c1.getDeclaredMethod("getName", null);
getName.setAccessible(true);
long startTime = System.currentTimeMillis();
for(int i = 0;i<100000000;i++){
getName.invoke(user, null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方式并关闭检测执行1亿次" + (endTime - startTime) + "ms");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test1();
test2();
test3();
}
}
八、练习ORM 反射操作注解
public class Demo10 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.dwr.reflection.Student2");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
Table table = (Table)c1.getAnnotation(Table.class);
String value = table.value();
System.out.println(value);
Field name = c1.getDeclaredField("id");
FieldTable annotation = name.getAnnotation(FieldTable.class);
System.out.println(annotation);
}
}
@Table("db_student")
class Student2{
@FieldTable(columnName = "db_id",type = "int", length = 10)
private int id;
@FieldTable(columnName = "db_age",type = "int", length = 10)
private int age;
@FieldTable(columnName = "db_name",type = "varchar", length = 3)
private String name;
Student2() {
}
Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldTable{
String columnName();
String type();
int length();
}