node-web-mvc
Version:
node spring mvc
103 lines (102 loc) • 4.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module HandlerMethod
* @description action执行器
*/
const MethodParameter_1 = __importDefault(require("./MethodParameter"));
const Javascript_1 = __importDefault(require("../../interface/Javascript"));
const RuntimeAnnotation_1 = __importDefault(require("../annotations/annotation/RuntimeAnnotation"));
const ResponseStatus_1 = __importDefault(require("../annotations/ResponseStatus"));
class HandlerMethod {
get beanTypeName() {
return this.beanType ? this.beanType.name : '';
}
constructor(bean, method, beanFactory) {
if (method instanceof HandlerMethod) {
const handler = method;
this.isBeanType = handler.isBeanType;
this.method = handler.method;
this.methodName = handler.methodName;
this.bean = bean;
this.beanType = bean.constructor;
this.parameters = handler.parameters;
this.beanFactory = method.beanFactory;
this.responseStatus = handler.responseStatus;
this.responseStatusReason = handler.responseStatusReason;
this.resolvedFromHandlerMethod = method;
}
else {
const isType = typeof bean === 'function' || typeof bean === 'string';
this.isBeanType = isType;
this.method = method;
this.methodName = method ? method.name : '@@handler@@';
this.bean = isType ? null : bean;
this.beanType = isType ? bean : bean.constructor;
this.parameters = this.initMethodParameters();
this.beanFactory = beanFactory;
this.evaluateResponseStatus();
}
}
// 初始化参数
initMethodParameters() {
// TODO: 确保有底层注解
const methodAnno = RuntimeAnnotation_1.default.getMethodAnnotations(this.beanType, this.methodName)[0];
const parameterNames = (methodAnno === null || methodAnno === void 0 ? void 0 : methodAnno.parameters) || Javascript_1.default.resolveParameters(this.method);
return parameterNames.map((paramName, i) => {
var _a;
const dataType = (_a = methodAnno === null || methodAnno === void 0 ? void 0 : methodAnno.paramTypes) === null || _a === void 0 ? void 0 : _a[i];
return new MethodParameter_1.default(this.beanType, this.methodName, paramName, i, dataType);
});
}
/**
* 获取当前方法上的指定注解信息
* @param { Annotation } annotationType 要获取的注解类型类
*/
getAnnotation(annotationType) {
return this.getMethodAnnotation(annotationType);
}
/**
* 获取当前方法上的指定注解信息
* @param { Annotation } annotationType 要获取的注解类型类
*/
getMethodAnnotation(annotationType) {
var _a;
return (_a = RuntimeAnnotation_1.default.getMethodAnnotation(this.beanType, this.methodName, annotationType)) === null || _a === void 0 ? void 0 : _a.nativeAnnotation;
}
/**
* 获取当前方法所在类的指定类注解信息
* @param annotationType 要获取的类注解类型类
*/
getClassAnnotation(annotationType) {
var _a;
return (_a = RuntimeAnnotation_1.default.getClassAnnotation(this.beanType, annotationType)) === null || _a === void 0 ? void 0 : _a.nativeAnnotation;
}
/**
* 判定当前方法是否存在指定类型的注解
*/
hasMethodAnnotation(annotationType) {
return !!this.getMethodAnnotation(annotationType);
}
createWithResolvedBean() {
if (!this.isBeanType) {
return new HandlerMethod(this.bean, this);
}
const bean = this.beanFactory.getBean(this.beanType);
return new HandlerMethod(bean, this);
}
/**
* 从 ResponseStatus 获取当前action设定的返回状态,如果没有获取到则使用默认的
*/
evaluateResponseStatus() {
const annotation = this.getAnnotation(ResponseStatus_1.default);
if (annotation != null) {
this.responseStatus = annotation.code;
this.responseStatusReason = annotation.reason;
}
}
}
exports.default = HandlerMethod;