Open houyouliang opened 7 years ago
package com.ceair.pss.psdp.console.utils;
import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.jar.JarEntry; import java.util.jar.JarFile;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
/**
@date 2017年10月12日 下午10:38:09 */ public class ClassLoaderUtil { private static final Logger logger = LoggerFactory.getLogger(ClassLoaderUtil.class);
public static void getFieldMap(Object sourceObj, ClassLoader classLoader, String parentTarget, Map<String, Object> fieldsMap, List<Class<?>> list) { Class<?> clazz = sourceObj.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field f1 : fields) { Class<?> fieldClazz = f1.getType(); String name = f1.getName(); if (ParserConfig.primitiveClasses.contains(fieldClazz)) { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); } else { fieldsMap.put(name, fieldClazz.getName()); } } else if (fieldClazz.isAssignableFrom(List.class)) { Type fc = f1.getGenericType(); if (fc instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) fc; Class<?> genericClazz = (Class<?>) pt.getActualTypeArguments()[0]; // 【4】 if (genericClazz.getName().startsWith("java.lang") //设置list的终止类型 || genericClazz.getName().startsWith("java.util.Date") || genericClazz.getName().startsWith("javax") || genericClazz.getName().startsWith("com.sun") || genericClazz.getName().startsWith("sun") || genericClazz.getName().startsWith("boolean") || genericClazz.getName().startsWith("double") || genericClazz.getName().startsWith("int")) { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); //fieldList.add(parentTarget + "." + name); } else { fieldsMap.put(name, fieldClazz.getName()); //fieldList.add(name); } } else { try { Object obj = genericClazz.newInstance(); if (parentTarget != null) { getFieldMap(obj, classLoader, parentTarget + "." + name, fieldsMap, list); } else { getFieldMap(obj, classLoader, name, fieldsMap, list); } } catch (Exception e) { e.printStackTrace(); } } } else { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); //fieldList.add(parentTarget + "." + name); } else { fieldsMap.put(name, fieldClazz.getName()); //fieldList.add(name); } } } else { if (list.contains(fieldClazz)) { try { Object obj = fieldClazz.newInstance(); if (parentTarget != null) { getFieldMap(obj, classLoader, parentTarget + "." + name, fieldsMap, list); } else { getFieldMap(obj, classLoader, name, fieldsMap, list); } } catch (Exception e) { logger.error(e.getMessage()); } } } } }
public static Map<String, Object> findClassFields(String targetJarPath, String className) { Map<String, Object> fieldsMap = new TreeMap<> (); try { JarFile jarFile = ClassLoaderUtil.getJarFile(targetJarPath); ClassLoader classLoader = ClassLoaderUtil.getClassLoader(targetJarPath); List<Class<?>> list = ClassLoaderUtil.findClassByPath(classLoader, jarFile); Object obj = Class.forName(className).newInstance(); ClassLoaderUtil.getFieldMap(obj, classLoader, null, fieldsMap, list); return fieldsMap; } catch (Exception e) { logger.error(e.getMessage()); } return fieldsMap; }
public static void unistJar(String jarPath) throws Exception{ URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
File f = new File(jarPath); URL url = f.toURI().toURL(); Object ucpObj = null; Field ucpField = URLClassLoader.class.getDeclaredField("ucp"); ucpField.setAccessible(true); ucpObj = ucpField.get(classLoader); // 获得ucp内部的jarLoader Method m = ucpObj.getClass().getDeclaredMethod("getLoader", URL.class); m.setAccessible(true); Object jarLoader = m.invoke(ucpObj, url); String clsName = jarLoader.getClass().getName(); if(clsName.indexOf("JarLoader")!=-1){ m = jarLoader.getClass().getDeclaredMethod("getJarFile", URL.class); m.setAccessible(true); JarFile jf = (JarFile)m.invoke(jarLoader, url); System.out.println("release jar: "+jf.getName()); // 释放jarLoader中的jar文件 jf.close(); jf = null; }
} }
package com.ceair.pss.psdp.console.utils;
import java.io.File; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.jar.JarEntry; import java.util.jar.JarFile;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
/**
@date 2017年10月12日 下午10:38:09 */ public class ClassLoaderUtil { private static final Logger logger = LoggerFactory.getLogger(ClassLoaderUtil.class);
/**
/**
/**
add.setAccessible(true);
add.invoke(urlClassLoader, new Object[] {url1 }); return urlClassLoader; }
/**
public static void getFieldMap(Object sourceObj, ClassLoader classLoader, String parentTarget, Map<String, Object> fieldsMap, List<Class<?>> list) { Class<?> clazz = sourceObj.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field f1 : fields) { Class<?> fieldClazz = f1.getType(); String name = f1.getName(); if (ParserConfig.primitiveClasses.contains(fieldClazz)) { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); } else { fieldsMap.put(name, fieldClazz.getName()); } } else if (fieldClazz.isAssignableFrom(List.class)) { Type fc = f1.getGenericType(); if (fc instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) fc; Class<?> genericClazz = (Class<?>) pt.getActualTypeArguments()[0]; // 【4】 if (genericClazz.getName().startsWith("java.lang") //设置list的终止类型 || genericClazz.getName().startsWith("java.util.Date") || genericClazz.getName().startsWith("javax") || genericClazz.getName().startsWith("com.sun") || genericClazz.getName().startsWith("sun") || genericClazz.getName().startsWith("boolean") || genericClazz.getName().startsWith("double") || genericClazz.getName().startsWith("int")) { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); //fieldList.add(parentTarget + "." + name); } else { fieldsMap.put(name, fieldClazz.getName()); //fieldList.add(name); } } else { try { Object obj = genericClazz.newInstance(); if (parentTarget != null) { getFieldMap(obj, classLoader, parentTarget + "." + name, fieldsMap, list); } else { getFieldMap(obj, classLoader, name, fieldsMap, list); } } catch (Exception e) { e.printStackTrace(); } } } else { if (parentTarget != null) { fieldsMap.put(parentTarget + "." + name, fieldClazz.getName()); //fieldList.add(parentTarget + "." + name); } else { fieldsMap.put(name, fieldClazz.getName()); //fieldList.add(name); } } } else { if (list.contains(fieldClazz)) { try { Object obj = fieldClazz.newInstance(); if (parentTarget != null) { getFieldMap(obj, classLoader, parentTarget + "." + name, fieldsMap, list); } else { getFieldMap(obj, classLoader, name, fieldsMap, list); } } catch (Exception e) { logger.error(e.getMessage()); } } } } }
public static Map<String, Object> findClassFields(String targetJarPath, String className) { Map<String, Object> fieldsMap = new TreeMap<> (); try { JarFile jarFile = ClassLoaderUtil.getJarFile(targetJarPath); ClassLoader classLoader = ClassLoaderUtil.getClassLoader(targetJarPath); List<Class<?>> list = ClassLoaderUtil.findClassByPath(classLoader, jarFile); Object obj = Class.forName(className).newInstance(); ClassLoaderUtil.getFieldMap(obj, classLoader, null, fieldsMap, list); return fieldsMap; } catch (Exception e) { logger.error(e.getMessage()); } return fieldsMap; }
public static void unistJar(String jarPath) throws Exception{ URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
} }