java反射学习笔记-core java

1.反射概述

反射库是个强大的工具库,大量用于javaBean中。能够分析类能力的程序都被称为反射。

作用有:

1.分析类的结构。(后面实现了一个类结构分析器)

2.在运行时查看对象。(不管是公共还是私有的)后面实现了一个超级攻击类,万能toString()

3.实现数组的操作代码

4.利用method对象,很像C++的函数指针。(可以调用类似于单例模式的私有化的构造器)

2.利用反射分析类的能力

利用反射,分析类的构造器,私有变量,方法等。
/**
	 * 源码结构查看器(这里只实现了查看构造函数= =!)
	 * java.lang.Double
	 * java.io.FileInputStream
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		
		Scanner scanner = new Scanner(System.in);
		String className = scanner.next();
		String tmpString = "";
		Class c = Class.forName(className);
		Class[] interfaces = c.getInterfaces();
		Class cp = c.getSuperclass();
		System.out.println("public class "+className+" extends "+cp.toString()+" implements ");
		for(Class tmp:interfaces){
			tmpString+=tmp.toString()+" ";
		}
		System.out.println(tmpString);
		System.out.println("{");
		Constructor[] cons = c.getConstructors();
		tmpString = "";
		//输出构造函数
		for(Constructor tmp:cons){
			String modi = Modifier.toString(tmp.getModifiers());
			for(Class pt:tmp.getParameterTypes()){
				tmpString+=pt.toString()+",";
			}
			tmpString = tmpString.replace("class ", "");
			System.out.println("	"+modi+" "+tmp.getName()+"("+tmpString.substring(0, tmpString.length()-1)+")");
		}
		//
		System.out.println("}");
	}

3.运行时使用反射分析对象

利用反射可以访问类的私有变量
Field.setAccessible(true);
这个方法是Accessible的一个方法,这是Field, Method ,Constructor的超类
public class SuperToStringUtils {

	public static void main(String[] args) throws Exception {
		Student c = new Student(1001,"隔壁老王","软件工程");
		System.out.println(SuperToStringUtils.toString(c));
	}
	
	public static <T> String toString(Object c) throws Exception{
		String out = "";
		Class cl = c.getClass();
		Field[] fields = cl.getDeclaredFields() ;
		if(cl==String.class)
			return (String) c;
		for(Field f:fields){
			f.setAccessible(true);
			out+="属性"+f.getName()+"的值为:"+f.get(c)+"	";
			}
		return out;
	}
}
class Student{
	private int id;
	private String name;
	private String classname;
	private java.io.File File = new java.io.File("./file.tmp");
	private Date date = new Date(System.currentTimeMillis());
	public Student(int id, String name, String classname) {
		super();
		this.id = id;
		this.name = name;
		this.classname = classname;
	}
	public Student() {
		super();
	}
}


4.调用私有的方法或者构造函数

下面举一个单例模式中的一个问题,哪种单例的实现形式是绝对的单例

public class NotSingletion {


	/**
	 * 由此可见~~单例模式也不是绝对的,除了第三个用枚举类实现的,老夫真的没辙了
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		
		Constructor constructor = (Constructor<SingletionA>) SingletionA.class.getDeclaredConstructors()[0];
		constructor.setAccessible(true);
		System.out.println("No1 Instance:"+SingletionA.getInstance());
		System.out.println("No2 Instance:"+constructor.newInstance());
		System.out.println("No3 Instance:"+constructor.newInstance());
		System.out.println("==========================================");
		constructor = (Constructor<SingletionB>) SingletionB.class.getDeclaredConstructors()[0];
		constructor.setAccessible(true);
		System.out.println("No1 Instance:"+SingletionA.getInstance());
		System.out.println("No2 Instance:"+constructor.newInstance());
		System.out.println("No3 Instance:"+constructor.newInstance());
		System.out.println("==========================================");
		constructor = (Constructor<SingletionC>) SingletionC.class.getDeclaredConstructors()[0];
		constructor.setAccessible(true);
		System.out.println("No1 Instance:"+SingletionA.getInstance());
		System.out.println("No2 Instance:"+constructor.newInstance());
		System.out.println("No3 Instance:"+constructor.newInstance());
	}
}
class SingletionA{
        private SingletionA(){  
              
        }  
        private static class lazyHolder{  
            private static SingletionA init = new SingletionA();  
        }  
        public static SingletionA getInstance(){  
            return lazyHolder.init;  
    }  
}
class SingletionB{
	private static SingletionB singletion = new SingletionB();
	private SingletionB(){
	}
	public static SingletionB getInstance(){
			if(singletion==null){  
				synchronized(SingletionB.class)  {         
					singletion = new SingletionB();  
				}  
			}
		
		return singletion;
	}
}
enum SingletionC{
	instance{
		public void doing(){
			System.out.println("123456");
		}
	};
	public abstract void doing();
}

No1 Instance:day20150730.SingletionA@2f9ee1ac
No2 Instance:day20150730.SingletionA@3fbefab0
No3 Instance:day20150730.SingletionA@133c5982
==========================================
No1 Instance:day20150730.SingletionA@2f9ee1ac
No2 Instance:day20150730.SingletionB@3d4b7453
No3 Instance:day20150730.SingletionB@24c21495
==========================================
No1 Instance:day20150730.SingletionA@2f9ee1ac
Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
at java.lang.reflect.Constructor.newInstance(Constructor.java:511)
at day20150730.NotSingletion.main(NotSingletion.java:25)

A ,B两种方法都弄出来了不同的实例,第三个没有构造器,所以没办法弄出别的实例来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值