diff --git a/src/helpers/SimApiControllerScanner.cj b/src/helpers/SimApiControllerScanner.cj index 17a216e..84895ba 100644 --- a/src/helpers/SimApiControllerScanner.cj +++ b/src/helpers/SimApiControllerScanner.cj @@ -15,6 +15,7 @@ import std.collection.* import std.core.* import std.reflect.* import soulsoft_web_mvc.core.* +import simapi.interfaces.* /** * 控制器自动扫描器:从调用栈定位调用者包,枚举该包(含子包)中继承 Controller 的类型。 @@ -33,6 +34,18 @@ public class SimApiControllerScanner { result.toArray() } + /** + * 扫描调用者包及其所有子包中 ISimApiAuthChecker 的实现类。 + * 对齐 C# AddSimApi 中遍历调用者程序集 AddScoped 注册 checker 的机制。 + * @return 找到的 checker 实现类型列表(不含抽象类型与接口本身)。 + */ + public static func scanAuthCheckers(): Array { + let callerPackage = getCallerPackage() + var result = ArrayList() + collectImplementations(callerPackage, TypeInfo.of(), result) + result.toArray() + } + /** * 获取调用者(应用)包名:遍历栈帧,跳过 simapi/soulsoft/std 等框架包, * 返回第一个应用包的 declaringClass(对齐 C# 通过 StackTrace 找调用程序集)。 @@ -101,4 +114,39 @@ public class SimApiControllerScanner { } false } + + /// 收集指定包及其子包中实现指定接口的非抽象类 + private static func collectImplementations(packageName: String, interfaceType: TypeInfo, + result: ArrayList): Unit { + if (packageName.isEmpty()) { + return + } + try { + let info = PackageInfo.get(packageName) + for (ti in info.typeInfos) { + if (isImplementation(ti, interfaceType)) { + result.add(ti) + } + } + for (sub in info.subPackages) { + collectImplementations("${packageName}.${sub.name}", interfaceType, result) + } + } catch (_: Exception) { + // 包不存在时跳过 + } + } + + /// 判断类型是否为接口的非抽象实现类 + private static func isImplementation(typeInfo: TypeInfo, interfaceType: TypeInfo): Bool { + if (let classTypeInfo: ClassTypeInfo <- typeInfo) { + if (classTypeInfo.isAbstract()) { + return false + } + if (typeInfo == interfaceType) { + return false + } + return typeInfo.isSubtypeOf(interfaceType) + } + false + } }