`
heji
  • 浏览: 87748 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

反射记录

    博客分类:
  • java
阅读更多
package com.heji.reflection;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MyReflection {

	private int a = 0;
	public float b = 1.0f;

	public void printf(String str) {
		System.out.println(str);
	}

	public static void main(String[] args) {

		try {
			System.out.println("=======打印出所有的方法=======");
			Class<?> cls = Class.forName("java.lang.Object");
			Method[] methods = cls.getDeclaredMethods();
			for (Method method : methods) {
				System.out.println(method.toString());
			}

			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======打印出所有的public方法=======");
			Method[] methods1 = cls.getMethods();
			for (Method method : methods1) {
				System.out.println(method.toString());
			}
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======获取构造函数=======");
			Constructor<?>[] constructors = cls.getDeclaredConstructors();
			for (Constructor<?> constructor : constructors) {
				System.out.println(constructor.toString());
			}
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======获取变量=======");
			Class<?> cls1 = Class.forName("com.heji.reflection.MyReflection");
			Field[] fields = cls1.getDeclaredFields();
			for (Field field : fields) {
				System.out.println(field.getName() + "; " + field.getType()
						+ "; " + Modifier.toString(field.getModifiers()));
			}
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======调用方法=======");
			Method method = cls1.getMethod("printf", new Class[] { String.class });
			Object object = cls1.newInstance();
			System.out.println(method.invoke(object, new Object[] { "Hello" }));
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======改变变量的值=======");
			MyReflection myReflection = new MyReflection();
			Field field1 = cls1.getDeclaredField("a");
			Field field2 = cls1.getField("b");
			System.out.println("before a = " + field1.get(myReflection));
			System.out.println("before b = " + field2.get(myReflection));
			
			field1.set(myReflection, 100);
			field2.set(myReflection, 20.0f);
			System.out.println("after a = " + field1.get(myReflection));
			System.out.println("after b = " + field2.get(myReflection));
			System.out.println("----------------------------");
			System.out.println();

			System.out.println("=======数组=======");
			Class<?> cls2 = Class.forName("java.lang.String");
			Object os = Array.newInstance(cls2, 8);
			Array.set(os, 7, "kjdkl");
			System.out.println(Array.get(os, 7));
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics