反射应用(读取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;
	}
}

posted on 2012-12-17 20:05  Miracle刘  阅读(444)  评论(0编辑  收藏  举报