根据jar包路径获取POPJ包含继承关系属性与字段
来源:互联网 发布:讲你知有没有国语 编辑:程序博客网 时间:2024/06/06 18:25
import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarFile;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.self.drools.service.DroolsClasspathLoader;public class JarClassJavaBeanUtils{private static final Logger logger = LoggerFactory.getLogger(JarClassJavaBeanUtils.class); public static void getJarJavaBeanInfo(List<JarEntry> jarEntrys , String classJaveBeanName , JarJavaBeanInfo info) { String superJaveBeanName = null ; for(JarEntry entry : jarEntrys){ if(entry.getName().indexOf("META-INF")<0){ try {String classFullName = entry.getName();if(classFullName.indexOf(".class")<0){ classFullName = classFullName.substring(0,classFullName.length()-1);} else{ String className = classFullName.substring(0,classFullName.length()-6).replace("/", "."); if(! classJaveBeanName.equals(className)){ continue ; } Class<?> myclass = DroolsClasspathLoader.classloader.loadClass(className); superJaveBeanName = myclass.getSuperclass().getName() ; Class<?> clazz = Class.forName(className); Field[] fields = clazz.getDeclaredFields(); for(Field f : fields){ String type = f.getType().getName() ; int k = type.lastIndexOf(".") ; if(k != -1){ type = type.substring(k+1) ; } if(type.equals("List")){ String _ls = f.getGenericType().getTypeName() ; _ls = _ls.substring(_ls.indexOf("<")+1 , _ls.lastIndexOf(">")) ; if(_ls.lastIndexOf(".") != -1){ _ls = _ls.substring(_ls.lastIndexOf(".")+1) ; } type += "<" + _ls + ">" ; } info.getDeclaredFields().add(new JarJavaBeanInfo.DeclaredField(f.getName(), type)) ; } Method m[] = myclass.getMethods(); for(Method md : m){ List<String> types = new ArrayList<String>() ; Class<?>[] p = md.getParameterTypes() ; for(Class<?> i : p){ String type = i.getTypeName() ; int k = type.lastIndexOf(".") ; if(k != -1){ type = type.substring(k+1) ; } types.add(type) ; } info.getMethods().add(new JarJavaBeanInfo.DeclaredMethods(types, md.getName())) ; }}} catch (ClassNotFoundException e) { logger.error("获取类的属性和方法失败,ClassNotFoundException"+e.getMessage());} catch (SecurityException e) { logger.error("获取类的属性和方法失败"+e.getMessage());} } } if(superJaveBeanName == null){ return ; } if(!"java.lang.Object".equals(superJaveBeanName)){ getJarJavaBeanInfo(jarEntrys , superJaveBeanName , info) ; } } public static void getJarJavaBeanInfo(String classJaveBeanName , List<String> addJarPathFiles , JarJavaBeanInfo info) throws Exception { DroolsClasspathLoader.loadClasspath(addJarPathFiles) ; List<JarEntry> jarEntrys = new ArrayList<JarEntry>() ; for(String jarPath : addJarPathFiles){ JarFile jar = new JarFile(jarPath); Enumeration<JarEntry> enumFiles = jar.entries(); while(enumFiles.hasMoreElements()){ jarEntrys.add(enumFiles.nextElement()) ; } } getJarJavaBeanInfo(jarEntrys, classJaveBeanName, info) ; } public static List<String> getClassJavaBeanName(String jarFile , List<String> addJarPathFiles) throws Exception { List<String> classJaveBeanNames = new ArrayList<String>() ; try{ DroolsClasspathLoader.loadClasspath(addJarPathFiles) ; JarFile jar = new JarFile(jarFile); Enumeration<JarEntry> enumFiles = jar.entries(); JarEntry entry; while(enumFiles.hasMoreElements()){ entry = (JarEntry)enumFiles.nextElement(); if(entry.getName().indexOf("META-INF")<0){ String classFullName = entry.getName(); if(classFullName.indexOf(".class")<0){ classFullName = classFullName.substring(0,classFullName.length()-1); } else{ String className = classFullName.substring(0,classFullName.length()-6).replace("/", "."); classJaveBeanNames.add(className) ; } } } } catch(IOException e){ logger.error("从jar包中获取类名失败"+e.getMessage()); } catch(Exception e){ logger.error("从jar包中获取类名失败"+e.getMessage()); } return classJaveBeanNames ; }}
import java.util.List;import java.util.Set;import java.util.TreeSet;public class JarJavaBeanInfo {public static class DeclaredField implements Comparable<DeclaredField>{private String field ;private String type ;private String show ;public DeclaredField(String field, String type) {super();this.field = field;this.type = type;this.show = type + " " + field ;}public String getShow() {return show;}public void setShow(String show) {this.show = show;}public String getField() {return field;}public void setField(String field) {this.field = field;}public String getType() {return type;}public void setType(String type) {this.type = type;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((field == null) ? 0 : field.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;DeclaredField other = (DeclaredField) obj;if (field == null) {if (other.field != null)return false;} else if (!field.equals(other.field))return false;return true;}@Overridepublic String toString() {return " [field=" + field + ", type=" + type + "]";} public int compareTo(DeclaredField other) {return this.field.toLowerCase().compareTo(other.field.toLowerCase()) ;}}public static class DeclaredMethods implements Comparable<DeclaredMethods>{public List<String> types ;public String methodName ;public String show ;public int compareTo(DeclaredMethods other) {return this.methodName.compareTo(other.methodName) ;}public List<String> getTypes() {return types;}public void setTypes(List<String> types) {this.types = types;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}public String getShow() {return show;}public void setShow(String show) {this.show = show;}public DeclaredMethods(List<String> types, String methodName) {super();this.types = types;this.methodName = methodName;if(this.types == null || this.types.size() == 0){this.show = this.methodName + " ( ) " ; return ;}this.show = this.methodName + " ( " ;int n = types.size() ;for(int i = 0 ; i <= n-2 ; i++){this.show += types.get(i) + " , " ;}this.show += types.get(n-1) + " ) " ;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result+ ((methodName == null) ? 0 : methodName.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;DeclaredMethods other = (DeclaredMethods) obj;if (methodName == null) {if (other.methodName != null)return false;} else if (!methodName.equals(other.methodName))return false;return true;}}private Set<DeclaredField> declaredFields ;private Set<DeclaredMethods> methods ;public JarJavaBeanInfo() {super();this.declaredFields = new TreeSet<DeclaredField>() ;this.methods = new TreeSet<DeclaredMethods>() ;}public Set<DeclaredField> getDeclaredFields() {return declaredFields;}public void setDeclaredFields(Set<DeclaredField> declaredFields) {this.declaredFields = declaredFields;}public Set<DeclaredMethods> getMethods() {return methods;}public void setMethods(Set<DeclaredMethods> methods) {this.methods = methods;}}
import java.io.File;import java.lang.reflect.Method;import java.net.URL;import java.net.URLClassLoader;import java.util.ArrayList;import java.util.List; public final class DroolsClasspathLoader { private static Method addURL = initAddMethod(); public static URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); private static Method initAddMethod() { try { Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); add.setAccessible(true); return add; } catch (Exception e) { throw new RuntimeException(e); } } public static void loadClasspath(List<String> pathFiles) { for (String f : pathFiles) { loadClasspath(f); } } private static void loadClasspath(String filepath) { File file = new File(filepath); loopFiles(file); } private static void loadResourceDir(String filepath) { File file = new File(filepath); loopDirs(file); } private static void loopDirs(File file) { // 资源文件只加载路径 if (file.isDirectory()) { addURL(file); File[] tmps = file.listFiles(); for (File tmp : tmps) { loopDirs(tmp); } } } private static void loopFiles(File file) { if (file.isDirectory()) { File[] tmps = file.listFiles(); for (File tmp : tmps) { loopFiles(tmp); } } else { if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) { addURL(file); } } } private static void addURL(File file) { try { addURL.invoke(classloader, new Object[] { file.toURI().toURL() }); } catch (Exception e) { } } private static List<String> getJarFiles() { return new ArrayList<String>(); } private static List<String> getResFiles() { return new ArrayList<String>(); } }
import java.util.Arrays;import java.util.List;import com.alibaba.fastjson.JSONObject;import com.self.drools.maven.jar.JarClassJavaBeanUtils;import com.self.drools.maven.jar.JarJavaBeanInfo;public class JarClassJavaBeanUtilsTest { public static void main(String[] args) throws Exception { testGetClassJavaBeanName() ; testGetJarJavaBeanInfo() ; } public static void testGetClassJavaBeanName() throws Exception{ List<String> classJavaBeans = JarClassJavaBeanUtils.getClassJavaBeanName( "C:\\Users\\liyang\\Desktop\\mjars\\49\\org .jar" ,Arrays.asList("C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar")) ; System.out.println(classJavaBeans) ; } public static void testGetJarJavaBeanInfo() throws Exception{ JarJavaBeanInfo info = new JarJavaBeanInfo() ; JarClassJavaBeanUtils.getJarJavaBeanInfo( "com..." ,Arrays.asList("C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar" , "C:\\Users\\liyang\\Desktop\\mjars\\49...jar"), info) ; System.out.println(JSONObject.toJSON(info)) ; }}
阅读全文
0 0
- 根据jar包路径获取POPJ包含继承关系属性与字段
- 获取JAR包的路径
- Java获取Jar包路径
- 获取当前 jar 包的路径
- 获取jar包中的文件路径
- Spring jar 包详解 与 依赖关系
- 【Java 路径】运行可执行jar包获取相关路径
- 这个网站不错,根据引用jar包路径查找原JAR包
- 泛型类的继承与包含关系中的未指定标签
- C++ 根据当前路径与相对路径,获取绝对路径
- Java获取class/jar包路径的方法
- Java获取Jar包路径(android自动构建)
- 获取正在运行的类所在jar包的路径
- Java如何获取当前的jar包路径
- Java如何获取当前的jar包路径以及如何读取jar包中的资源
- jar包路径问题
- jar包 路径配置
- 读jar包路径
- POJ 1509 后缀自动机
- 实现Runnable接口,通过Thread启动多线程
- 绝对定位居中,宽度多宽都没事
- CentOS7下搭建nexus
- 视频剪辑软件怎么调声像和音量?
- 根据jar包路径获取POPJ包含继承关系属性与字段
- 使用passthrough模式,只通过PCI任意设备(该过程是在RHEL7系统进行,其他系统原理一样)
- Ubuntu 16.04 LTS安装telnet & ssh
- Oracle表空间专题系列
- phpstudy是个好东西
- Active Learning: 一个降低深度学习时间,空间,经济成本的解决方案
- Android 5.x新增控件之-TextInputLayout控件
- Python 3 极简教程.pdf
- 数据科学家需要掌握的10个基本统计技术