1. 程式人生 > >反射 method的呼叫 無參和多引數

反射 method的呼叫 無參和多引數

package test.ClassLoader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestClassLoader {

	
	public static void main(String a[]) {
		//測試一個引數的寫法
		getNowClass2();
		//測試一個引數的另一個寫法
		getNowClass1();
		//測試無參寫法
		getNowClass();
	}
	
	public void printDemo(int m) {
		System.out.println("invoke print");
	}
	public void printDemo() {
		System.out.println("invoke print no paramater");
		//System.out.printf("congratulations %s1 !", "hello");
	}
	
	@SuppressWarnings("unchecked")
	static void getNowClass2() {
		ClassLoader cls = Thread.currentThread().getContextClassLoader();
		// ClassLoader cls = this.getClass().getClassLoader();
		try {
			Class<TestClassLoader> t = (Class<TestClassLoader>) cls.loadClass("test.ClassLoader.TestClassLoader");
			// t.newInstance().printDemo();
			Method[] declaredMethods = t.getDeclaredMethods();
			System.out.println(declaredMethods);
			// 獲得某個函式
			Method method = t.getMethod("printDemo",int.class);
			method.setAccessible(true);
			method.invoke(t.newInstance(),100);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	static void getNowClass1() {
		ClassLoader cls = Thread.currentThread().getContextClassLoader();
		// ClassLoader cls = this.getClass().getClassLoader();
		try {
			Class<TestClassLoader> t = (Class<TestClassLoader>) cls.loadClass("test.ClassLoader.TestClassLoader");
			// t.newInstance().printDemo();
			Method[] declaredMethods = t.getDeclaredMethods();
			System.out.println(declaredMethods);
			// 獲得某個函式
			Method method = t.getMethod("printDemo", new Class[] { int.class });
			method.setAccessible(true);
			method.invoke(t.newInstance(), new Object[] { new Integer(100) });
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	static void getNowClass() {
		ClassLoader cls = Thread.currentThread().getContextClassLoader();
		// ClassLoader cls = this.getClass().getClassLoader();
		try {
			Class<TestClassLoader> t = (Class<TestClassLoader>) cls.loadClass("test.ClassLoader.TestClassLoader");
			// t.newInstance().printDemo();
			Method[] declaredMethods = t.getDeclaredMethods();
			System.out.println(declaredMethods);
			// 獲得某個函式
			Method method = t.getMethod("printDemo");
			method.setAccessible(true);
			method.invoke(t.newInstance());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}




}

這兩個函式要public  不然會報錯

public void printDemo(int m) {
System.out.println("invoke print");
}
public void printDemo() {
System.out.println("invoke print no paramater");
//System.out.printf("congratulations %s1 !", "hello");
}