JavaFX-Plus v1.beta
This commit is contained in:
100
src/main/java/cn/edu/scau/biubiusuisui/utils/ClassUtils.java
Normal file
100
src/main/java/cn/edu/scau/biubiusuisui/utils/ClassUtils.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package cn.edu.scau.biubiusuisui.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author jack
|
||||
* @Date:2019/6/25 5:20
|
||||
*/
|
||||
public class ClassUtils {
|
||||
private ClassLoader cl;
|
||||
|
||||
public ClassUtils() {
|
||||
cl = getClass().getClassLoader();
|
||||
}
|
||||
|
||||
private List<String> getAllFXControllerClassName(String base, List<String> nameList) {
|
||||
String splashPath = StringUtils.dotToSplash(base);
|
||||
URL url = cl.getResource(splashPath);
|
||||
String filePath = StringUtils.getRootPath(url);
|
||||
List<String> names = null;
|
||||
names = readFromDirectory(filePath);
|
||||
for (String name : names) {
|
||||
if (isClassFile(name)) {
|
||||
nameList.add(toFullyQualifiedName(name, base));
|
||||
} else if (isDirectory(name)){
|
||||
nameList = getAllFXControllerClassName(base + "." + name, nameList);
|
||||
}
|
||||
}
|
||||
return nameList;
|
||||
}
|
||||
|
||||
public List<String> scanAllClassName(String base) {
|
||||
return getAllFXControllerClassName(base, new LinkedList<>());
|
||||
}
|
||||
|
||||
private static String toFullyQualifiedName(String shortName, String basePackage) {
|
||||
StringBuilder sb = new StringBuilder(basePackage);
|
||||
sb.append('.');
|
||||
sb.append(StringUtils.trimExtension(shortName));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static boolean isClassFile(String name) {
|
||||
return name.endsWith(".class");
|
||||
}
|
||||
private static boolean isDirectory(String name){
|
||||
return !name.contains(".");
|
||||
}
|
||||
private static List<String> readFromDirectory(String path) {
|
||||
File file = new File(path);
|
||||
String[] names = file.list();
|
||||
if (null == names) {
|
||||
return null;
|
||||
} else {
|
||||
return Arrays.asList(names);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean hasDeclaredAnnotation(Class clazz, Class annotation){
|
||||
if(annotation == null){
|
||||
return false;
|
||||
}
|
||||
if (hasAnnotationInList(annotation, clazz.getDeclaredAnnotations())) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasAnnotation(Class clazz, Class annotation) {
|
||||
if(annotation == null){
|
||||
return false;
|
||||
}
|
||||
if (hasAnnotationInList(annotation, clazz.getAnnotations())) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasAnnotationInList(Class annotation, Annotation[] annotations2) {
|
||||
if(getAnnotationInList(annotation,annotations2) == null){
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static Annotation getAnnotationInList(Class annotation,Annotation[]annotations){
|
||||
if(annotations == null || annotation == null){
|
||||
return null;
|
||||
}
|
||||
for (Annotation annotation1 : annotations) {
|
||||
if (annotation1.annotationType().equals(annotation)) {
|
||||
return annotation1;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user