Example usage for org.springframework.core.convert.support GenericConversionService GenericConversionService

List of usage examples for org.springframework.core.convert.support GenericConversionService GenericConversionService

Introduction

In this page you can find the example usage for org.springframework.core.convert.support GenericConversionService GenericConversionService.

Prototype

GenericConversionService

Source Link

Usage

From source file:org.ldaptive.beans.spring.SpringLdapEntryMapper.java

/**
 * Creates an evaluation context to use in the spring class descriptor. Adds
 * the default converters from the default conversion service.
 *
 * @param  object  to supply to the evaluation context
 *
 * @return  evalutation context/* ww  w.j a v  a  2  s.c  om*/
 */
protected EvaluationContext createEvaluationContext(final Object object) {
    final GenericConversionService conversionService = new GenericConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    if (converters != null) {
        for (Converter<?, ?> converter : converters) {
            conversionService.addConverter(converter);
        }
    }
    addDefaultConverters(conversionService);

    final StandardEvaluationContext context = new StandardEvaluationContext(object);
    context.setTypeConverter(new StandardTypeConverter(conversionService));
    return context;
}

From source file:net.cpollet.jixture.fixtures.transformers.TestFileFixtureTransformer.java

@Before
public void setUp() throws NoSuchFieldException {
    GenericConversionService genericConversionService = new GenericConversionService();

    genericConversionService.addConverter(new Converter<String, Integer>() {
        @Override//from w  ww  . j  av a  2 s. co m
        public Integer convert(String source) {
            if (0 == source.length()) {
                return null;
            }
            return NumberUtils.parseNumber(source, Integer.class);
        }
    });

    Mockito.when(conversionServiceFactoryBean.getObject()).thenReturn(genericConversionService);

    mockCartEntry();
    mockProduct();
}

From source file:org.jboss.seam.spring.factorybean.TypeSafeCdiBeanLookup.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    try {//w  ww  . j  a  va  2s .c  o  m
        Object conversionServiceBeanInstance = applicationContext.getBean("conversionService");
        if (conversionServiceBeanInstance instanceof ConversionService) {
            this.conversionService = (ConversionService) conversionServiceBeanInstance;
        }
    } catch (BeansException e) {
        // do nothing
    }
    this.conversionService = new GenericConversionService();
}

From source file:ch.rasc.wampspring.method.PayloadArgumentResolverTest.java

@SuppressWarnings("resource")
@Before// w ww  .j  a  v a 2  s  .  c  o  m
public void setup() throws Exception {
    DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
    dlbf.registerSingleton("mvcValidator", testValidator());
    GenericApplicationContext ctx = new GenericApplicationContext(dlbf);
    ctx.refresh();
    this.resolver = new PayloadArgumentResolver(ctx,
            new MethodParameterConverter(new ObjectMapper(), new GenericConversionService()));
    this.payloadMethod = getClass().getDeclaredMethod("handleMessage", String.class, String.class, String.class,
            String.class, String.class, String.class, String.class, Integer.class);

    this.paramAnnotated = getMethodParameter(this.payloadMethod, 0);
    this.paramAnnotatedNotRequired = getMethodParameter(this.payloadMethod, 1);
    this.paramAnnotatedRequired = getMethodParameter(this.payloadMethod, 2);
    this.paramWithSpelExpression = getMethodParameter(this.payloadMethod, 3);
    this.paramValidated = getMethodParameter(this.payloadMethod, 4);
    this.paramValidated.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    this.paramValidatedNotAnnotated = getMethodParameter(this.payloadMethod, 5);
    this.paramNotAnnotated = getMethodParameter(this.payloadMethod, 6);
}

From source file:playground.Application.java

@Bean
ConversionService conversionService() {/*from   w w  w .  j  a  va  2  s . c om*/
    GenericConversionService service = new GenericConversionService();
    service.addConverter(new ReactiveStreamsToCompletableFutureConverter());
    service.addConverter(new ReactiveStreamsToRxJava1Converter());
    return service;
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testNullNestedTypeDescriptorWithBadConversionService() {
    Foo foo = new Foo();
    JuffrouSpringBeanWrapper wrapper = new JuffrouSpringBeanWrapper(foo);
    wrapper.setConversionService(new GenericConversionService() {
        @Override//  www.ja v  a2 s  .  c o m
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    wrapper.setAutoGrowNestedPaths(true);
    wrapper.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", foo.listOfMaps.get(0).get("luckyNumber"));
}

From source file:org.grails.plugins.AbstractGrailsPluginManager.java

/**
 * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
 * @param springConfig The RuntimeSpringConfiguration instance
 *///from  w ww. ja v  a2  s.  c om
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }

        converterRegistry.addConverter(
                new Converter<GrailsApplication, org.codehaus.groovy.grails.commons.GrailsApplication>() {
                    @Override
                    public org.codehaus.groovy.grails.commons.GrailsApplication convert(
                            GrailsApplication source) {
                        return new LegacyGrailsApplication(source);
                    }
                });
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {
            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment()
                && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}

From source file:org.kuali.rice.krad.datadictionary.DataDictionary.java

/**
 * Sets up the bean post processor and conversion service
 *
 * @param beans - The bean factory for the the dictionary beans
 *///from ww w . j  a v  a2s.c om
public static void setupProcessor(DefaultListableBeanFactory beans) {
    try {
        // UIF post processor that sets component ids
        BeanPostProcessor idPostProcessor = ComponentBeanPostProcessor.class.newInstance();
        beans.addBeanPostProcessor(idPostProcessor);
        beans.setBeanExpressionResolver(new StandardBeanExpressionResolver() {
            @Override
            protected void customizeEvaluationContext(StandardEvaluationContext evalContext) {
                try {
                    evalContext.registerFunction("getService", ExpressionFunctions.class
                            .getDeclaredMethod("getService", new Class[] { String.class }));
                } catch (NoSuchMethodException me) {
                    LOG.error("Unable to register custom expression to data dictionary bean factory", me);
                }
            }
        });

        // special converters for shorthand map and list property syntax
        GenericConversionService conversionService = new GenericConversionService();
        conversionService.addConverter(new StringMapConverter());
        conversionService.addConverter(new StringListConverter());

        beans.setConversionService(conversionService);
    } catch (Exception e1) {
        throw new DataDictionaryException(
                "Cannot create component decorator post processor: " + e1.getMessage(), e1);
    }
}

From source file:org.springframework.beans.AbstractPropertyAccessorTests.java

@Test
public void setPropertyIntermediateListIsNullWithBadConversionService() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new GenericConversionService() {
        @Override//from w  w w. ja va 2  s. co  m
        public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType,
                TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    accessor.setAutoGrowNestedPaths(true);
    accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testNullNestedTypeDescriptorWithBadConversionService() {
    Foo foo = new Foo();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(foo);
    wrapper.setConversionService(new GenericConversionService() {
        @Override//ww w  .ja va 2 s.co m
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    wrapper.setAutoGrowNestedPaths(true);
    wrapper.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", foo.listOfMaps.get(0).get("luckyNumber"));
}