JAVA 反射

package com.test.rmi;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;

public class TestUrlClassLoader {

public static void main(String aadfass[]) {
URLClassLoader loader = null;
try {
URL[] urls = new URL[1];
URLStreamHandler streamHandler = null;
// E:\mp_workspace\HttpServer2.0\webRoot 是工程下一目录,用来存放测试的class文件
String respository = new URL("file", "127.0.0.1",
"E:\\workspace2\\JavaNet\\bin\\com\\test\\rmi\\Myclass.class")
.toString();
urls[0] = new URL(null, respository, streamHandler);

loader = new URLClassLoader(urls);

} catch (Exception e) {
e.printStackTrace();
}
Class className = null;
try {
className = loader.loadClass("com.test.rmi.Myclass");
System.out.println(className.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 获取构造方法
Constructor<?> cons[] = className.getConstructors();
for (int i = 0; i < cons.length; i++) {
System.out.println("方法:" + cons[i]);
}

Myclass myclass = null;
try {
myclass = (Myclass) cons[1].newInstance("asdf");
myclass.test();
} catch (Exception e) {
e.printStackTrace();
}

for (int i = 0; i < cons.length; i++) {
Class<?> p[] = cons[i].getParameterTypes();
System.out.print("构造方法:  ");
int mo = cons[i].getModifiers();// 构造函数没有public 或者private这一类的修饰符
System.out.print(Modifier.toString(mo) + " ");
System.out.print(cons[i].getName());
System.out.print("(");
for (int j = 0; j < p.length; ++j) {
System.out.print(p[j].getName() + " arg" + i);
if (j < p.length - 1) {
System.out.print(",");
}
}
System.out.println("){}");
}
// 实现的接口
Class<?> intes[] = className.getInterfaces();
for (int i = 0; i < intes.length; i++) {
System.out.println("实现的接口   " + intes[i].getName());
}
// 取得父类
Class<?> temp = className.getSuperclass();
System.out.println("继承的父类为:   " + temp.getName());
// 获取方法
Method method[] = className.getMethods();
for (int i = 0; i < method.length; i++) {
Class<?> p[] = method[i].getParameterTypes();
Class<?> retruntype = method[i].getReturnType();
int mo = method[i].getModifiers();
System.out.print("方法:");
System.out.print(Modifier.toString(mo) + "  --");
System.out.print(method[i].getName() + "(");
for (int a = 0; a < p.length; a++) {
System.out.print(p[a].getName() + " arg" + a);
if (a < p.length - 1) {
System.out.print(",");
}
}
System.out.print("){}");
System.out.print("--ret---" + retruntype);

Class<?> exce[] = method[i].getExceptionTypes();
if (exce.length > 0) {
System.out.print(") throws ");
for (int k = 0; k < exce.length; ++k) {
System.out.print(exce[k].getName() + " ");
if (k < exce.length - 1) {
System.out.print(",");
}
}
} else {
System.out.print(")");
}
System.out.println();
}

try {
Method dd=className.getMethod("test2", int.class,String.class );
dd.invoke(cons[1].newInstance("asdf"),2, "asdfklasdlf**************");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

///////////////////////////////////////////////////////////---Mycalss

package com.test.rmi;

import java.io.IOException;

public class Myclass {
public void test() {
System.out.println("good byte");
}

public Myclass(int a,int b){

}
public Myclass(String ab){

}
public static void main(String[] args) {

System.out.println("hello word");
}


public void test2(int a,String  abbs)throws IOException{
for(int i=0;i<a;i++){
System.out.println(abbs);
}
}
}

你可能感兴趣的:(java 反射)