package amery.jdk.reflect;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectClass {

    private final static String TAG = "peter.log.ReflectClass";
    
    // 创建对象
    @Test
    public void reflectNewInstance() {
        try {
            Class<?> classBook = Class.forName("amery.jdk.reflect.Book");
            Object objectBook = classBook.newInstance();
            Book book = (Book) objectBook;
            book.setName("Android进阶之光");
            book.setAuthor("刘望舒");
            System.out.println("reflectNewInstance book = " + book.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 反射私有的构造方法
    @Test
    public  void reflectPrivateConstructor() {
        try {
            Class<?> classBook = Class.forName("amery.jdk.reflect.Book");
            Constructor<?> declaredConstructorBook = classBook.getDeclaredConstructor(String.class,String.class);
            declaredConstructorBook.setAccessible(true);
            Object objectBook = declaredConstructorBook.newInstance("Android开发艺术探索","任玉刚");
            Book book = (Book) objectBook;
            System.out.println("reflectPrivateConstructor book = " + book.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 反射私有属性
    @Test
    public void reflectPrivateField() {
        try {
            Class<?> classBook = Class.forName("amery.jdk.reflect.Book");
            Object objectBook = classBook.newInstance();
            Field fieldTag = classBook.getDeclaredField("TAG");
            fieldTag.setAccessible(true);
            String tag = (String) fieldTag.get(objectBook);
            System.out.println("reflectPrivateField tag = " + tag);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 反射私有方法
    @Test
    public void reflectPrivateMethod() {
        try {
            Class<?> classBook = Class.forName("amery.jdk.reflect.Book");
            Method methodBook = classBook.getDeclaredMethod("declaredMethod",int.class);
            methodBook.setAccessible(true);
            Object objectBook = classBook.newInstance();
            String string = (String) methodBook.invoke(objectBook,0);

            System.out.println("reflectPrivateMethod string = " + string);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Test
    public void getClassWays() {
        System.out.println(this.getClass().getCanonicalName());
        System.out.println(Book.class.getFields());
    }
    
}

@@ -242,20 +242,16 @@
 Class%22;%0A
-    
 %0A    // 
@@ -770,17 +770,16 @@
  public 
- 
 void ref
@@ -992,16 +992,17 @@
 g.class,
+ 
 String.c
@@ -1149,16 +1149,17 @@
 %E5%BC%80%E5%8F%91%E8%89%BA%E6%9C%AF%E6%8E%A2%E7%B4%A2%22,
+ 
 %22%E4%BB%BB%E7%8E%89%E5%88%9A%22);%0A
@@ -2129,16 +2129,17 @@
 Method%22,
+ 
 int.clas
@@ -2309,16 +2309,103 @@
 ectBook,
+ 0);%0A%0A//            Book book = (Book) (objectBook);%0A//            book.declaredMethod(
 0);%0A%0A   
@@ -2725,15 +2725,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

add reflect