Polishing

(cherry picked from commit b5127dc)
This commit is contained in:
Juergen Hoeller
2016-07-20 22:16:35 +02:00
parent 9ec50a278e
commit 74b4dd0a40
17 changed files with 215 additions and 204 deletions

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@ import java.io.InputStream;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.lang.reflect.Proxy;
import org.springframework.util.ClassUtils;
@@ -68,7 +67,7 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
@Override
protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
try {
if (this.classLoader != null) {
// Use the specified ClassLoader to resolve local classes.
@@ -85,13 +84,13 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
}
@Override
protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
if (!this.acceptProxyClasses) {
throw new NotSerializableException("Not allowed to accept serialized proxy classes");
}
if (this.classLoader != null) {
// Use the specified ClassLoader to resolve local proxy classes.
Class[] resolvedInterfaces = new Class[interfaces.length];
Class<?>[] resolvedInterfaces = new Class<?>[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
try {
resolvedInterfaces[i] = ClassUtils.forName(interfaces[i], this.classLoader);
@@ -101,7 +100,7 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
}
}
try {
return Proxy.getProxyClass(this.classLoader, resolvedInterfaces);
return ClassUtils.createCompositeInterface(resolvedInterfaces, this.classLoader);
}
catch (IllegalArgumentException ex) {
throw new ClassNotFoundException(null, ex);
@@ -113,11 +112,11 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
return super.resolveProxyClass(interfaces);
}
catch (ClassNotFoundException ex) {
Class[] resolvedInterfaces = new Class[interfaces.length];
Class<?>[] resolvedInterfaces = new Class<?>[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
resolvedInterfaces[i] = resolveFallbackIfPossible(interfaces[i], ex);
}
return Proxy.getProxyClass(getFallbackClassLoader(), resolvedInterfaces);
return ClassUtils.createCompositeInterface(resolvedInterfaces, getFallbackClassLoader());
}
}
}
@@ -131,7 +130,7 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
* @param ex the original exception thrown when attempting to load the class
* @return the newly resolved class (never {@code null})
*/
protected Class resolveFallbackIfPossible(String className, ClassNotFoundException ex)
protected Class<?> resolveFallbackIfPossible(String className, ClassNotFoundException ex)
throws IOException, ClassNotFoundException{
throw ex;
@@ -139,8 +138,9 @@ public class ConfigurableObjectInputStream extends ObjectInputStream {
/**
* Return the fallback ClassLoader to use when no ClassLoader was specified
* and ObjectInputStream's own default ClassLoader failed.
* <p>The default implementation simply returns {@code null}.
* and ObjectInputStream's own default class loader failed.
* <p>The default implementation simply returns {@code null}, indicating
* that no specific fallback is available.
*/
protected ClassLoader getFallbackClassLoader() throws IOException {
return null;

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
}
/**
* This implementation returns a File reference for the underlying class path
* This implementation returns a File reference for the given URI-identified
* resource, provided that it refers to a file in the file system.
* @see org.springframework.util.ResourceUtils#getFile(java.net.URI, String)
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import org.springframework.util.StringUtils;
/**
* {@link Resource} implementation for {@code java.io.File} handles.
* Obviously supports resolution as File, and also as URL.
* Supports resolution as a {@code File} and also as a {@code URL}.
* Implements the extended {@link WritableResource} interface.
*
* @author Juergen Hoeller
@@ -85,7 +85,6 @@ public class FileSystemResource extends AbstractResource implements WritableReso
return this.path;
}
/**
* This implementation returns whether the underlying file exists.
* @see java.io.File#exists()

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -37,25 +37,25 @@ import java.net.URL;
* @see #getFile()
* @see WritableResource
* @see ContextResource
* @see FileSystemResource
* @see ClassPathResource
* @see UrlResource
* @see ClassPathResource
* @see FileSystemResource
* @see ByteArrayResource
* @see InputStreamResource
*/
public interface Resource extends InputStreamSource {
/**
* Return whether this resource actually exists in physical form.
* Determine whether this resource actually exists in physical form.
* <p>This method performs a definitive existence check, whereas the
* existence of a {@code Resource} handle only guarantees a
* valid descriptor handle.
* existence of a {@code Resource} handle only guarantees a valid
* descriptor handle.
*/
boolean exists();
/**
* Return whether the contents of this resource can be read,
* e.g. via {@link #getInputStream()} or {@link #getFile()}.
* Indicate whether the contents of this resource can be read via
* {@link #getInputStream()}.
* <p>Will be {@code true} for typical resource descriptors;
* note that actual content reading may still fail when attempted.
* However, a value of {@code false} is a definitive indication
@@ -65,8 +65,8 @@ public interface Resource extends InputStreamSource {
boolean isReadable();
/**
* Return whether this resource represents a handle with an open
* stream. If true, the InputStream cannot be read multiple times,
* Indicate whether this resource represents a handle with an open stream.
* If {@code true}, the InputStream cannot be read multiple times,
* and must be read and closed to avoid resource leaks.
* <p>Will be {@code false} for typical resource descriptors.
*/
@@ -83,6 +83,7 @@ public interface Resource extends InputStreamSource {
* Return a URI handle for this resource.
* @throws IOException if the resource cannot be resolved as URI,
* i.e. if the resource is not available as descriptor
* @since 2.5
*/
URI getURI() throws IOException;

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -32,8 +32,8 @@ import org.springframework.util.StringUtils;
/**
* {@link Resource} implementation for {@code java.net.URL} locators.
* Obviously supports resolution as URL, and also as File in case of
* the "file:" protocol.
* Supports resolution as a {@code URL} and also as a {@code File} in
* case of the {@code "file:"} protocol.
*
* @author Juergen Hoeller
* @since 28.12.2003
@@ -58,9 +58,10 @@ public class UrlResource extends AbstractFileResolvingResource {
/**
* Create a new UrlResource based on the given URI object.
* Create a new {@code UrlResource} based on the given URI object.
* @param uri a URI
* @throws MalformedURLException if the given URL path is not valid
* @since 2.5
*/
public UrlResource(URI uri) throws MalformedURLException {
Assert.notNull(uri, "URI must not be null");
@@ -70,7 +71,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* Create a new UrlResource based on the given URL object.
* Create a new {@code UrlResource} based on the given URL object.
* @param url a URL
*/
public UrlResource(URL url) {
@@ -81,7 +82,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* Create a new UrlResource based on a URL path.
* Create a new {@code UrlResource} based on a URL path.
* <p>Note: The given path needs to be pre-encoded if necessary.
* @param path a URL path
* @throws MalformedURLException if the given URL path is not valid
@@ -95,7 +96,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* Create a new UrlResource based on a URI specification.
* Create a new {@code UrlResource} based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
@@ -109,7 +110,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* Create a new UrlResource based on a URI specification.
* Create a new {@code UrlResource} based on a URI specification.
* <p>The given parts will automatically get encoded if necessary.
* @param protocol the URL protocol to use (e.g. "jar" or "file" - without colon);
* also known as "scheme"
@@ -133,6 +134,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
}
/**
* Determine a cleaned URL for the given original URL.
* @param originalUrl the original URL
@@ -151,10 +153,9 @@ public class UrlResource extends AbstractFileResolvingResource {
}
}
/**
* This implementation opens an InputStream for the given URL.
* It sets the "UseCaches" flag to {@code false},
* <p>It sets the {@code useCaches} flag to {@code false},
* mainly to avoid jar file locking on Windows.
* @see java.net.URL#openConnection()
* @see java.net.URLConnection#setUseCaches(boolean)
@@ -213,7 +214,7 @@ public class UrlResource extends AbstractFileResolvingResource {
}
/**
* This implementation creates a UrlResource, applying the given path
* This implementation creates a {@code UrlResource}, applying the given path
* relative to the path of the underlying URL of this resource descriptor.
* @see java.net.URL#URL(java.net.URL, String)
*/

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -55,13 +55,16 @@ public abstract class ClassUtils {
/** Prefix for internal non-primitive array class names: "[L" */
private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";
/** The package separator character '.' */
/** The package separator character: '.' */
private static final char PACKAGE_SEPARATOR = '.';
/** The inner class separator character '$' */
/** The path separator character: '/' */
private static final char PATH_SEPARATOR = '/';
/** The inner class separator character: '$' */
private static final char INNER_CLASS_SEPARATOR = '$';
/** The CGLIB class separator character "$$" */
/** The CGLIB class separator: "$$" */
public static final String CGLIB_CLASS_SEPARATOR = "$$";
/** The ".class" file suffix */
@@ -265,9 +268,10 @@ public abstract class ClassUtils {
return (clToUse != null ? clToUse.loadClass(name) : Class.forName(name));
}
catch (ClassNotFoundException ex) {
int lastDotIndex = name.lastIndexOf('.');
int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR);
if (lastDotIndex != -1) {
String innerClassName = name.substring(0, lastDotIndex) + '$' + name.substring(lastDotIndex + 1);
String innerClassName =
name.substring(0, lastDotIndex) + INNER_CLASS_SEPARATOR + name.substring(lastDotIndex + 1);
try {
return (clToUse != null ? clToUse.loadClass(innerClassName) : Class.forName(innerClassName));
}
@@ -455,8 +459,8 @@ public abstract class ClassUtils {
* @see java.beans.Introspector#decapitalize(String)
*/
public static String getShortNameAsProperty(Class<?> clazz) {
String shortName = ClassUtils.getShortName(clazz);
int dotIndex = shortName.lastIndexOf('.');
String shortName = getShortName(clazz);
int dotIndex = shortName.lastIndexOf(PACKAGE_SEPARATOR);
shortName = (dotIndex != -1 ? shortName.substring(dotIndex + 1) : shortName);
return Introspector.decapitalize(shortName);
}
@@ -525,7 +529,7 @@ public abstract class ClassUtils {
StringBuilder result = new StringBuilder();
while (clazz.isArray()) {
clazz = clazz.getComponentType();
result.append(ClassUtils.ARRAY_SUFFIX);
result.append(ARRAY_SUFFIX);
}
result.insert(0, clazz.getName());
return result.toString();
@@ -955,7 +959,7 @@ public abstract class ClassUtils {
*/
public static String convertResourcePathToClassName(String resourcePath) {
Assert.notNull(resourcePath, "Resource path must not be null");
return resourcePath.replace('/', '.');
return resourcePath.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR);
}
/**
@@ -965,7 +969,7 @@ public abstract class ClassUtils {
*/
public static String convertClassNameToResourcePath(String className) {
Assert.notNull(className, "Class name must not be null");
return className.replace('.', '/');
return className.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
}
/**
@@ -1011,12 +1015,12 @@ public abstract class ClassUtils {
return "";
}
String className = clazz.getName();
int packageEndIndex = className.lastIndexOf('.');
int packageEndIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
if (packageEndIndex == -1) {
return "";
}
String packageName = className.substring(0, packageEndIndex);
return packageName.replace('.', '/');
return packageName.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
}
/**
@@ -1165,7 +1169,6 @@ public abstract class ClassUtils {
*/
public static Class<?> createCompositeInterface(Class<?>[] interfaces, ClassLoader classLoader) {
Assert.notEmpty(interfaces, "Interfaces must not be empty");
Assert.notNull(classLoader, "ClassLoader must not be null");
return Proxy.getProxyClass(classLoader, interfaces);
}
@@ -1229,7 +1232,7 @@ public abstract class ClassUtils {
* @see org.springframework.aop.support.AopUtils#isCglibProxy(Object)
*/
public static boolean isCglibProxy(Object object) {
return ClassUtils.isCglibProxyClass(object.getClass());
return isCglibProxyClass(object.getClass());
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -126,8 +126,8 @@ public abstract class ResourceUtils {
URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
if (url == null) {
String description = "class path resource [" + path + "]";
throw new FileNotFoundException(
description + " cannot be resolved to URL because it does not exist");
throw new FileNotFoundException(description +
" cannot be resolved to URL because it does not exist");
}
return url;
}
@@ -166,9 +166,8 @@ public abstract class ResourceUtils {
ClassLoader cl = ClassUtils.getDefaultClassLoader();
URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
if (url == null) {
throw new FileNotFoundException(
description + " cannot be resolved to absolute file path " +
"because it does not reside in the file system");
throw new FileNotFoundException(description +
" cannot be resolved to absolute file path because it does not exist");
}
return getFile(url, description);
}
@@ -227,6 +226,7 @@ public abstract class ResourceUtils {
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
* @since 2.5
*/
public static File getFile(URI resourceUri) throws FileNotFoundException {
return getFile(resourceUri, "URI");
@@ -241,6 +241,7 @@ public abstract class ResourceUtils {
* @return a corresponding File object
* @throws FileNotFoundException if the URL cannot be resolved to
* a file in the file system
* @since 2.5
*/
public static File getFile(URI resourceUri, String description) throws FileNotFoundException {
Assert.notNull(resourceUri, "Resource URI must not be null");

View File

@@ -74,10 +74,10 @@ public class CollectionToCollectionConverterTests {
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
assertFalse(list.equals(result));
assertEquals(9, result.get(0));
assertEquals(37, result.get(1));
assertEquals(9, result.get(0).intValue());
assertEquals(37, result.get(1).intValue());
}
@Test
@@ -199,7 +199,7 @@ public class CollectionToCollectionConverterTests {
public void listToCollectionNoCopyRequired() throws NoSuchFieldException {
List<?> input = new ArrayList<String>(Arrays.asList("foo", "bar"));
assertSame(input, conversionService.convert(input, TypeDescriptor.forObject(input),
new TypeDescriptor(getClass().getField("wildCardCollection"))));
new TypeDescriptor(getClass().getField("wildcardCollection"))));
}
@Test
@@ -232,7 +232,7 @@ public class CollectionToCollectionConverterTests {
assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
@Test(expected=ConverterNotFoundException.class)
@Test(expected = ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
List<String> resources = new ArrayList<String>();
resources.add(null);
@@ -241,7 +241,7 @@ public class CollectionToCollectionConverterTests {
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
@Test(expected=ConversionFailedException.class)
@Test(expected = ConversionFailedException.class)
public void nothingInCommon() throws Exception {
List<Object> resources = new ArrayList<Object>();
resources.add(new ClassPathResource("test"));
@@ -261,9 +261,9 @@ public class CollectionToCollectionConverterTests {
public List<String> strings;
public List list = Collections.emptyList();
public List<?> list = Collections.emptyList();
public Collection<?> wildCardCollection = Collections.emptyList();
public Collection<?> wildcardCollection = Collections.emptyList();
public List<Resource> resources;