java">com.Main
Main.java
Screen.java
com.UnitTest
ScreenTest.java
Main.java
在Main.java中直接使用反射的方式,调用Screen.java的接口,在ScreenTest中采用单体测试反射调用Screen
java">package com.Main;
import java.lang.reflect.*;
import com.Main.*;
public class Main {
public static void main(String[] str) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, ClassNotFoundException, NoSuchMethodException, SecurityException {
//1
Screen screen1 =new Screen();
Class<?> clazz = screen1.getClass();
//2
Class<?> clazz1 = Screen.class;
//3
Class<?> clazz2 = Class.forName("com.Main.Screen");
Screen s = (Screen)clazz2.newInstance();
s.fun();
//调用无参数
try {
System.out.println("-----------------调用无参");
Class<?> cls = Screen.class;
Method method = cls.getDeclaredMethod("fun", null);
method.setAccessible(true);
method.invoke(cls.newInstance());
method.setAccessible(false);
}catch(Exception e) {
System.out.println(e.getStackTrace());
}
//调用有参
try {
System.out.println("-----------------调用有参");
Class<?> cls = Screen.class;
Class<?> []args = new Class[2];
args[0]=String.class;
args[1]=int.class;
Method method = cls.getDeclaredMethod("fun1",args);//Method method = cls.getDeclaredMethod("fun1",String.class,int.class);
method.setAccessible(true);
method.invoke(cls.newInstance(),"123",123);
method.setAccessible(false);
}catch(Exception e) {
System.out.println(e.getStackTrace());
}
//调用内部类
try {
System.out.println("-----------------调用内部类");
Class<?> cls = Screen.class;
Screen screen = (Screen)cls.newInstance();
Class<?> cls1 = Class.forName("com.Main.Screen$Bluetooth");
Constructor<?>[] constructor=cls1.getDeclaredConstructors();
for(Constructor<?> con:constructor) {
System.out.println(con);
}
constructor[0].setAccessible(true);
Object obj = constructor[0].newInstance(screen);
Method method = cls1.getDeclaredMethod("onChange",String.class,int.class);
method.setAccessible(true);
method.invoke(obj,"String",123);
method.setAccessible(false);
constructor[0].setAccessible(false);
}catch(Exception e) {
System.out.print(e.getStackTrace());
}
}
}
Screen.java
java">package com.Main;
public class Screen {
private static Screen screen;
public Screen() {
}
public Screen(int _a) {
}
//单例模式
public static Screen getInstance() {
if(screen==null) {
screen = new Screen();
}
return screen;
}
public void fun() {
System.out.println("fun() ");
}
public void fun1(String _string,int _num) {
if("hello".equals(_string) && _num == 5) {
System.out.println("fun1() "+_string+_num);
}else {
} System.out.println("fun1() 没有走到的分支");
}
public class Bluetooth {
public Bluetooth() {
System.out.println(" Bluetooth()");
}
public Bluetooth(String _string) {
System.out.println("Bluetooth(String _string)");
}
public void onChange(String string,int i) {
System.out.println("Bluetooth类 onChange"+string+" "+i);
}
}
}
ScreenTest.java
java">package com.UnitTest;
import com.Main.*;
import com.Main.Screen.Bluetooth;
import org.junit.*;
import java.lang.reflect.*;
public class ScreenTest {
private static Screen screen;
public Method getFatherMethod(Class<?> clazz,String methodName,final Class<?>[] classes) throws Exception{
Method method=null;
try {
method=clazz.getDeclaredMethod(methodName, classes);
}catch(NoSuchMethodException e) {
if(clazz.getSuperclass()==null) {
return method;
}else {
method = getFatherMethod(clazz.getSuperclass(),methodName,classes);
}
System.out.println("NoSuchMethodException"+e.getStackTrace());
}
return method;
}
public Field getFatherField(Class<?> clazz,String methodName) throws Exception{
Field field = null;
try {
field = clazz.getDeclaredField(methodName);
}catch(NoSuchFieldException e) {
if(clazz.getSuperclass()==null) {
return field;
}else {
field = getFatherField(clazz.getSuperclass(),methodName);
}
System.out.println("NoSuchFieldException"+e.getStackTrace());
}
return field;
}
@BeforeClass
public static void BefoClass() {
screen = Screen.getInstance();
}
@AfterClass
public static void AfterClass() {
System.gc();
}
@Test
public void funTest() throws Exception {
System.out.println("------------------1----begin---------------------");
try {
Method method = getFatherMethod(Screen.class,"fun", null);
if(method!=null) {
method.setAccessible(true);
Object invoke = method.invoke(screen);
if(invoke!=null) {
System.out.println("invoke sucess");
}
method.setAccessible(false);
}
}catch(Exception e) {
System.out.println("funTest"+e.getStackTrace());
}
System.out.println("------------------1----end---------------------");
}
@Test
public void fun1Test() throws Exception{
System.out.println("------------------2----begin---------------------");
try {
Class<?>[] args1 = new Class[2];
args1[0]=String.class;
args1[1]=int.class;
Method method = getFatherMethod(Screen.class,"fun1", args1);
method.setAccessible(true);
//method.invoke(screen, null,0);
method.invoke(screen, "hello",5);
method.setAccessible(false);
}catch(Exception e) {
System.out.println("fun1Test "+e.getStackTrace());
}
System.out.println("------------------2----end---------------------");
}
@Test
public void BluetoothTest() {
System.out.println("------------------3----begin---------------------");
try {
Class<?> cls = Class.forName("com.Main.Screen$Bluetooth");
if(cls != null) {
System.out.println("------------------cls != null---------------------");
Class<?>[] args= new Class[2];
args[0]=String.class;
args[1]=int.class;
Method method = cls.getDeclaredMethod("onChange", args);
method.setAccessible(true);
Constructor<?>[] constructor = cls.getDeclaredConstructors();
for(Constructor<?> con:constructor) {
System.out.println("Constructor<?> "+con);
}
constructor[0].setAccessible(true);
Object obj = constructor[0].newInstance(screen);//传一个外部类的对象进去
method.invoke(obj, "123",123);
constructor[0].setAccessible(false);
method.setAccessible(false);
}
}catch(Exception e) {
System.out.println("BluetoothTest "+e.getStackTrace());
}
System.out.println("------------------3----end---------------------");
}
}