兼容@Resource标记在泛型类型字段上

This commit is contained in:
Redkale
2019-01-03 09:06:08 +08:00
parent b7930f1ed7
commit 2a3b8f87d3
2 changed files with 23 additions and 1 deletions

View File

@@ -570,7 +570,8 @@ public final class ResourceFactory {
if (Modifier.isStatic(field.getModifiers())) continue; if (Modifier.isStatic(field.getModifiers())) continue;
field.setAccessible(true); field.setAccessible(true);
final Class classtype = field.getType(); final Class classtype = field.getType();
final Type genctype = field.getGenericType(); final Type genctype = TypeToken.containsUnknownType(field.getGenericType())
? TypeToken.getGenericType(field.getGenericType(), src.getClass()) : field.getGenericType();
Resource rc = field.getAnnotation(Resource.class); Resource rc = field.getAnnotation(Resource.class);
if (rc == null) { //深度注入 if (rc == null) { //深度注入
boolean flag = true; //是否没有重复 boolean flag = true; //是否没有重复

View File

@@ -56,6 +56,27 @@ public abstract class TypeToken<T> {
return true; return true;
} }
public final static boolean containsUnknownType(final Type type) {
if (type == null) return false;
if (type instanceof Class) return false;
if (type instanceof WildcardType) return true;
if (type instanceof TypeVariable) return true;
if (type instanceof GenericArrayType) return containsUnknownType(((GenericArrayType) type).getGenericComponentType());
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
if (containsUnknownType(pt.getRawType())) return true;
if (containsUnknownType(pt.getOwnerType())) return true;
Type[] ts = pt.getActualTypeArguments();
if (ts != null) {
for (Type t : ts) {
if (containsUnknownType(t)) return true;
}
}
return false;
}
return true;
}
public final static Class typeToClass(final Type type) { public final static Class typeToClass(final Type type) {
if (type instanceof Class) return (Class) type; if (type instanceof Class) return (Class) type;
if (type instanceof WildcardType) return null; if (type instanceof WildcardType) return null;