com.greenhat.loader.ClassLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greenhat Show documentation
Show all versions of greenhat Show documentation
Why told green-hat,because i have one~
package com.greenhat.loader;
import com.greenhat.Config;
import com.greenhat.ioc.annotation.Bean;
import com.greenhat.ioc.annotation.Controller;
import com.greenhat.ioc.annotation.Service;
import com.greenhat.util.ClassUtil;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
public final class ClassLoader {
private static final List> CLASS_LIST;
static {
String basePackage= Config.APP_BASE_PACKAGE;
CLASS_LIST = ClassUtil.getClassList(basePackage);
}
public static List> getClassList() {
return CLASS_LIST;
}
public static List> getServiceClasses(){
List> classes=new ArrayList>();
for (Class> cls:CLASS_LIST){
if (cls.isAnnotationPresent(Service.class)){
classes.add(cls);
}
}
return classes;
}
public static List> getControllerClasses(){
List> classes=new ArrayList>();
for (Class> cls:CLASS_LIST){
if (cls.isAnnotationPresent(Controller.class)){
classes.add(cls);
}
}
return classes;
}
public static List> getBeanClasses(){
List> classes=new ArrayList>();
for (Class> cls:CLASS_LIST){
if (cls.isAnnotationPresent(Bean.class)){
classes.add(cls);
}
}
return classes;
}
/**
* 获取包名下某 父类【或接口】 的所有 子类【或实现类】
* @param superClass
* @return
*/
public static List> getClassListBySuper(Class> superClass){
List> classes=new ArrayList>();
for (Class> cls:CLASS_LIST){
if (superClass.isAssignableFrom(cls)&&!superClass.equals(cls)){
classes.add(cls);
}
}
return classes;
}
/**
* 获取应用包名下带有【某注解】的所有类
* @param annotationClass
* @return
*/
public static List> getClassListByAnnotation(Class extends Annotation> annotationClass){
List> classes=new ArrayList>();
for (Class> cls:CLASS_LIST){
if (cls.isAnnotationPresent(annotationClass)){
classes.add(cls);
}
}
return classes;
}
}