Search in sources :

Example 1 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-boot by spring-projects.

the class AbstractJsonMarshalTesterTests method parseMapShouldReturnContent.

@Test
public void parseMapShouldReturnContent() throws Exception {
    ResolvableType type = ResolvableTypes.get("mapOfExampleObject");
    AbstractJsonMarshalTester<Object> tester = createTester(type);
    assertThat(tester.parse(MAP_JSON)).asMap().containsEntry("a", OBJECT);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 2 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-boot by spring-projects.

the class AbstractJsonMarshalTesterTests method parseArrayShouldReturnContent.

@Test
public void parseArrayShouldReturnContent() throws Exception {
    ResolvableType type = ResolvableTypes.get("arrayOfExampleObject");
    AbstractJsonMarshalTester<Object> tester = createTester(type);
    assertThat(tester.parse(ARRAY_JSON)).asArray().containsOnly(OBJECT);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 3 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-boot by spring-projects.

the class DefinitionsParser method parseMockBeanAnnotation.

private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element) {
    Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value());
    Assert.state(!typesToMock.isEmpty(), "Unable to deduce type to mock from " + element);
    if (StringUtils.hasLength(annotation.name())) {
        Assert.state(typesToMock.size() == 1, "The name attribute can only be used when mocking a single class");
    }
    for (ResolvableType typeToMock : typesToMock) {
        MockDefinition definition = new MockDefinition(annotation.name(), typeToMock, annotation.extraInterfaces(), annotation.answer(), annotation.serializable(), annotation.reset(), QualifierDefinition.forElement(element));
        addDefinition(element, definition, "mock");
    }
}
Also used : ResolvableType(org.springframework.core.ResolvableType)

Example 4 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-boot by spring-projects.

the class AbstractJsonMarshalTesterTests method writeListShouldReturnJsonContent.

@Test
public void writeListShouldReturnJsonContent() throws Exception {
    ResolvableType type = ResolvableTypes.get("listOfExampleObject");
    List<ExampleObject> value = Collections.singletonList(OBJECT);
    JsonContent<Object> content = createTester(type).write(value);
    assertThat(content).isEqualToJson(ARRAY_JSON);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 5 with ResolvableType

use of org.springframework.core.ResolvableType in project spring-framework by spring-projects.

the class AbstractBeanFactory method isTypeMatch.

@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
    String beanName = transformedBeanName(name);
    // Check manually registered singletons.
    Object beanInstance = getSingleton(beanName, false);
    if (beanInstance != null) {
        if (beanInstance instanceof FactoryBean) {
            if (!BeanFactoryUtils.isFactoryDereference(name)) {
                Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
                return (type != null && typeToMatch.isAssignableFrom(type));
            } else {
                return typeToMatch.isInstance(beanInstance);
            }
        } else if (!BeanFactoryUtils.isFactoryDereference(name)) {
            if (typeToMatch.isInstance(beanInstance)) {
                // Direct match for exposed instance?
                return true;
            } else if (typeToMatch.hasGenerics() && containsBeanDefinition(beanName)) {
                // Generics potentially only match on the target class, not on the proxy...
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                Class<?> targetType = mbd.getTargetType();
                if (targetType != null && targetType != ClassUtils.getUserClass(beanInstance) && typeToMatch.isAssignableFrom(targetType)) {
                    // Check raw class match as well, making sure it's exposed on the proxy.
                    Class<?> classToMatch = typeToMatch.resolve();
                    return (classToMatch == null || classToMatch.isInstance(beanInstance));
                }
            }
        }
        return false;
    } else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
        // null instance registered
        return false;
    }
    // No singleton instance found -> check bean definition.
    BeanFactory parentBeanFactory = getParentBeanFactory();
    if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
        // No bean definition found in this factory -> delegate to parent.
        return parentBeanFactory.isTypeMatch(originalBeanName(name), typeToMatch);
    }
    // Retrieve corresponding bean definition.
    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
    Class<?> classToMatch = typeToMatch.resolve();
    if (classToMatch == null) {
        classToMatch = FactoryBean.class;
    }
    Class<?>[] typesToMatch = (FactoryBean.class == classToMatch ? new Class<?>[] { classToMatch } : new Class<?>[] { FactoryBean.class, classToMatch });
    // Check decorated bean definition, if any: We assume it'll be easier
    // to determine the decorated bean's type than the proxy's type.
    BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
    if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
        RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
        Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd, typesToMatch);
        if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
            return typeToMatch.isAssignableFrom(targetClass);
        }
    }
    Class<?> beanType = predictBeanType(beanName, mbd, typesToMatch);
    if (beanType == null) {
        return false;
    }
    // Check bean class whether we're dealing with a FactoryBean.
    if (FactoryBean.class.isAssignableFrom(beanType)) {
        if (!BeanFactoryUtils.isFactoryDereference(name)) {
            // If it's a FactoryBean, we want to look at what it creates, not the factory class.
            beanType = getTypeForFactoryBean(beanName, mbd);
            if (beanType == null) {
                return false;
            }
        }
    } else if (BeanFactoryUtils.isFactoryDereference(name)) {
        // Special case: A SmartInstantiationAwareBeanPostProcessor returned a non-FactoryBean
        // type but we nevertheless are being asked to dereference a FactoryBean...
        // Let's check the original bean class and proceed with it if it is a FactoryBean.
        beanType = predictBeanType(beanName, mbd, FactoryBean.class);
        if (beanType == null || !FactoryBean.class.isAssignableFrom(beanType)) {
            return false;
        }
    }
    ResolvableType resolvableType = mbd.targetType;
    if (resolvableType == null) {
        resolvableType = mbd.factoryMethodReturnType;
    }
    if (resolvableType != null && resolvableType.resolve() == beanType) {
        return typeToMatch.isAssignableFrom(resolvableType);
    }
    return typeToMatch.isAssignableFrom(beanType);
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) ResolvableType(org.springframework.core.ResolvableType)

Aggregations

ResolvableType (org.springframework.core.ResolvableType)96 Test (org.junit.Test)66 MethodParameter (org.springframework.core.MethodParameter)20 DataBuffer (org.springframework.core.io.buffer.DataBuffer)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)14 List (java.util.List)11 MediaType (org.springframework.http.MediaType)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 Map (java.util.Map)9 Assert (org.springframework.util.Assert)7 Collections (java.util.Collections)6 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)6 ArrayList (java.util.ArrayList)5 ReactiveAdapter (org.springframework.core.ReactiveAdapter)5 Pojo (org.springframework.http.codec.Pojo)5 ServerWebInputException (org.springframework.web.server.ServerWebInputException)5 Publisher (org.reactivestreams.Publisher)4 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)4 Single (rx.Single)4