黑马程序员---java学习笔记之java加强(三)

1、类中静态方法不能使用类上的泛型限定。

2、通过反射的方法获取某方法的泛型参数的泛型类型。

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Vector;



public class GenericDemo2 {

	public static void main(String[] args) throws Exception {
		
	Vector<Date> v = new Vector<Date>();
	
	Method method =GenericDemo2.class.getMethod("applyVector", Vector.class);
	
	Type[] type =method.getGenericParameterTypes();
	
	ParameterizedType t =(ParameterizedType)type[0];
	
	System.out.println(t.getRawType());
	System.out.println(t.getActualTypeArguments()[0]);
	

	}
	public static void applyVector(Vector<Date> v)
	{
		
	}

}

3、java提供的三个类加载器

        a):BootStrap(非java类),负责加载的类路径是:jre/lib/rt.jar

        b):ExtClassLoader,负责加载的类路径是:jre/lib/ext/*.jar

        c):AppClassLoader,负责加载的类路径是:classpath下的类


4、

System.out.println(ClassLoaderDemo.class.getClassLoader().getClass()
				.getName());//打印结果是:sun.misc.Launcher$AppClassLoader
		
		System.out.println(System.class.getClassLoader());//打印结果为null,因为
		//家在该类的加载器是BootStrap
		
		
		//下面通过代码来表明BootStrap、ExtClassLoader、AppClassLoader三个加载器之间的关系
		ClassLoader cl = ClassLoaderDemo.class.getClassLoader();
		
		while( cl != null )
		{
			System.out.println(cl.getClass().getName());
			//打印结果分别是:sun.misc.Launcher$AppClassLoader
			//sun.misc.Launcher$ExtClassLoader
			
			cl = cl.getParent();
		}
		//由打印结果可知,AppClassLoader的父加载器是ExtClassLoader,ExtClassLoader
		//的父加载器是BootStrap
5、 每个类加载器加载类时,先委托给其上级类加载器。这称为类加载器的委托机制。

6、类加载器的加密和解密

7、java servlet中的一个类加载器的问题

8、代理类,面向方面的编程(AOP)。

        业务交叉,面向方面编程,代理能够很好的解决这个问题。

        动态代理。

        CGLIB动态地生成没有借口的类的子类。

9、创建Collection的代理类的示例

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

public class ProxyDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Class proxyClass = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
		
		
		Constructor[] cons = proxyClass.getConstructors();
		for( Constructor con : cons )
		{
			String conName = con.getName();
			StringBuilder sBuilder = new StringBuilder(conName);
			sBuilder.append('(');
			
			Class[] prames = con.getParameterTypes();
			for( Class prame : prames )
			{
				sBuilder.append(prame.getName()+",");
			}
			
			if( (prames != null) && (prames.length != 0) )
				sBuilder.deleteCharAt(sBuilder.length()-1);
			
			sBuilder.append(')');
			
			System.out.println(sBuilder);
			
		}
		
		
		Method[] methods = proxyClass.getMethods();
		for( Method method : methods )
		{
			String name = method.getName();
			StringBuilder sBuilder = new StringBuilder(name);
			sBuilder.append('(');
			
			Class[] methodPrames = method.getParameterTypes();
			for( Class methodPrame : methodPrames )
			{
				sBuilder.append(methodPrame.getName()+",");
			}
			if( methodPrames != null  &&  methodPrames.length!= 0)
				sBuilder.deleteCharAt(sBuilder.length()-1);	
			sBuilder.append(')');
			
			System.out.println(sBuilder);
		}
	}

}
10、构造动态类对象的第一种方法:获取动态类字节码实例对象,之后获取该字节码对象的构造方法对象,然后使用该构造方法对象构造动态类的对象。

Class proxyClass = Proxy.getProxyClass(Collection.class.getClassLoader(),Collection.class);
		
		Collection coll = (Collection)proxyClass.getConstructor(InvocationHandler.class).newInstance(new InvocationHandler(){

			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				// TODO Auto-generated method stub
				return null;
			}
			
		} );
第二种方法,

Collection coll = (Collection)Proxy.newProxyInstance(Collection.class.getClass().getClassLoader(),
				new Class[]{Collection.class}, 
				new InvocationHandler(){

					@Override
					public Object invoke(Object proxy, Method method,
							Object[] args) throws Throwable {
						// TODO Auto-generated method stub
						return null;
					}
			
		});
11、动态类的一种使用:

		Collection coll = (Collection)Proxy.newProxyInstance(Collection.class.getClass().getClassLoader(),
				new Class[]{Collection.class}, 
				new InvocationHandler(){

					ArrayList target = new ArrayList();
					@Override
					public Object invoke(Object proxy, Method method,
							Object[] args) throws Throwable {
						long startTime = System.currentTimeMillis();
						Object retVal = method.invoke(target, args);
						long endTime = System.currentTimeMillis();
						
						System.out.println(endTime - startTime);
						
						return retVal;
					}
			
		});
		
		coll.add("abc");
		coll.add("123");
		coll.add("xyz");
		
		System.out.println(coll.size());
		

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值