下面是java反射的两个例子
Reflection,通过一个类名,打印出构造函数,方法和变量
- package senior;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.util.Scanner;
- public class ReflectionTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String name;
- if (args.length > 0)
- name = args[0];
- else {
- System.out.println("Enter class name (e.g. java.util.Date): ");
- Scanner in = new Scanner(System.in);
- name = in.nextLine();
- }
- try {
- Class<?> cl = Class.forName(name);
- Class<?> superClass = cl.getSuperclass();
- String modifiers = Modifier.toString(cl.getModifiers());
- if (modifiers.length() > 0)
- System.out.print(modifiers + " ");
- System.out.print("class " + name);
- if (superClass != null && superClass != Object.class)
- System.out.print("extends " + superClass.getName());
- System.out.print("\n{\n");
- printConstructors(cl);// 打印构造方法
- System.out.println();
- printMethods(cl);// 打印方法
- System.out.println();
- printFields(cl);// 打印数据成员
- System.out.println("}");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.exit(0);
- }
- private static void printFields(Class<?> cl) {
- // TODO Auto-generated method stub
- Field[] fields = cl.getDeclaredFields();
- for (Field f : fields) {
- System.out.print("\t");
- Class<?> type = f.getType();
- String name = f.getName();
- String modifiers = Modifier.toString(f.getModifiers());
- if (modifiers.length() > 0)
- System.out.print(modifiers + " ");
- System.out.println(type.getName() + " " + name + ";");
- }
- }
- private static void printMethods(Class<?> cl) {
- // TODO Auto-generated method stub
- Method[] methods = cl.getDeclaredMethods();
- for (Method m : methods) {
- System.out.print("\t");
- Class<?> retType = m.getReturnType();
- String name = m.getName();
- String modifiers = Modifier.toString(m.getModifiers());
- System.out.print(modifiers + " ");
- System.out.print(retType + " " + name + "(");
- @SuppressWarnings("rawtypes")
- Class[] paramTypes = m.getParameterTypes();
- for (int j = 0; j < paramTypes.length; j++) {
- if (j > 0)
- System.out.print(", ");
- System.out.print(paramTypes[j].getName());
- }
- System.out.println(");");
- }
- }
- private static void printConstructors(Class<?> cl) {
- // TODO Auto-generated method stub
- @SuppressWarnings("rawtypes")
- Constructor[] constructors = cl.getDeclaredConstructors();
- for (Constructor<?> c : constructors) {
- System.out.print("\t");
- String name = c.getName();
- String modifiers = Modifier.toString(c.getModifiers());
- System.out.print(modifiers + " ");
- System.out.print(name + "(");
- @SuppressWarnings("rawtypes")
- Class[] paramTypes = c.getParameterTypes();
- for (int j = 0; j < paramTypes.length; j++) {
- if (j > 0)
- System.out.print(", ");
- System.out.print(paramTypes[j].getName());
- }
- System.out.println(");");
- }
- }
- }
package senior;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
public class ReflectionTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name;
if (args.length > 0)
name = args[0];
else {
System.out.println("Enter class name (e.g. java.util.Date): ");
Scanner in = new Scanner(System.in);
name = in.nextLine();
}
try {
Class<?> cl = Class.forName(name);
Class<?> superClass = cl.getSuperclass();
String modifiers = Modifier.toString(cl.getModifiers());
if (modifiers.length() > 0)
System.out.print(modifiers + " ");
System.out.print("class " + name);
if (superClass != null && superClass != Object.class)
System.out.print("extends " + superClass.getName());
System.out.print("\n{\n");
printConstructors(cl);// 打印构造方法
System.out.println();
printMethods(cl);// 打印方法
System.out.println();
printFields(cl);// 打印数据成员
System.out.println("}");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
private static void printFields(Class<?> cl) {
// TODO Auto-generated method stub
Field[] fields = cl.getDeclaredFields();
for (Field f : fields) {
System.out.print("\t");
Class<?> type = f.getType();
String name = f.getName();
String modifiers = Modifier.toString(f.getModifiers());
if (modifiers.length() > 0)
System.out.print(modifiers + " ");
System.out.println(type.getName() + " " + name + ";");
}
}
private static void printMethods(Class<?> cl) {
// TODO Auto-generated method stub
Method[] methods = cl.getDeclaredMethods();
for (Method m : methods) {
System.out.print("\t");
Class<?> retType = m.getReturnType();
String name = m.getName();
String modifiers = Modifier.toString(m.getModifiers());
System.out.print(modifiers + " ");
System.out.print(retType + " " + name + "(");
@SuppressWarnings("rawtypes")
Class[] paramTypes = m.getParameterTypes();
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
private static void printConstructors(Class<?> cl) {
// TODO Auto-generated method stub
@SuppressWarnings("rawtypes")
Constructor[] constructors = cl.getDeclaredConstructors();
for (Constructor<?> c : constructors) {
System.out.print("\t");
String name = c.getName();
String modifiers = Modifier.toString(c.getModifiers());
System.out.print(modifiers + " ");
System.out.print(name + "(");
@SuppressWarnings("rawtypes")
Class[] paramTypes = c.getParameterTypes();
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
}
运行结果:
MethodPointer相当于C/C++中的回调函数
- package senior;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- public class MethodPointerTest {
- /**
- * @param args
- * @throws NoSuchMethodException
- * @throws SecurityException
- */
- public static void main(String[] args) throws SecurityException,
- NoSuchMethodException {
- // TODO Auto-generated method stub
- Method square = MethodPointerTest.class.getMethod("square",
- double.class);
- Method sqrt = Math.class.getMethod("sqrt", double.class);
- printTable(1, 10, 10, square);
- printTable(1, 10, 10, sqrt);
- }
- public static double square(double x) {
- return x * x;
- }
- private static void printTable(int i, int j, int k, Method f) {
- // TODO Auto-generated method stub
- double x = (j - i) / (k - 1);
- for (double l = i; l <= j; l += x) {
- double y;
- try {
- y = (Double) f.invoke(null, l);
- System.out.printf("%10.4f|%10.4f%n", l, y);
- } 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();
- }
- }
- }
- }
package senior;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MethodPointerTest {
/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException,
NoSuchMethodException {
// TODO Auto-generated method stub
Method square = MethodPointerTest.class.getMethod("square",
double.class);
Method sqrt = Math.class.getMethod("sqrt", double.class);
printTable(1, 10, 10, square);
printTable(1, 10, 10, sqrt);
}
public static double square(double x) {
return x * x;
}
private static void printTable(int i, int j, int k, Method f) {
// TODO Auto-generated method stub
double x = (j - i) / (k - 1);
for (double l = i; l <= j; l += x) {
double y;
try {
y = (Double) f.invoke(null, l);
System.out.printf("%10.4f|%10.4f%n", l, y);
} 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();
}
}
}
}
运行结果: