- public class ReflectRun {
-
- public static void main(String[] args) throws java.lang.Exception {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- System.out.println("========Class区域========");
-
-
-
-
- Class clazz1 = ReflectPoint.class;
- Class clazz2 = new ReflectPoint().getClass();
-
-
-
-
-
- Class clazz3 = Class.forName("com.holle.ReflectPoint");
-
- System.out.println("clazz1 == clazz2: " + (clazz1 == clazz2));
- System.out.println("clazz1 == clazz3: " + (clazz1 == clazz3));
-
- System.out.println("clazz1是否是基本类型字节码: " + clazz1.isPrimitive());
- System.out.println("int是否是基本类型字节码: "
- + int.class.isPrimitive());
- System.out.println("int与Integer的字节码是否是同一个:"
- + (int.class == Integer.class));
-
- System.out.println("TYPE表示基本类型 的 Class 实例: "
- + (int.class == Integer.TYPE));
- System.out.println("int数组是否是基本类型字节码: "
- + int[].class.isPrimitive());
-
- System.out.println("int数组是否是数组字节码: "
- + int[].class.isArray());
-
-
-
- System.out.println("=======反射-构造函数Constructor区域=========");
-
-
-
-
- java.lang.reflect.Constructor cons = clazz3.getConstructor(int.class,
- int.class);
-
-
-
-
- ReflectPoint rpCons1 = (ReflectPoint) cons.newInstance(2, 3);
- System.out.println("x = " + rpCons1.x);
-
- ReflectPoint rpCons2 = (ReflectPoint) clazz3.newInstance();
- System.out.println("x = " + rpCons2.x);
-
-
- System.out.println("========反射-属性Field区域==========");
-
- java.lang.reflect.Field rpField1 = clazz3.getField("x");
-
-
-
-
- System.out.println("公用属性x = " + rpField1.get(rpCons1));
-
-
-
-
- java.lang.reflect.Field rpField2 = clazz3.getDeclaredField("y");
-
-
-
-
- rpField2.setAccessible(true);
- System.out.println("私有属性y = " + rpField2.get(rpCons1));
-
- java.lang.reflect.Field rpField3 = clazz3.getField("z");
- System.out.println("静态变量z = " + rpField3.get(null));
-
-
- ReflectPoint rp = new ReflectPoint();
- System.out.println(rp);
- changeValue(rp);
- System.out.println(rp);
-
-
- System.out.println("========反射-方法Method区域==========");
-
- java.lang.reflect.Method rpMethod1 = clazz3.getMethod("showPosition",
- int.class, int.class);
-
-
-
-
- rpMethod1.invoke(null, 5, 9);
-
- ReflectPoint rp2 = new ReflectPoint();
-
- java.lang.reflect.Method rpMethod2 = rp2.getClass().getMethod("setX",
- int.class);
- rpMethod2.invoke(rp2, 3);
- java.lang.reflect.Method rpMethod3 = rp2.getClass().getMethod("setY",
- int.class);
- rpMethod3.invoke(rp2, 5);
-
- java.lang.reflect.Method rpMethod4 = rp2.getClass().getMethod("getX",
- null);
- System.out.println(rpMethod4.invoke(rp2, null));
-
- java.lang.reflect.Method rpMethod5 = rp2.getClass().getDeclaredMethod(
- "getY", null);
- rpMethod5.setAccessible(true);
- System.out.println(rpMethod5.invoke(rp2, null));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- String strattingArgs = args[0];
- Class argsClazz = Class.forName(strattingArgs);
- java.lang.reflect.Method mainMethod = argsClazz.getMethod("main",
- String[].class);
-
-
-
-
-
-
-
-
-
- mainMethod
- .invoke(null, new Object[] { new String[] { "1", "2", "3" } });
-
-
-
-
- mainMethod.invoke(null, (Object) new String[] { "1", "2", "3" });
-
-
- java.lang.reflect.Method testMethod = argsClazz.getMethod("test",
- int[].class);
- testMethod.invoke(null, new int[] { 1 });
-
- java.lang.reflect.Method IntegerMethod = argsClazz.getMethod(
- "testInteger", Integer[].class);
- IntegerMethod.invoke(null, (Object) new Integer[] { 1 });
-
-
- int[] a1 = new int[] { 1, 2, 3 };
- int[] a2 = new int[] { 4, 5 };
- int[][] a3 = new int[2][3];
- Integer[] a4 = new Integer[] { new Integer(1), new Integer(2) };
- String[] a5 = new String[] { "a", "b", "c" };
-
- System.out.println(a1.getClass() == a2.getClass());
-
-
-
-
-
-
-
- System.out.println(a1.getClass().getName());
-
- System.out.println(a1.getClass().getSuperclass().getName());
- System.out.println(a4.getClass().getSuperclass().getName());
-
- Object obj1 = a1;
- System.out.println("编程Object对象后的自己嘛同样是本身:" + obj1.getClass().getName());
- Object obj2 = a4;
- Object obj3 = a5;
-
-
-
-
- Object[] objsa2 = a3;
- Object[] objsa3 = a5;
-
-
-
-
-
-
-
- System.out.println(a1);
- System.out.println(a4);
-
-
-
-
-
-
-
- System.out.println(java.util.Arrays.asList(a1));
- System.out.println(java.util.Arrays.asList(a4));
-
- printObject(a1);
- printObject(a4);
- printObject("COME BABY!!!");
-
-
-
-
-
-
- System.out.println(new Object[] { 1 }[0].getClass().getName());
- printObjectName(a1);
- printObjectName(a5);
-
- }
-
-
-
-
-
-
-
- private static void printObject(Object obj) {
- if (obj.getClass().isArray()) {
-
- int len = java.lang.reflect.Array.getLength(obj);
- for (int i = 0; i < len; i++) {
- System.out.print(java.lang.reflect.Array.get(obj, i));
- }
- System.out.println();
- } else {
- System.out.println(obj);
- }
- }
-
-
-
-
-
-
-
- private static void printObjectName(Object obj) {
- System.out.println(obj.getClass().getName());
- if (obj.getClass().isArray()) {
-
- int len = java.lang.reflect.Array.getLength(obj);
- for (int i = 0; i < len; i++) {
-
- System.out.println(java.lang.reflect.Array.get(obj, i)
- .getClass().getName());
-
- System.out.println(java.lang.reflect.Array.get(obj, i)
- .getClass() == Integer.class);
- }
- System.out.println();
- } else {
- System.out.println(obj);
- }
- }
-
-
-
-
-
-
- private static void changeValue(Object obj) throws Exception {
- java.lang.reflect.Field[] fields = obj.getClass().getFields();
- for (java.lang.reflect.Field f : fields) {
-
- if (f.getType() == String.class) {
- String oldValue = (String) f.get(obj);
- String newValue = oldValue.replace('b', 'a');
- f.set(obj, newValue);
- }
- }
-
- }
- }
-
-
-
-
-
-
-
- class TestArguments {
- public static void main(String[] args) {
- for (String arg : args) {
- System.out.println(arg);
- }
- }
-
- public static void test(int[] args) {
- for (int arg : args) {
- System.out.println(arg);
- }
- }
-
- public static void testInteger(Integer[] args) {
- for (Integer arg : args) {
- System.out.println(arg);
- }
- }
-
- }
-
-
-
-
-
- public class ReflectPoint {
-
- public int x = 0;
- private int y = 0;
- public String str1 = "ball";
- public String str2 = "baby";
- public String str3 = "asc";
- public static int z = 0;
-
- public ReflectPoint() {
- }
-
- public ReflectPoint(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- }
-
- public void setX(int x) {
- this.x = x;
- }
-
- public void setY(int y) {
- this.y = y;
- }
-
- public int getX() {
- return x;
- }
-
- private int getY() {
- return y;
- }
-
- public static void showPosition(int x, int y) {
- System.out.println("调用静态方法:(" + x + "," + y + ")");
- }
-
- @Override
- public String toString() {
- return str1 + ":" + str2 + ":" + str3;
- }
- }