Java Code Examples for org.springframework.core.ResolvableType#hasUnresolvableGenerics()
The following examples show how to use
org.springframework.core.ResolvableType#hasUnresolvableGenerics() .
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: ApplicationListenerMethodAdapter.java From spring-analysis-note with MIT License | 5 votes |
@Override public boolean supportsEventType(ResolvableType eventType) { for (ResolvableType declaredEventType : this.declaredEventTypes) { if (declaredEventType.isAssignableFrom(eventType)) { return true; } if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) { ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); if (declaredEventType.isAssignableFrom(payloadType)) { return true; } } } return eventType.hasUnresolvableGenerics(); }
Example 2
Source File: FormHttpMessageReader.java From spring-analysis-note with MIT License | 5 votes |
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { boolean multiValueUnresolved = elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.toClass()); return ((MULTIVALUE_STRINGS_TYPE.isAssignableFrom(elementType) || multiValueUnresolved) && (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType))); }
Example 3
Source File: ReturnTypeParser.java From springdoc-openapi with Apache License 2.0 | 5 votes |
/** * 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 4
Source File: ApplicationListenerMethodAdapter.java From java-technology-stack with MIT License | 5 votes |
@Override public boolean supportsEventType(ResolvableType eventType) { for (ResolvableType declaredEventType : this.declaredEventTypes) { if (declaredEventType.isAssignableFrom(eventType)) { return true; } if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) { ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); if (declaredEventType.isAssignableFrom(payloadType)) { return true; } } } return eventType.hasUnresolvableGenerics(); }
Example 5
Source File: FormHttpMessageReader.java From java-technology-stack with MIT License | 5 votes |
@Override public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) { return ((MULTIVALUE_TYPE.isAssignableFrom(elementType) || (elementType.hasUnresolvableGenerics() && MultiValueMap.class.isAssignableFrom(elementType.toClass()))) && (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType))); }
Example 6
Source File: ApplicationListenerMethodAdapter.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public boolean supportsEventType(ResolvableType eventType) { for (ResolvableType declaredEventType : this.declaredEventTypes) { if (declaredEventType.isAssignableFrom(eventType)) { return true; } else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) { ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); if (declaredEventType.isAssignableFrom(payloadType)) { return true; } } } return eventType.hasUnresolvableGenerics(); }
Example 7
Source File: AbstractJackson2HttpMessageConverter.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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 8
Source File: FastJsonHttpMessageConverter.java From uavstack with Apache License 2.0 | 5 votes |
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 9
Source File: ApplicationListenerMethodAdapter.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public boolean supportsEventType(ResolvableType eventType) { for (ResolvableType declaredEventType : this.declaredEventTypes) { if (declaredEventType.isAssignableFrom(eventType)) { return true; } else if (PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) { ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); if (declaredEventType.isAssignableFrom(payloadType)) { return true; } } } return eventType.hasUnresolvableGenerics(); }
Example 10
Source File: UiEventListenerMethodAdapter.java From cuba with Apache License 2.0 | 5 votes |
@Override public boolean supportsEventType(@Nonnull ResolvableType eventType) { for (ResolvableType declaredEventType : this.declaredEventTypes) { if (declaredEventType.isAssignableFrom(eventType)) { return true; } else if (eventType.getRawClass() != null && PayloadApplicationEvent.class.isAssignableFrom(eventType.getRawClass())) { ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric(); if (declaredEventType.isAssignableFrom(payloadType)) { return true; } } } return eventType.hasUnresolvableGenerics(); }
Example 11
Source File: GenericTypeAwareAutowireCandidateResolver.java From spring-analysis-note with MIT License | 4 votes |
/** * Match the given dependency type with its generic type information against the given * candidate bean definition. */ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { ResolvableType dependencyType = descriptor.getResolvableType(); if (dependencyType.getType() instanceof Class) { // No generic type -> we know it's a Class type-match, so no need to check again. return true; } ResolvableType targetType = null; boolean cacheType = false; RootBeanDefinition rbd = null; if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) { rbd = (RootBeanDefinition) bdHolder.getBeanDefinition(); } if (rbd != null) { targetType = rbd.targetType; if (targetType == null) { cacheType = true; // First, check factory method return type, if applicable targetType = getReturnTypeForFactoryMethod(rbd, descriptor); if (targetType == null) { RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd); if (dbd != null) { targetType = dbd.targetType; if (targetType == null) { targetType = getReturnTypeForFactoryMethod(dbd, descriptor); } } } } } if (targetType == null) { // Regular case: straight bean instance, with BeanFactory available. if (this.beanFactory != null) { Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName()); if (beanType != null) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType)); } } // Fallback: no BeanFactory set, or no type resolvable through it // -> best-effort match against the target class if applicable. if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) { Class<?> beanClass = rbd.getBeanClass(); if (!FactoryBean.class.isAssignableFrom(beanClass)) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass)); } } } if (targetType == null) { return true; } if (cacheType) { rbd.targetType = targetType; } if (descriptor.fallbackMatchAllowed() && (targetType.hasUnresolvableGenerics() || targetType.resolve() == Properties.class)) { // Fallback matches allow unresolvable generics, e.g. plain HashMap to Map<String,String>; // and pragmatically also java.util.Properties to any Map (since despite formally being a // Map<Object,Object>, java.util.Properties is usually perceived as a Map<String,String>). return true; } // Full check for complex generic type match... return dependencyType.isAssignableFrom(targetType); }
Example 12
Source File: GenericTypeAwareAutowireCandidateResolver.java From java-technology-stack with MIT License | 4 votes |
/** * Match the given dependency type with its generic type information against the given * candidate bean definition. */ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { ResolvableType dependencyType = descriptor.getResolvableType(); if (dependencyType.getType() instanceof Class) { // No generic type -> we know it's a Class type-match, so no need to check again. return true; } ResolvableType targetType = null; boolean cacheType = false; RootBeanDefinition rbd = null; if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) { rbd = (RootBeanDefinition) bdHolder.getBeanDefinition(); } if (rbd != null) { targetType = rbd.targetType; if (targetType == null) { cacheType = true; // First, check factory method return type, if applicable targetType = getReturnTypeForFactoryMethod(rbd, descriptor); if (targetType == null) { RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd); if (dbd != null) { targetType = dbd.targetType; if (targetType == null) { targetType = getReturnTypeForFactoryMethod(dbd, descriptor); } } } } } if (targetType == null) { // Regular case: straight bean instance, with BeanFactory available. if (this.beanFactory != null) { Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName()); if (beanType != null) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType)); } } // Fallback: no BeanFactory set, or no type resolvable through it // -> best-effort match against the target class if applicable. if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) { Class<?> beanClass = rbd.getBeanClass(); if (!FactoryBean.class.isAssignableFrom(beanClass)) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass)); } } } if (targetType == null) { return true; } if (cacheType) { rbd.targetType = targetType; } if (descriptor.fallbackMatchAllowed() && (targetType.hasUnresolvableGenerics() || targetType.resolve() == Properties.class)) { // Fallback matches allow unresolvable generics, e.g. plain HashMap to Map<String,String>; // and pragmatically also java.util.Properties to any Map (since despite formally being a // Map<Object,Object>, java.util.Properties is usually perceived as a Map<String,String>). return true; } // Full check for complex generic type match... return dependencyType.isAssignableFrom(targetType); }
Example 13
Source File: GenericTypeAwareAutowireCandidateResolver.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Match the given dependency type with its generic type information against the given * candidate bean definition. */ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { ResolvableType dependencyType = descriptor.getResolvableType(); if (dependencyType.getType() instanceof Class) { // No generic type -> we know it's a Class type-match, so no need to check again. return true; } ResolvableType targetType = null; boolean cacheType = false; RootBeanDefinition rbd = null; if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) { rbd = (RootBeanDefinition) bdHolder.getBeanDefinition(); } if (rbd != null) { targetType = rbd.targetType; if (targetType == null) { cacheType = true; // First, check factory method return type, if applicable targetType = getReturnTypeForFactoryMethod(rbd, descriptor); if (targetType == null) { RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd); if (dbd != null) { targetType = dbd.targetType; if (targetType == null) { targetType = getReturnTypeForFactoryMethod(dbd, descriptor); } } } } } if (targetType == null) { // Regular case: straight bean instance, with BeanFactory available. if (this.beanFactory != null) { Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName()); if (beanType != null) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType)); } } // Fallback: no BeanFactory set, or no type resolvable through it // -> best-effort match against the target class if applicable. if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) { Class<?> beanClass = rbd.getBeanClass(); if (!FactoryBean.class.isAssignableFrom(beanClass)) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass)); } } } if (targetType == null) { return true; } if (cacheType) { rbd.targetType = targetType; } if (descriptor.fallbackMatchAllowed() && targetType.hasUnresolvableGenerics()) { return true; } // Full check for complex generic type match... return dependencyType.isAssignableFrom(targetType); }
Example 14
Source File: GenericTypeAwareAutowireCandidateResolver.java From blog_demos with Apache License 2.0 | 4 votes |
/** * Match the given dependency type with its generic type information against the given * candidate bean definition. */ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { ResolvableType dependencyType = descriptor.getResolvableType(); if (dependencyType.getType() instanceof Class) { // No generic type -> we know it's a Class type-match, so no need to check again. return true; } ResolvableType targetType = null; RootBeanDefinition rbd = null; if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) { rbd = (RootBeanDefinition) bdHolder.getBeanDefinition(); } if (rbd != null) { // First, check factory method return type, if applicable targetType = getReturnTypeForFactoryMethod(rbd, descriptor); if (targetType == null) { RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd); if (dbd != null) { targetType = getReturnTypeForFactoryMethod(dbd, descriptor); } } } if (targetType == null) { // Regular case: straight bean instance, with BeanFactory available. if (this.beanFactory != null) { Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName()); if (beanType != null) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType)); } } // Fallback: no BeanFactory set, or no type resolvable through it // -> best-effort match against the target class if applicable. if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) { Class<?> beanClass = rbd.getBeanClass(); if (!FactoryBean.class.isAssignableFrom(beanClass)) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass)); } } } if (targetType == null || (descriptor.fallbackMatchAllowed() && targetType.hasUnresolvableGenerics())) { return true; } // Full check for complex generic type match... return dependencyType.isAssignableFrom(targetType); }
Example 15
Source File: GenericTypeAwareAutowireCandidateResolver.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Match the given dependency type with its generic type information against the given * candidate bean definition. */ protected boolean checkGenericTypeMatch(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { ResolvableType dependencyType = descriptor.getResolvableType(); if (dependencyType.getType() instanceof Class) { // No generic type -> we know it's a Class type-match, so no need to check again. return true; } ResolvableType targetType = null; RootBeanDefinition rbd = null; if (bdHolder.getBeanDefinition() instanceof RootBeanDefinition) { rbd = (RootBeanDefinition) bdHolder.getBeanDefinition(); } if (rbd != null) { // First, check factory method return type, if applicable targetType = getReturnTypeForFactoryMethod(rbd, descriptor); if (targetType == null) { RootBeanDefinition dbd = getResolvedDecoratedDefinition(rbd); if (dbd != null) { targetType = getReturnTypeForFactoryMethod(dbd, descriptor); } } } if (targetType == null) { // Regular case: straight bean instance, with BeanFactory available. if (this.beanFactory != null) { Class<?> beanType = this.beanFactory.getType(bdHolder.getBeanName()); if (beanType != null) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanType)); } } // Fallback: no BeanFactory set, or no type resolvable through it // -> best-effort match against the target class if applicable. if (targetType == null && rbd != null && rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) { Class<?> beanClass = rbd.getBeanClass(); if (!FactoryBean.class.isAssignableFrom(beanClass)) { targetType = ResolvableType.forClass(ClassUtils.getUserClass(beanClass)); } } } if (targetType == null || (descriptor.fallbackMatchAllowed() && targetType.hasUnresolvableGenerics())) { return true; } // Full check for complex generic type match... return dependencyType.isAssignableFrom(targetType); }