方法调用:
(Class<? extends T>) ResolvableType.forClass(AbstractFailureAnalyzer.class, getClass()).resolveGeneric();
其中参数 getClass()
的值:class org.springframework.boot.diagnostics.AbstractFailureAnalyzer
/********** org.springframework.core.ResolvableType ******************/
public static ResolvableType forClass(Class<?> baseType, Class<?> implementationClass) {
Assert.notNull(baseType, "Base type must not be null");
// implementationClass : PortInUseFailureAnalyzer
// baseType : AbstractFailureAnalyzer
ResolvableType asType = forType(implementationClass).as(baseType);
return (asType == NONE ? forType(baseType) : asType);
}
static ResolvableType forType(
@Nullable Type type, @Nullable TypeProvider typeProvider, @Nullable VariableResolver variableResolver) {
if (type == null && typeProvider != null) {
type = SerializableTypeWrapper.forTypeProvider(typeProvider);
}
if (type == null) {
return NONE;
}
// For simple Class references, build the wrapper right away -
// no expensive resolution necessary, so not worth caching...
if (type instanceof Class) {
// 端口占用异常直接在这里返回
return new ResolvableType(type, typeProvider, variableResolver, (ResolvableType) null);
}
// Purge empty entries on access since we don't have a clean-up thread or the like.
cache.purgeUnreferencedEntries();
// Check the cache - we may have a ResolvableType which has been resolved before...
ResolvableType resultType = new ResolvableType(type, typeProvider, variableResolver);
ResolvableType cachedType = cache.get(resultType);
if (cachedType == null) {
cachedType = new ResolvableType(type, typeProvider, variableResolver, resultType.hash);
cache.put(cachedType, cachedType);
}
resultType.resolved = cachedType.resolved;
return resultType;
}
private ResolvableType(Type type, @Nullable TypeProvider typeProvider,
@Nullable VariableResolver variableResolver, @Nullable ResolvableType componentType) {
this.type = type;
this.typeProvider = typeProvider;
this.variableResolver = variableResolver;
this.componentType = componentType;
this.hash = null;
this.resolved = resolveClass();
}
@Nullable
private Class<?> resolveClass() {
if (this.type == EmptyType.INSTANCE) {
return null;
}
if (this.type instanceof Class) {
// 本例直接在这里返回this.type,也就是实际的类型
return (Class<?>) this.type;
}
if (this.type instanceof GenericArrayType) {
Class<?> resolvedComponent = getComponentType().resolve();
return (resolvedComponent != null ? Array.newInstance(resolvedComponent, 0).getClass() : null);
}
return resolveType().resolve();
}
public ResolvableType getGeneric(@Nullable int... indexes) {
ResolvableType[] generics = getGenerics();
if (indexes == null || indexes.length == 0) {
return (generics.length == 0 ? NONE : generics[0]);
}
ResolvableType generic = this;
for (int index : indexes) {
generics = generic.getGenerics();
if (index < 0 || index >= generics.length) {
return NONE;
}
generic = generics[index];
}
return generic;
}
public ResolvableType[] getGenerics() {
if (this == NONE) {
return EMPTY_TYPES_ARRAY;
}
ResolvableType[] generics = this.generics;
if (generics == null) {
if (this.type instanceof Class) {
Type[] typeParams = ((Class<?>) this.type).getTypeParameters();
generics = new ResolvableType[typeParams.length];
for (int i = 0; i < generics.length; i++) {
generics[i] = ResolvableType.forType(typeParams[i], this);
}
}
else if (this.type instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) this.type).getActualTypeArguments();
generics = new ResolvableType[actualTypeArguments.length];
for (int i = 0; i < actualTypeArguments.length; i++) {
generics[i] = forType(actualTypeArguments[i], this.variableResolver);
}
}
else {
generics = resolveType().getGenerics();
}
this.generics = generics;
}
return generics;
}
给this.generics赋值的调用栈
getGenerics:712, ResolvableType (org.springframework.core)
getGeneric:661, ResolvableType (org.springframework.core)
resolveGeneric:763, ResolvableType (org.springframework.core)
getCauseType:56, AbstractFailureAnalyzer (org.springframework.boot.diagnostics)
analyze:33, AbstractFailureAnalyzer (org.springframework.boot.diagnostics)
analyze:111, FailureAnalyzers (org.springframework.boot.diagnostics)
reportException:104, FailureAnalyzers (org.springframework.boot.diagnostics)
reportFailure:816, SpringApplication (org.springframework.boot)
handleRunFailure:801, SpringApplication (org.springframework.boot)
run:325, SpringApplication (org.springframework.boot)
setInitializer:30, HelloSpringBootApplication (com.yh.stu.springboot)
main:24, HelloSpringBootApplication (com.yh.stu.springboot)
getGenerics:693, ResolvableType (org.springframework.core)
getGeneric:661, ResolvableType (org.springframework.core)
resolveGeneric:763, ResolvableType (org.springframework.core)
getCauseType:56, AbstractFailureAnalyzer (org.springframework.boot.diagnostics)
analyze:33, AbstractFailureAnalyzer (org.springframework.boot.diagnostics)
analyze:111, FailureAnalyzers (org.springframework.boot.diagnostics)
reportException:104, FailureAnalyzers (org.springframework.boot.diagnostics)
reportFailure:816, SpringApplication (org.springframework.boot)
handleRunFailure:801, SpringApplication (org.springframework.boot)
run:325, SpringApplication (org.springframework.boot)
setInitializer:30, HelloSpringBootApplication (com.yh.stu.springboot)
main:24, HelloSpringBootApplication (com.yh.stu.springboot)
案例1 PortInUseFailureAnalyzer.getCauseType
getCauseType
在父类 AbstractFailureAnalyzer
中
/********** org.springframework.boot.diagnostics.AbstractFailureAnalyzer ******************/
protected Class<? extends T> getCauseType() {
// getClass() 调用的是子类PortInUseFailureAnalyzer的 class
return (Class<? extends T>)
ResolvableType.forClass(AbstractFailureAnalyzer.class, getClass()).resolveGeneric();
}
/********** org.springframework.core.ResolvableType ******************/
public static ResolvableType forClass(Class<?> baseType, Class<?> implementationClass) {
Assert.notNull(baseType, "Base type must not be null");
ResolvableType asType = forType(implementationClass).as(baseType);
return (asType == NONE ? forType(baseType) : asType);
}
我们将 ResolvableType asType = forType(implementationClass).as(baseType)
这行代码拆分一下:
// implementationClass 就是 PortInUseFailureAnalyzer.class
ResolvableType forType= forType(implementationClass);
// baseType 就是 AbstractFailureAnalyzer.class
ResolvableType asType = forType.as(baseType);
1、创建 PortInUseFailureAnalyzer 的 ResolvableType
this.type = PortInUseFailureAnalyzer.class
this.resolved = PortInUseFailureAnalyzer.class
2、创建父类 AbstractFailureAnalyzer 的 ResolvableType
/********** org.springframework.core.ResolvableType ******************/
public ResolvableType as(Class<?> type) {
if (this == NONE) {
return NONE;
}
Class<?> resolved = resolve();// 方法就是 return this.resolved,PortInUseFailureAnalyzer.class
if (resolved == null || resolved == type) {
return this;
}
for (ResolvableType interfaceType : getInterfaces()) { // 方法返回空集合
ResolvableType interfaceAsType = interfaceType.as(type);
if (interfaceAsType != NONE) {
return interfaceAsType;
}
}
return getSuperType().as(type);
}
public ResolvableType getSuperType() {
Class<?> resolved = resolve();// 方法就是 return this.resolved,PortInUseFailureAnalyzer.class
if (resolved == null || resolved.getGenericSuperclass() == null) {
return NONE;
}
ResolvableType superType = this.superType;
if (superType == null) {
// 创建
superType = forType(resolved.getGenericSuperclass(), this);
this.superType = superType;
}
return superType;
}
public static ResolvableType forType(@Nullable Type type, @Nullable ResolvableType owner) {
VariableResolver variableResolver = null;
if (owner != null) {
variableResolver = owner.asVariableResolver();
}
return forType(type, variableResolver);
}
ResolvableType asType = rt.as(AbstractFailureAnalyzer.class)