2007-07-24 01:44:20 +00:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
|
|
|
public class Reflection {
|
2011-02-23 00:54:56 +00:00
|
|
|
public static boolean booleanMethod() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte byteMethod() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static char charMethod() {
|
|
|
|
return '2';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static short shortMethod() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int intMethod() {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float floatMethod() {
|
|
|
|
return 5.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long longMethod() {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double doubleMethod() {
|
|
|
|
return 7.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2013-12-03 22:30:25 +00:00
|
|
|
private static class Hello {
|
|
|
|
private class World { }
|
|
|
|
}
|
2013-10-31 22:01:33 +00:00
|
|
|
|
|
|
|
private static void innerClasses() throws Exception {
|
|
|
|
Class c = Reflection.class;
|
|
|
|
Class[] inner = c.getDeclaredClasses();
|
|
|
|
expect(1 == inner.length);
|
|
|
|
expect(Hello.class == inner[0]);
|
|
|
|
}
|
|
|
|
|
2013-10-25 20:35:52 +00:00
|
|
|
private int egads;
|
|
|
|
|
|
|
|
private static void annotations() throws Exception {
|
|
|
|
Field egads = Reflection.class.getDeclaredField("egads");
|
|
|
|
expect(egads.getAnnotation(Deprecated.class) == null);
|
|
|
|
}
|
|
|
|
|
2007-07-24 01:44:20 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2013-10-31 22:01:33 +00:00
|
|
|
innerClasses();
|
2013-10-25 20:35:52 +00:00
|
|
|
annotations();
|
2013-10-31 22:01:33 +00:00
|
|
|
|
2007-07-24 01:44:20 +00:00
|
|
|
Class system = Class.forName("java.lang.System");
|
|
|
|
Field out = system.getDeclaredField("out");
|
2007-07-25 00:34:45 +00:00
|
|
|
Class output = Class.forName("java.io.PrintStream");
|
2007-07-24 01:44:20 +00:00
|
|
|
Method println = output.getDeclaredMethod("println", String.class);
|
|
|
|
|
|
|
|
println.invoke(out.get(null), "Hello, World!");
|
2011-02-23 00:54:56 +00:00
|
|
|
|
|
|
|
expect((Boolean) Reflection.class.getMethod("booleanMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(1 == (Byte) Reflection.class.getMethod("byteMethod").invoke(null));
|
|
|
|
|
|
|
|
expect('2' == (Character) Reflection.class.getMethod
|
|
|
|
("charMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(3 == (Short) Reflection.class.getMethod
|
|
|
|
("shortMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(4 == (Integer) Reflection.class.getMethod
|
|
|
|
("intMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(5.0 == (Float) Reflection.class.getMethod
|
|
|
|
("floatMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(6 == (Long) Reflection.class.getMethod
|
|
|
|
("longMethod").invoke(null));
|
|
|
|
|
|
|
|
expect(7.0 == (Double) Reflection.class.getMethod
|
|
|
|
("doubleMethod").invoke(null));
|
2013-10-25 18:23:41 +00:00
|
|
|
|
|
|
|
Class[][] array = new Class[][] { { Class.class } };
|
|
|
|
expect("[Ljava.lang.Class;".equals(array[0].getClass().getName()));
|
|
|
|
expect(Class[].class == array[0].getClass());
|
|
|
|
expect(array.getClass().getComponentType() == array[0].getClass());
|
2007-07-24 01:44:20 +00:00
|
|
|
}
|
|
|
|
}
|