
cn.zkdcloud.util.FileUtil Maven / Gradle / Ivy
The newest version!
package cn.zkdcloud.util;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;
import org.apache.commons.io.FileUtils;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* file operator
* some thoughts source of latke
*/
public class FileUtil {
private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
/**
* 扫描默认路径,返回默认URLs
* @return urls
*/
public static List getDefaultURL(){
List rets = new ArrayList();
String defaultPath = classLoader.getResource("").getPath();
rets.addAll(getDirURL(defaultPath));
rets.addAll(getJarUrl(defaultPath,".class"));
return rets;
}
/**
* 默认资源路径是文件夹下的获取URL
* @param defaultDirPath defaultDir
* @return list>
*/
public static List getDirURL(String defaultDirPath){
List rets = new ArrayList();
File defaultDir = new File(defaultDirPath);
if(defaultDir.isDirectory()){
Collection listFiles = FileUtils.listFiles(defaultDir,new String[]{"class"},true);
for(File file : listFiles){
try {
rets.add(file.toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return rets;
}
return rets;
}
/**
* 扫描jar包中的class
* @param defaultDirPath scanJarPath
* @param suffix select suffix
* @return URL
*/
public static List getJarUrl(String defaultDirPath,String suffix){
List ret = new ArrayList();
if(defaultDirPath.contains(".jar")){
if(defaultDirPath.startsWith("file:/")){//去掉file
defaultDirPath = defaultDirPath.substring(5);
}
String originJarPath = defaultDirPath.substring(0,defaultDirPath.lastIndexOf(".jar") + 4);//原始jar路径
String commonPath = defaultDirPath.substring(originJarPath.length() + 2).replaceAll("!","");// 相对路径下的公共目录
defaultDirPath = originJarPath + "!/"; //package path to "jar:file:/path!/"
if(!defaultDirPath.startsWith("/")){
defaultDirPath = "jar:file:/" + defaultDirPath;
} else {
defaultDirPath = "jar:file:" + defaultDirPath;
}
if(!defaultDirPath.endsWith("!/")){
defaultDirPath = defaultDirPath.substring(0,defaultDirPath.lastIndexOf(".jar" + 4)) + "!/";
}
try {
URL jarFileURL = URI.create(defaultDirPath).toURL();//jar URL
JarFile jarFile = new JarFile(originJarPath);//jarFile
Enumeration entryEnumeration = jarFile.entries();
while(entryEnumeration.hasMoreElements()){
JarEntry jarEntry = entryEnumeration.nextElement();
if( jarEntry.getName().startsWith(commonPath) && jarEntry.getName().endsWith(suffix)){
ret.add(new URL(jarFileURL,jarEntry.getName()));
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
/**
* 扫描默认领,返回默认classes
* @return classes
*/
public static List getDefaultClass(){
List rets = new ArrayList();
List urls = getDefaultURL();
for(URL url : urls){
try {
ClassFile classFile = new ClassFile(new DataInputStream(url.openStream()));
String clazzName = classFile.getName();
Class clazz = classLoader.loadClass(clazzName);
rets.add(clazz);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return rets;
}
/**
* 根据类注解名获取classes
* @param annotationName condition
* @return ret
*/
public static List getClassByAnnotion(String annotationName){
List rets = new ArrayList();
List urls = getDefaultURL();
for(URL url : urls){
try {
ClassFile classFile = new ClassFile(new DataInputStream(url.openStream()));
AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
if(annotationsAttribute == null){
continue;
}
for(Annotation annotation : annotationsAttribute.getAnnotations()){ // find fit class by annotation condition
if(annotation.getTypeName().equals(annotationName)){
String clazzName = classFile.getName();
Class clazz = classLoader.loadClass(clazzName);
rets.add(clazz);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return rets;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy