Browse Source

Polishing

pull/1260/head
Juergen Hoeller 10 years ago
parent
commit
f473392a42
  1. 6
      spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java
  2. 12
      spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
  3. 17
      spring-core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java
  4. 50
      spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  5. 30
      spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java
  6. 15
      spring-core/src/test/java/org/springframework/tests/TestResourceUtils.java
  7. 11
      spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java

6
spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -112,9 +112,9 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
* Add a new BeanFactoryPostProcessor that will get applied to the internal * Add a new BeanFactoryPostProcessor that will get applied to the internal
* bean factory of this application context on refresh, before any of the * bean factory of this application context on refresh, before any of the
* bean definitions get evaluated. To be invoked during context configuration. * bean definitions get evaluated. To be invoked during context configuration.
* @param beanFactoryPostProcessor the factory processor to register * @param postProcessor the factory processor to register
*/ */
void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor); void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);
/** /**
* Add a new ApplicationListener that will be notified on context events * Add a new ApplicationListener that will be notified on context events

12
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -53,6 +53,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.HierarchicalMessageSource; import org.springframework.context.HierarchicalMessageSource;
import org.springframework.context.LifecycleProcessor; import org.springframework.context.LifecycleProcessor;
@ -404,8 +405,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
} }
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) { public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
this.beanFactoryPostProcessors.add(beanFactoryPostProcessor); Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
this.beanFactoryPostProcessors.add(postProcessor);
} }
@ -418,6 +420,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
public void addApplicationListener(ApplicationListener<?> listener) { public void addApplicationListener(ApplicationListener<?> listener) {
Assert.notNull(listener, "ApplicationListener must not be null");
if (this.applicationEventMulticaster != null) { if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener); this.applicationEventMulticaster.addApplicationListener(listener);
} }
@ -560,11 +563,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
// Configure the bean factory with context callbacks. // Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
// BeanFactory interface not registered as resolvable type in a plain factory. // BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean. // MessageSource registered (and found for autowiring) as a bean.

17
spring-core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,15 +27,16 @@ public interface ConverterRegistry {
/** /**
* Add a plain converter to this registry. * Add a plain converter to this registry.
* The convertible sourceType/targetType pair is derived from the Converter's parameterized types. * The convertible source/target type pair is derived from the Converter's parameterized types.
* @throws IllegalArgumentException if the parameterized types could not be resolved * @throws IllegalArgumentException if the parameterized types could not be resolved
*/ */
void addConverter(Converter<?, ?> converter); void addConverter(Converter<?, ?> converter);
/** /**
* Add a plain converter to this registry. * Add a plain converter to this registry.
* The convertible sourceType/targetType pair is specified explicitly. * The convertible source/target type pair is specified explicitly.
* Allows for a Converter to be reused for multiple distinct pairs without having to create a Converter class for each pair. * <p>Allows for a Converter to be reused for multiple distinct pairs without
* having to create a Converter class for each pair.
* @since 3.1 * @since 3.1
*/ */
void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter); void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter);
@ -47,13 +48,13 @@ public interface ConverterRegistry {
/** /**
* Add a ranged converter factory to this registry. * Add a ranged converter factory to this registry.
* The convertible sourceType/rangeType pair is derived from the ConverterFactory's parameterized types. * The convertible source/target type pair is derived from the ConverterFactory's parameterized types.
* @throws IllegalArgumentException if the parameterized types could not be resolved. * @throws IllegalArgumentException if the parameterized types could not be resolved
*/ */
void addConverterFactory(ConverterFactory<?, ?> converterFactory); void addConverterFactory(ConverterFactory<?, ?> factory);
/** /**
* Remove any converters from sourceType to targetType. * Remove any converters from {@code sourceType} to {@code targetType}.
* @param sourceType the source type * @param sourceType the source type
* @param targetType the target type * @param targetType the target type
*/ */

50
spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -81,8 +81,10 @@ public class GenericConversionService implements ConfigurableConversionService {
public void addConverter(Converter<?, ?> converter) { public void addConverter(Converter<?, ?> converter) {
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converter, Converter.class); GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converter, Converter.class);
Assert.notNull(typeInfo, "Unable to the determine sourceType <S> and targetType " + if (typeInfo == null) {
"<T> which your Converter<S, T> converts between; declare these generic types."); throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
"Converter [" + converter.getClass().getName() + "]; does the class parameterize those types?");
}
addConverter(new ConverterAdapter(converter, typeInfo)); addConverter(new ConverterAdapter(converter, typeInfo));
} }
@ -96,14 +98,13 @@ public class GenericConversionService implements ConfigurableConversionService {
invalidateCache(); invalidateCache();
} }
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) { public void addConverterFactory(ConverterFactory<?, ?> factory) {
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class); GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(factory, ConverterFactory.class);
if (typeInfo == null) { if (typeInfo == null) {
throw new IllegalArgumentException("Unable to the determine sourceType <S> and " + throw new IllegalArgumentException("Unable to determine source type <S> and target type <T> for your " +
"targetRangeType R which your ConverterFactory<S, R> converts between; " + "ConverterFactory [" + factory.getClass().getName() + "]; does the class parameterize those types?");
"declare these generic types.");
} }
addConverter(new ConverterFactoryAdapter(converterFactory, typeInfo)); addConverter(new ConverterFactoryAdapter(factory, typeInfo));
} }
public void removeConvertible(Class<?> sourceType, Class<?> targetType) { public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
@ -129,17 +130,18 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
/** /**
* Returns true if conversion between the sourceType and targetType can be bypassed. * Return whether conversion between the sourceType and targetType can be bypassed.
* More precisely this method will return true if objects of sourceType can be * <p>More precisely, this method will return true if objects of sourceType can be
* converted to the targetType by returning the source object unchanged. * converted to the targetType by returning the source object unchanged.
* @param sourceType context about the source type to convert from (may be null if source is null) * @param sourceType context about the source type to convert from
* (may be {@code null} if source is {@code null})
* @param targetType context about the target type to convert to (required) * @param targetType context about the target type to convert to (required)
* @return true if conversion can be bypassed * @return {@code true} if conversion can be bypassed; {@code false} otherwise
* @throws IllegalArgumentException if targetType is null * @throws IllegalArgumentException if targetType is {@code null}
* @since 3.2 * @since 3.2
*/ */
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType, "The targetType to convert to cannot be null"); Assert.notNull(targetType, "targetType to convert to cannot be null");
if (sourceType == null) { if (sourceType == null) {
return true; return true;
} }
@ -149,18 +151,18 @@ public class GenericConversionService implements ConfigurableConversionService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T convert(Object source, Class<T> targetType) { public <T> T convert(Object source, Class<T> targetType) {
Assert.notNull(targetType,"The targetType to convert to cannot be null"); Assert.notNull(targetType, "targetType to convert to cannot be null");
return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType)); return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
} }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Assert.notNull(targetType,"The targetType to convert to cannot be null"); Assert.notNull(targetType, "targetType to convert to cannot be null");
if (sourceType == null) { if (sourceType == null) {
Assert.isTrue(source == null, "The source must be [null] if sourceType == [null]"); Assert.isTrue(source == null, "source must be [null] if sourceType == [null]");
return handleResult(sourceType, targetType, convertNullSource(sourceType, targetType)); return handleResult(null, targetType, convertNullSource(null, targetType));
} }
if (source != null && !sourceType.getObjectType().isInstance(source)) { if (source != null && !sourceType.getObjectType().isInstance(source)) {
throw new IllegalArgumentException("The source to convert from must be an instance of " + throw new IllegalArgumentException("source to convert from must be an instance of " +
sourceType + "; instead it was a " + source.getClass().getName()); sourceType + "; instead it was a " + source.getClass().getName());
} }
GenericConverter converter = getConverter(sourceType, targetType); GenericConverter converter = getConverter(sourceType, targetType);
@ -198,7 +200,7 @@ public class GenericConversionService implements ConfigurableConversionService {
/** /**
* Template method to convert a null source. * Template method to convert a null source.
* <p>Default implementation returns {@code null}. * <p>The default implementation returns {@code null}.
* Subclasses may override to return custom null objects for specific target types. * Subclasses may override to return custom null objects for specific target types.
* @param sourceType the sourceType to convert from * @param sourceType the sourceType to convert from
* @param targetType the targetType to convert to * @param targetType the targetType to convert to
@ -213,11 +215,10 @@ public class GenericConversionService implements ConfigurableConversionService {
* First queries this ConversionService's converter cache. * First queries this ConversionService's converter cache.
* On a cache miss, then performs an exhaustive search for a matching converter. * On a cache miss, then performs an exhaustive search for a matching converter.
* If no converter matches, returns the default converter. * If no converter matches, returns the default converter.
* Subclasses may override.
* @param sourceType the source type to convert from * @param sourceType the source type to convert from
* @param targetType the target type to convert to * @param targetType the target type to convert to
* @return the generic converter that will perform the conversion, or {@code null} if * @return the generic converter that will perform the conversion,
* no suitable converter was found * or {@code null} if no suitable converter was found
* @see #getDefaultConverter(TypeDescriptor, TypeDescriptor) * @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
*/ */
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
@ -243,9 +244,8 @@ public class GenericConversionService implements ConfigurableConversionService {
/** /**
* Return the default converter if no converter is found for the given sourceType/targetType pair. * Return the default converter if no converter is found for the given sourceType/targetType pair.
* Returns a NO_OP Converter if the sourceType is assignable to the targetType. * <p>Returns a NO_OP Converter if the sourceType is assignable to the targetType.
* Returns {@code null} otherwise, indicating no suitable converter could be found. * Returns {@code null} otherwise, indicating no suitable converter could be found.
* Subclasses may override.
* @param sourceType the source type to convert from * @param sourceType the source type to convert from
* @param targetType the target type to convert to * @param targetType the target type to convert to
* @return the default generic converter that will perform the conversion * @return the default generic converter that will perform the conversion

30
spring-core/src/main/java/org/springframework/core/io/support/EncodedResource.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,7 @@ import org.springframework.util.ObjectUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @since 1.2.6 * @since 1.2.6
* @see Resource#getInputStream()
* @see java.io.Reader * @see java.io.Reader
* @see java.nio.charset.Charset * @see java.nio.charset.Charset
*/ */
@ -51,7 +52,7 @@ public class EncodedResource {
/** /**
* Create a new {@code EncodedResource} for the given {@code Resource}, * Create a new {@code EncodedResource} for the given {@code Resource},
* not specifying an explicit encoding or {@code Charset}. * not specifying an explicit encoding or {@code Charset}.
* @param resource the {@code Resource} to hold; never {@code null} * @param resource the {@code Resource} to hold (never {@code null})
*/ */
public EncodedResource(Resource resource) { public EncodedResource(Resource resource) {
this(resource, null, null); this(resource, null, null);
@ -60,7 +61,7 @@ public class EncodedResource {
/** /**
* Create a new {@code EncodedResource} for the given {@code Resource}, * Create a new {@code EncodedResource} for the given {@code Resource},
* using the specified {@code encoding}. * using the specified {@code encoding}.
* @param resource the {@code Resource} to hold; never {@code null} * @param resource the {@code Resource} to hold (never {@code null})
* @param encoding the encoding to use for reading from the resource * @param encoding the encoding to use for reading from the resource
*/ */
public EncodedResource(Resource resource, String encoding) { public EncodedResource(Resource resource, String encoding) {
@ -70,7 +71,7 @@ public class EncodedResource {
/** /**
* Create a new {@code EncodedResource} for the given {@code Resource}, * Create a new {@code EncodedResource} for the given {@code Resource},
* using the specified {@code Charset}. * using the specified {@code Charset}.
* @param resource the {@code Resource} to hold; never {@code null} * @param resource the {@code Resource} to hold (never {@code null})
* @param charset the {@code Charset} to use for reading from the resource * @param charset the {@code Charset} to use for reading from the resource
*/ */
public EncodedResource(Resource resource, Charset charset) { public EncodedResource(Resource resource, Charset charset) {
@ -85,6 +86,7 @@ public class EncodedResource {
this.charset = charset; this.charset = charset;
} }
/** /**
* Return the {@code Resource} held by this {@code EncodedResource}. * Return the {@code Resource} held by this {@code EncodedResource}.
*/ */
@ -140,8 +142,8 @@ public class EncodedResource {
} }
/** /**
* Open a {@code java.io.InputStream} for the specified resource, ignoring any * Open an {@code InputStream} for the specified resource, ignoring any specified
* specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}. * {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}.
* @throws IOException if opening the InputStream failed * @throws IOException if opening the InputStream failed
* @see #requiresReader() * @see #requiresReader()
* @see #getReader() * @see #getReader()
@ -152,17 +154,17 @@ public class EncodedResource {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object other) {
if (obj == this) { if (this == other) {
return true; return true;
} }
if (obj instanceof EncodedResource) { if (!(other instanceof EncodedResource)) {
EncodedResource that = (EncodedResource) obj; return false;
return (this.resource.equals(that.resource) &&
ObjectUtils.nullSafeEquals(this.charset, that.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, that.encoding));
} }
return false; EncodedResource otherResource = (EncodedResource) other;
return (this.resource.equals(otherResource.resource) &&
ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding));
} }
@Override @Override

15
spring-core/src/test/java/org/springframework/tests/TestResourceUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.tests; package org.springframework.tests;
import static java.lang.String.format;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
/** /**
@ -25,22 +23,17 @@ import org.springframework.core.io.ClassPathResource;
* *
* @author Chris Beams * @author Chris Beams
*/ */
public class TestResourceUtils { public abstract class TestResourceUtils {
/** /**
* Loads a {@link ClassPathResource} qualified by the simple name of clazz, * Load a {@link ClassPathResource} qualified by the simple name of clazz,
* and relative to the package for clazz. * and relative to the package for clazz.
*
* <p>Example: given a clazz 'com.foo.BarTests' and a resourceSuffix of 'context.xml', * <p>Example: given a clazz 'com.foo.BarTests' and a resourceSuffix of 'context.xml',
* this method will return a ClassPathResource representing com/foo/BarTests-context.xml * this method will return a ClassPathResource representing com/foo/BarTests-context.xml
*
* <p>Intended for use loading context configuration XML files within JUnit tests. * <p>Intended for use loading context configuration XML files within JUnit tests.
*
* @param clazz
* @param resourceSuffix
*/ */
public static ClassPathResource qualifiedResource(Class<?> clazz, String resourceSuffix) { public static ClassPathResource qualifiedResource(Class<?> clazz, String resourceSuffix) {
return new ClassPathResource(format("%s-%s", clazz.getSimpleName(), resourceSuffix), clazz); return new ClassPathResource(String.format("%s-%s", clazz.getSimpleName(), resourceSuffix), clazz);
} }
} }

11
spring-tx/src/main/java/org/springframework/transaction/interceptor/DefaultTransactionAttribute.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,10 +19,11 @@ package org.springframework.transaction.interceptor;
import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.DefaultTransactionDefinition;
/** /**
* Transaction attribute that takes the EJB approach to rolling * Spring's common transaction attribute implementation.
* back on runtime, but not checked, exceptions. * Rolls back on runtime, but not checked, exceptions by default.
* *
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller
* @since 16.03.2003 * @since 16.03.2003
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -57,7 +58,7 @@ public class DefaultTransactionAttribute extends DefaultTransactionDefinition im
} }
/** /**
* Create a new DefaultTransactionAttribute with the the given * Create a new DefaultTransactionAttribute with the given
* propagation behavior. Can be modified through bean property setters. * propagation behavior. Can be modified through bean property setters.
* @param propagationBehavior one of the propagation constants in the * @param propagationBehavior one of the propagation constants in the
* TransactionDefinition interface * TransactionDefinition interface
@ -74,6 +75,7 @@ public class DefaultTransactionAttribute extends DefaultTransactionDefinition im
* Associate a qualifier value with this transaction attribute. * Associate a qualifier value with this transaction attribute.
* <p>This may be used for choosing a corresponding transaction manager * <p>This may be used for choosing a corresponding transaction manager
* to process this specific transaction. * to process this specific transaction.
* @since 3.0
*/ */
public void setQualifier(String qualifier) { public void setQualifier(String qualifier) {
this.qualifier = qualifier; this.qualifier = qualifier;
@ -81,6 +83,7 @@ public class DefaultTransactionAttribute extends DefaultTransactionDefinition im
/** /**
* Return a qualifier value associated with this transaction attribute. * Return a qualifier value associated with this transaction attribute.
* @since 3.0
*/ */
public String getQualifier() { public String getQualifier() {
return this.qualifier; return this.qualifier;

Loading…
Cancel
Save