Java Code Examples for org.springframework.core.ResolvableType#NONE

The following examples show how to use org.springframework.core.ResolvableType#NONE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReturnTypeParser.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Find type for generics.
 *
 * @param generics the generics
 * @param typeArguments the type arguments
 * @param contextType the context type
 */
static void findTypeForGenerics(Class<?>[] generics, Type[] typeArguments, ResolvableType contextType) {
	for (int i = 0; i < typeArguments.length; i++) {
		Type typeArgument = typeArguments[i];
		if (typeArgument instanceof TypeVariable) {
			ResolvableType resolvedTypeArgument = resolveVariable(
					(TypeVariable<?>) typeArgument, contextType);
			if (resolvedTypeArgument != ResolvableType.NONE) {
				generics[i] = resolvedTypeArgument.resolve();
			}
			else {
				generics[i] = ResolvableType.forType(typeArgument).resolve();
			}
		}
		else {
			generics[i] = ResolvableType.forType(typeArgument).resolve();
		}
	}
}
 
Example 2
Source File: ReturnTypeParser.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve variable resolvable type.
 *
 * @param typeVariable the type variable
 * @param contextType the context type
 * @return the resolvable type
 */
static ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
	ResolvableType resolvedType;
	if (contextType.hasGenerics()) {
		resolvedType = ResolvableType.forType(typeVariable, contextType);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}

	ResolvableType superType = contextType.getSuperType();
	if (superType != ResolvableType.NONE) {
		resolvedType = resolveVariable(typeVariable, superType);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}
	for (ResolvableType ifc : contextType.getInterfaces()) {
		resolvedType = resolveVariable(typeVariable, ifc);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}
	return ResolvableType.NONE;
}
 
Example 3
Source File: AbstractJackson2HttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
	ResolvableType resolvedType;
	if (contextType.hasGenerics()) {
		resolvedType = ResolvableType.forType(typeVariable, contextType);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}

	ResolvableType superType = contextType.getSuperType();
	if (superType != ResolvableType.NONE) {
		resolvedType = resolveVariable(typeVariable, superType);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}
	for (ResolvableType ifc : contextType.getInterfaces()) {
		resolvedType = resolveVariable(typeVariable, ifc);
		if (resolvedType.resolve() != null) {
			return resolvedType;
		}
	}
	return ResolvableType.NONE;
}
 
Example 4
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private static ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
    ResolvableType resolvedType;
    if (contextType.hasGenerics()) {
        resolvedType = ResolvableType.forType(typeVariable, contextType);
        if (resolvedType.resolve() != null) {
            return resolvedType;
        }
    }

    ResolvableType superType = contextType.getSuperType();
    if (superType != ResolvableType.NONE) {
        resolvedType = resolveVariable(typeVariable, superType);
        if (resolvedType.resolve() != null) {
            return resolvedType;
        }
    }
    for (ResolvableType ifc : contextType.getInterfaces()) {
        resolvedType = resolveVariable(typeVariable, ifc);
        if (resolvedType.resolve() != null) {
            return resolvedType;
        }
    }
    return ResolvableType.NONE;
}
 
Example 5
Source File: ControlInitializerSupport.java    From jdal with Apache License 2.0 6 votes vote down vote up
protected List<Object> getEntityList(ResolvableType propertyType, String sortProperty) {
	Class<?> entityClass = null;

	if (propertyType.asCollection() != ResolvableType.NONE) {
		// look for generic type
		if (propertyType.hasGenerics()) {
			entityClass = propertyType.getGeneric().resolve();
		}
		else {
			if (log.isWarnEnabled())
				log.warn("Can't resolve generic type of [" + propertyType.toString() + "]");
		
			return new ArrayList<Object>();
		}
	}
	else  {
		entityClass = propertyType.resolve();
	}
	
	return getEntityList(entityClass, sortProperty);
}
 
Example 6
Source File: AbstractEncoderMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ResolvableType getElementType(ReactiveAdapter adapter, ResolvableType type) {
	if (adapter.isNoValue()) {
		return VOID_RESOLVABLE_TYPE;
	}
	else if (type != ResolvableType.NONE) {
		return type;
	}
	else {
		return OBJECT_RESOLVABLE_TYPE;
	}
}
 
Example 7
Source File: GenericApplicationListenerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
static ResolvableType resolveDeclaredEventType(Class<?> listenerType) {
	ResolvableType eventType = eventTypeCache.get(listenerType);
	if (eventType == null) {
		eventType = ResolvableType.forClass(listenerType).as(ApplicationListener.class).getGeneric();
		eventTypeCache.put(listenerType, eventType);
	}
	return (eventType != ResolvableType.NONE ? eventType : null);
}
 
Example 8
Source File: AbstractMessageWriterResultHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ResolvableType getElementType(ReactiveAdapter adapter, ResolvableType genericType) {
	if (adapter.isNoValue()) {
		return ResolvableType.forClass(Void.class);
	}
	else if (genericType != ResolvableType.NONE) {
		return genericType;
	}
	else {
		return ResolvableType.forClass(Object.class);
	}
}
 
Example 9
Source File: ReturnTypeParser.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * This is a copy of GenericTypeResolver.resolveType which is not available on spring 4.
 * This also keeps compatibility with spring-boot 1 applications.
 * Resolve the given generic type against the given context class,
 * substituting type variables as far as possible.
 * @param genericType the (potentially) generic type
 * @param contextClass a context class for the target type, for example a class in which the target type appears in a method signature (can be {@code null})
 * @return the resolved type (possibly the given generic type as-is)
 * @since 5.0
 */
static Type resolveType(Type genericType, @Nullable Class<?> contextClass) {
	if (contextClass != null) {
		if (genericType instanceof TypeVariable) {
			ResolvableType resolvedTypeVariable = resolveVariable(
					(TypeVariable<?>) genericType, ResolvableType.forClass(contextClass));
			if (resolvedTypeVariable != ResolvableType.NONE) {
				Class<?> resolved = resolvedTypeVariable.resolve();
				if (resolved != null) {
					return resolved;
				}
			}
		}
		else if (genericType instanceof ParameterizedType) {
			ResolvableType resolvedType = ResolvableType.forType(genericType);
			if (resolvedType.hasUnresolvableGenerics()) {
				ParameterizedType parameterizedType = (ParameterizedType) genericType;
				Class<?>[] generics = new Class<?>[parameterizedType.getActualTypeArguments().length];
				Type[] typeArguments = parameterizedType.getActualTypeArguments();
				ResolvableType contextType = ResolvableType.forClass(contextClass);
				findTypeForGenerics(generics, typeArguments, contextType);
				Class<?> rawClass = resolvedType.getRawClass();
				if (rawClass != null) {
					return ResolvableType.forClassWithGenerics(rawClass, generics).getType();
				}
			}
		}
	}
	return genericType;
}
 
Example 10
Source File: GenericApplicationListenerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
static ResolvableType resolveDeclaredEventType(Class<?> listenerType) {
	ResolvableType eventType = eventTypeCache.get(listenerType);
	if (eventType == null) {
		eventType = ResolvableType.forClass(listenerType).as(ApplicationListener.class).getGeneric();
		eventTypeCache.put(listenerType, eventType);
	}
	return (eventType != ResolvableType.NONE ? eventType : null);
}
 
Example 11
Source File: AbstractMessageWriterResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ResolvableType getElementType(ReactiveAdapter adapter, ResolvableType genericType) {
	if (adapter.isNoValue()) {
		return ResolvableType.forClass(Void.class);
	}
	else if (genericType != ResolvableType.NONE) {
		return genericType;
	}
	else {
		return ResolvableType.forClass(Object.class);
	}
}
 
Example 12
Source File: DefaultControllerAdvice.java    From oauth2-protocol-patterns with Apache License 2.0 5 votes vote down vote up
private List<ClientRegistration> getClientRegistrations() {
	ResolvableType type = ResolvableType.forInstance(this.clientRegistrationRepository).as(Iterable.class);
	if (type != ResolvableType.NONE && ClientRegistration.class.isAssignableFrom(type.resolveGenerics()[0])) {
		return StreamSupport.stream(((Iterable<ClientRegistration>) clientRegistrationRepository).spliterator(), false)
				.collect(Collectors.toList());
	}
	return Collections.emptyList();
}
 
Example 13
Source File: AbstractJackson2HttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the Jackson {@link JavaType} for the specified type and context class.
 * <p>The default implementation returns {@code typeFactory.constructType(type, contextClass)},
 * but this can be overridden in subclasses, to allow for custom generic collection handling.
 * For instance:
 * <pre class="code">
 * protected JavaType getJavaType(Type type) {
 *   if (type instanceof Class && List.class.isAssignableFrom((Class)type)) {
 *     return TypeFactory.collectionType(ArrayList.class, MyBean.class);
 *   } else {
 *     return super.getJavaType(type);
 *   }
 * }
 * </pre>
 * @param type the generic type to return the Jackson JavaType for
 * @param contextClass a context class for the target type, for example a class
 * in which the target type appears in a method signature (can be {@code null})
 * @return the Jackson JavaType
 */
protected JavaType getJavaType(Type type, Class<?> contextClass) {
	TypeFactory typeFactory = this.objectMapper.getTypeFactory();
	if (contextClass != null) {
		ResolvableType resolvedType = ResolvableType.forType(type);
		if (type instanceof TypeVariable) {
			ResolvableType resolvedTypeVariable = resolveVariable(
					(TypeVariable<?>) type, ResolvableType.forClass(contextClass));
			if (resolvedTypeVariable != ResolvableType.NONE) {
				return typeFactory.constructType(resolvedTypeVariable.resolve());
			}
		}
		else if (type instanceof ParameterizedType && resolvedType.hasUnresolvableGenerics()) {
			ParameterizedType parameterizedType = (ParameterizedType) type;
			Class<?>[] generics = new Class<?>[parameterizedType.getActualTypeArguments().length];
			Type[] typeArguments = parameterizedType.getActualTypeArguments();
			for (int i = 0; i < typeArguments.length; i++) {
				Type typeArgument = typeArguments[i];
				if (typeArgument instanceof TypeVariable) {
					ResolvableType resolvedTypeArgument = resolveVariable(
							(TypeVariable<?>) typeArgument, ResolvableType.forClass(contextClass));
					if (resolvedTypeArgument != ResolvableType.NONE) {
						generics[i] = resolvedTypeArgument.resolve();
					}
					else {
						generics[i] = ResolvableType.forType(typeArgument).resolve();
					}
				}
				else {
					generics[i] = ResolvableType.forType(typeArgument).resolve();
				}
			}
			return typeFactory.constructType(ResolvableType.
					forClassWithGenerics(resolvedType.getRawClass(), generics).getType());
		}
	}
	return typeFactory.constructType(type);
}
 
Example 14
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private static Type getType(Type type, Class<?> contextClass) {
    if (contextClass != null) {
        ResolvableType resolvedType = ResolvableType.forType(type);
        if (type instanceof TypeVariable) {
            ResolvableType resolvedTypeVariable = resolveVariable((TypeVariable) type, ResolvableType.forClass(contextClass));
            if (resolvedTypeVariable != ResolvableType.NONE) {
                return resolvedTypeVariable.resolve();
            }
        } else if (type instanceof ParameterizedType && resolvedType.hasUnresolvableGenerics()) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Class<?>[] generics = new Class[parameterizedType.getActualTypeArguments().length];
            Type[] typeArguments = parameterizedType.getActualTypeArguments();

            for (int i = 0; i < typeArguments.length; ++i) {
                Type typeArgument = typeArguments[i];
                if (typeArgument instanceof TypeVariable) {
                    ResolvableType resolvedTypeArgument = resolveVariable((TypeVariable) typeArgument, ResolvableType.forClass(contextClass));
                    if (resolvedTypeArgument != ResolvableType.NONE) {
                        generics[i] = resolvedTypeArgument.resolve();
                    } else {
                        generics[i] = ResolvableType.forType(typeArgument).resolve();
                    }
                } else {
                    generics[i] = ResolvableType.forType(typeArgument).resolve();
                }
            }

            return ResolvableType.forClassWithGenerics(resolvedType.getRawClass(), generics).getType();
        }
    }

    return type;
}
 
Example 15
Source File: LoginController.java    From tutorials with MIT License 5 votes vote down vote up
@GetMapping("/oauth_login")
public String getLoginPage(Model model) {
    Iterable<ClientRegistration> clientRegistrations = null;
    ResolvableType type = ResolvableType.forInstance(clientRegistrationRepository)
        .as(Iterable.class);
    if (type != ResolvableType.NONE && ClientRegistration.class.isAssignableFrom(type.resolveGenerics()[0])) {
        clientRegistrations = (Iterable<ClientRegistration>) clientRegistrationRepository;
    }

    clientRegistrations.forEach(registration -> oauth2AuthenticationUrls.put(registration.getClientName(), authorizationRequestBaseUri + "/" + registration.getRegistrationId()));
    model.addAttribute("urls", oauth2AuthenticationUrls);

    return "oauth_login";
}