• 反射应用(读取war包、jar包里的类,获得annotation的值)


    package com.miracle;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.util.jar.JarFile;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    
    public class myClassLoader extends ClassLoader {
    
    	@SuppressWarnings("unchecked")
    	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
    			InvocationTargetException {
    		try {
    			/*	Class test = new myClassLoader().loadClass("com.miracle.test.Conf", "E:\\test.jar");
    				Field[] field = test.getFields();
    				for (Field f : field) {
    					System.out.println(f.getName());
    				}*/
    			String className = "com.miracle.Test";
    			String warpath = "E:\\Ano.war";
    
    			Class t2 = new myClassLoader().loadClassFromWar("WEB-INF.classes." + className, warpath);
    			Field[] field = t2.getDeclaredFields();
    			//			Class ano = new myClassLoader().loadClassFromWar("WEB-INF.classes.com.miracle.MyAno", warpath);
    			//			Method m = ano.getMethod("value");
    			//			ano.getAnnotation(String.class);
    			//			Annotation a;
    
    			for (Field f : field) {
    				System.out.println(f.getName());
    				MyAno ano = f.getAnnotation(MyAno.class);
    				System.out.println(ano.value());
    			}
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public Class loadClassFromWar(String className, String warpath) throws ClassNotFoundException {
    		Class result = null;
    		byte[] classData = null;
    		try {
    			classData = getByteArrayFromWarFile(className, warpath);
    			result = defineClass(className.replace("WEB-INF.classes.", ""), classData, 0, classData.length);
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		return result;
    	}
    
    	private byte[] getByteArrayFromWarFile(String className, String warpath) {
    		int len;
    		byte buffer[] = new byte[1024];
    		ZipFile zipfile = null;
    		try {
    			zipfile = new ZipFile(warpath);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		className = className.replace('.', '/');
    		System.out.println(className);
    		try {
    			ZipEntry zipEntry = zipfile.getEntry(className + ".class");
    			if (zipEntry != null) {
    				System.out.println("zipEntry:" + zipEntry.getName());
    				try {
    					InputStream inputStream = zipfile.getInputStream(zipEntry);
    					int arrayLength = inputStream.available();
    					byte[] bytes = new byte[arrayLength];
    					int pos = 0;
    					while (true) {
    						int n = inputStream.read(bytes, pos, arrayLength - pos);
    						if (n <= 0)
    							break;
    						pos += n;
    					}
    					inputStream.close();
    					return bytes;
    				} catch (IOException exp) {
    					exp.printStackTrace();
    				}
    			} else {
    				System.out.println("not found");
    			}
    		} catch (Exception ex) {
    			//ex.printStackTrace();
    			return null;
    		}
    		return null;
    	}
    
    	public Class loadClass(String className, String jarpath) throws ClassNotFoundException {
    		Class result = null;
    		byte[] classData = null;
    		try {
    			classData = getByteArrayFromJarFile(className, jarpath);
    			result = defineClass(className, classData, 0, classData.length);
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    		return result;
    	}
    
    	private byte[] getByteArrayFromJarFile(String className, String jarpath) {
    		int len;
    		byte buffer[] = new byte[1024];
    		JarFile jarFile = null;
    		try {
    			jarFile = new JarFile(jarpath);
    		} catch (Exception ex) {
    		}
    		className = className.replace('.', '/');
    		try {
    			ZipEntry zipEntry = jarFile.getEntry(className + ".class");
    			if (zipEntry != null) {
    				try {
    					InputStream inputStream = jarFile.getInputStream(zipEntry);
    					int arrayLength = inputStream.available();
    					byte[] bytes = new byte[arrayLength];
    					int pos = 0;
    					while (true) {
    						int n = inputStream.read(bytes, pos, arrayLength - pos);
    						if (n <= 0)
    							break;
    						pos += n;
    					}
    					inputStream.close();
    					return bytes;
    				} catch (IOException exp) {
    					exp.printStackTrace();
    				}
    			}
    		} catch (Exception ex) {
    			//ex.printStackTrace();
    			return null;
    		}
    		return null;
    	}
    }

  • 相关阅读:
    TCP/IP,Http,Socket,XMPP的区别
    CygWin 常用命令
    js打印数组,js打印对象的方法
    中奖概率算法(php 可用于刮刮卡,大转盘等抽奖算法)
    sql语句优化
    求职简历怎么写,看看百度吐槽
    c#学习笔记之WPF Application和Windows Form Applications
    c#学习笔记之XML
    c#学习笔记之LINQ
    c#学习笔记之字符串和正则表达式
  • 原文地址:https://www.cnblogs.com/miraclespace/p/3712375.html
Copyright © 2020-2023  润新知