Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.beans.factory;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -16,18 +16,13 @@
|
||||
|
||||
package org.springframework.beans.factory;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.tests.TestResourceUtils.qualifiedResource;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
@@ -39,6 +34,8 @@ import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.tests.sample.beans.factory.DummyFactory;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.tests.TestResourceUtils.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
@@ -54,16 +51,16 @@ public final class BeanFactoryUtilsTests {
|
||||
private static final Resource LEAF_CONTEXT = qualifiedResource(CLASS, "leaf.xml");
|
||||
private static final Resource DEPENDENT_BEANS_CONTEXT = qualifiedResource(CLASS, "dependentBeans.xml");
|
||||
|
||||
private ConfigurableListableBeanFactory listableBeanFactory;
|
||||
private DefaultListableBeanFactory listableBeanFactory;
|
||||
|
||||
private DefaultListableBeanFactory dependentBeansFactory;
|
||||
|
||||
private ConfigurableListableBeanFactory dependentBeansBF;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Interesting hierarchical factory to test counts.
|
||||
// Slow to read so we cache it.
|
||||
|
||||
|
||||
DefaultListableBeanFactory grandParent = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(grandParent).loadBeanDefinitions(ROOT_CONTEXT);
|
||||
DefaultListableBeanFactory parent = new DefaultListableBeanFactory(grandParent);
|
||||
@@ -71,12 +68,13 @@ public final class BeanFactoryUtilsTests {
|
||||
DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
|
||||
new XmlBeanDefinitionReader(child).loadBeanDefinitions(LEAF_CONTEXT);
|
||||
|
||||
this.dependentBeansBF = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.dependentBeansBF).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
|
||||
dependentBeansBF.preInstantiateSingletons();
|
||||
this.dependentBeansFactory = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(this.dependentBeansFactory).loadBeanDefinitions(DEPENDENT_BEANS_CONTEXT);
|
||||
dependentBeansFactory.preInstantiateSingletons();
|
||||
this.listableBeanFactory = child;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
|
||||
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
|
||||
@@ -93,22 +91,21 @@ public final class BeanFactoryUtilsTests {
|
||||
// Leaf count
|
||||
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
|
||||
// Count minus duplicate
|
||||
assertTrue("Should count 7 beans, not "
|
||||
+ BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
|
||||
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
|
||||
assertTrue("Should count 7 beans, not " + BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory),
|
||||
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHierarchicalNamesWithNoMatch() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
NoOp.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, NoOp.class));
|
||||
assertEquals(0, names.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
IndexedTestBean.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, IndexedTestBean.class));
|
||||
assertEquals(1, names.size());
|
||||
assertTrue(names.contains("indexedBean"));
|
||||
// Distinguish from default ListableBeanFactory behavior
|
||||
@@ -117,8 +114,8 @@ public final class BeanFactoryUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testGetBeanNamesForTypeWithOverride() throws Exception {
|
||||
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
|
||||
ITestBean.class));
|
||||
List<String> names = Arrays.asList(
|
||||
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class));
|
||||
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
|
||||
assertEquals(4, names.size());
|
||||
assertTrue(names.contains("test"));
|
||||
@@ -131,7 +128,7 @@ public final class BeanFactoryUtilsTests {
|
||||
public void testNoBeansOfType() {
|
||||
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
|
||||
lbf.addBean("foo", new Object());
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
|
||||
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
|
||||
assertTrue(beans.isEmpty());
|
||||
}
|
||||
|
||||
@@ -148,7 +145,7 @@ public final class BeanFactoryUtilsTests {
|
||||
lbf.addBean("t3", t3);
|
||||
lbf.addBean("t4", t4);
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
|
||||
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
|
||||
assertEquals(2, beans.size());
|
||||
assertEquals(t1, beans.get("t1"));
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
@@ -192,8 +189,8 @@ public final class BeanFactoryUtilsTests {
|
||||
this.listableBeanFactory.registerSingleton("t3", t3);
|
||||
this.listableBeanFactory.registerSingleton("t4", t4);
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
|
||||
false);
|
||||
Map<String, ?> beans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
|
||||
assertEquals(6, beans.size());
|
||||
assertEquals(test3, beans.get("test3"));
|
||||
assertEquals(test, beans.get("test"));
|
||||
@@ -201,12 +198,9 @@ public final class BeanFactoryUtilsTests {
|
||||
assertEquals(t2, beans.get("t2"));
|
||||
assertEquals(t3.getObject(), beans.get("t3"));
|
||||
assertTrue(beans.get("t4") instanceof TestBean);
|
||||
// t3 and t4 are found here as of Spring 2.0, since they are
|
||||
// pre-registered
|
||||
// singleton instances, while testFactory1 and testFactory are *not*
|
||||
// found
|
||||
// because they are FactoryBean definitions that haven't been
|
||||
// initialized yet.
|
||||
// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
|
||||
// singleton instances, while testFactory1 and testFactory are *not* found
|
||||
// because they are FactoryBean definitions that haven't been initialized yet.
|
||||
|
||||
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
|
||||
Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
|
||||
@@ -248,8 +242,8 @@ public final class BeanFactoryUtilsTests {
|
||||
Object test3 = this.listableBeanFactory.getBean("test3");
|
||||
Object test = this.listableBeanFactory.getBean("test");
|
||||
|
||||
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
|
||||
false);
|
||||
Map<String, ?> beans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
|
||||
assertEquals(2, beans.size());
|
||||
assertEquals(test3, beans.get("test3"));
|
||||
assertEquals(test, beans.get("test"));
|
||||
@@ -284,25 +278,25 @@ public final class BeanFactoryUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testADependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("a");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("a");
|
||||
assertTrue(ObjectUtils.isEmpty(deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("b");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("b");
|
||||
assertTrue(Arrays.equals(new String[] { "c" }, deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("c");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("c");
|
||||
assertTrue(Arrays.equals(new String[] { "int", "long" }, deps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntDependencies() {
|
||||
String[] deps = this.dependentBeansBF.getDependentBeans("int");
|
||||
String[] deps = this.dependentBeansFactory.getDependentBeans("int");
|
||||
assertTrue(Arrays.equals(new String[] { "buffer" }, deps));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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,7 +32,6 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.security.auth.Subject;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -41,6 +40,7 @@ import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
@@ -190,7 +190,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
|
||||
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
Properties p = new Properties();
|
||||
p.setProperty("x1.(class)", DummyFactory.class.getName());
|
||||
@@ -1212,8 +1212,8 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
catch (UnsatisfiedDependencyException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getMessage().indexOf("rod") != -1);
|
||||
assertTrue(ex.getMessage().indexOf("rod2") != -1);
|
||||
assertTrue(ex.getMessage().contains("rod"));
|
||||
assertTrue(ex.getMessage().contains("rod2"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1283,13 +1283,13 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertNull(bean.getSpouse());
|
||||
}
|
||||
|
||||
@Test(expected=NoSuchBeanDefinitionException.class)
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void testGetBeanByTypeWithNoneFound() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
lbf.getBean(TestBean.class);
|
||||
}
|
||||
|
||||
@Test(expected=NoUniqueBeanDefinitionException.class)
|
||||
@Test(expected = NoUniqueBeanDefinitionException.class)
|
||||
public void testGetBeanByTypeWithAmbiguity() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
|
||||
@@ -1393,7 +1393,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
* Java method names. In other words, you can't name a method
|
||||
* {@code set&FactoryBean(...)}.
|
||||
*/
|
||||
@Test(expected=TypeMismatchException.class)
|
||||
@Test(expected = TypeMismatchException.class)
|
||||
public void testAutowireBeanWithFactoryBeanByName() {
|
||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||
RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class);
|
||||
@@ -1416,8 +1416,8 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
catch (UnsatisfiedDependencyException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getMessage().indexOf("test") != -1);
|
||||
assertTrue(ex.getMessage().indexOf("spouse") != -1);
|
||||
assertTrue(ex.getMessage().contains("test"));
|
||||
assertTrue(ex.getMessage().contains("spouse"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1434,8 +1434,8 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
catch (UnsatisfiedDependencyException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getMessage().indexOf("test") != -1);
|
||||
assertTrue(ex.getMessage().indexOf("spouse") != -1);
|
||||
assertTrue(ex.getMessage().contains("test"));
|
||||
assertTrue(ex.getMessage().contains("spouse"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1683,7 +1683,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertEquals("test", ex.getBeanName());
|
||||
assertTrue(ex.getMessage().toLowerCase().indexOf("interface") != -1);
|
||||
assertTrue(ex.getMessage().toLowerCase().contains("interface"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1697,7 +1697,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
assertEquals("test", ex.getBeanName());
|
||||
assertTrue(ex.getMessage().toLowerCase().indexOf("abstract") != -1);
|
||||
assertTrue(ex.getMessage().toLowerCase().contains("abstract"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2144,13 +2144,13 @@ public class DefaultListableBeanFactoryTests {
|
||||
assertEquals(expectedNameFromArgs, tb2.getName());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
|
||||
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
|
||||
beanDefinition.setScope("he put himself so low could hardly look me in the face");
|
||||
|
||||
final DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
factory.registerBeanDefinition("testBean", beanDefinition);
|
||||
factory.getBean("testBean");
|
||||
}
|
||||
@@ -2162,8 +2162,7 @@ public class DefaultListableBeanFactoryTests {
|
||||
RootBeanDefinition parent = new RootBeanDefinition();
|
||||
parent.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
||||
|
||||
AbstractBeanDefinition child = BeanDefinitionBuilder
|
||||
.childBeanDefinition("parent").getBeanDefinition();
|
||||
AbstractBeanDefinition child = BeanDefinitionBuilder.childBeanDefinition("parent").getBeanDefinition();
|
||||
child.setBeanClass(TestBean.class);
|
||||
child.setScope(theChildScope);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -439,8 +439,8 @@ public abstract class ReflectionUtils {
|
||||
* @see java.lang.reflect.Method#setAccessible
|
||||
*/
|
||||
public static void makeAccessible(Method method) {
|
||||
if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) &&
|
||||
!method.isAccessible()) {
|
||||
if ((!Modifier.isPublic(method.getModifiers()) ||
|
||||
!Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
|
||||
method.setAccessible(true);
|
||||
}
|
||||
}
|
||||
@@ -454,8 +454,8 @@ public abstract class ReflectionUtils {
|
||||
* @see java.lang.reflect.Constructor#setAccessible
|
||||
*/
|
||||
public static void makeAccessible(Constructor<?> ctor) {
|
||||
if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) &&
|
||||
!ctor.isAccessible()) {
|
||||
if ((!Modifier.isPublic(ctor.getModifiers()) ||
|
||||
!Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
|
||||
ctor.setAccessible(true);
|
||||
}
|
||||
}
|
||||
@@ -465,11 +465,11 @@ public abstract class ReflectionUtils {
|
||||
* class and superclasses.
|
||||
* <p>The same named method occurring on subclass and superclass will appear
|
||||
* twice, unless excluded by a {@link MethodFilter}.
|
||||
* @param clazz class to start looking at
|
||||
* @param clazz the class to introspect
|
||||
* @param mc the callback to invoke for each method
|
||||
* @see #doWithMethods(Class, MethodCallback, MethodFilter)
|
||||
*/
|
||||
public static void doWithMethods(Class<?> clazz, MethodCallback mc) throws IllegalArgumentException {
|
||||
public static void doWithMethods(Class<?> clazz, MethodCallback mc) {
|
||||
doWithMethods(clazz, mc, null);
|
||||
}
|
||||
|
||||
@@ -478,13 +478,11 @@ public abstract class ReflectionUtils {
|
||||
* class and superclasses (or given interface and super-interfaces).
|
||||
* <p>The same named method occurring on subclass and superclass will appear
|
||||
* twice, unless excluded by the specified {@link MethodFilter}.
|
||||
* @param clazz class to start looking at
|
||||
* @param clazz the class to introspect
|
||||
* @param mc the callback to invoke for each method
|
||||
* @param mf the filter that determines the methods to apply the callback to
|
||||
*/
|
||||
public static void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
public static void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf) {
|
||||
// Keep backing up the inheritance hierarchy.
|
||||
Method[] methods = getDeclaredMethods(clazz);
|
||||
for (Method method : methods) {
|
||||
@@ -495,7 +493,7 @@ public abstract class ReflectionUtils {
|
||||
mc.doWith(method);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new IllegalStateException("Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
|
||||
throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
|
||||
}
|
||||
}
|
||||
if (clazz.getSuperclass() != null) {
|
||||
@@ -509,10 +507,11 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all declared methods on the leaf class and all superclasses. Leaf
|
||||
* class methods are included first.
|
||||
* Get all declared methods on the leaf class and all superclasses.
|
||||
* Leaf class methods are included first.
|
||||
* @param leafClass the class to introspect
|
||||
*/
|
||||
public static Method[] getAllDeclaredMethods(Class<?> leafClass) throws IllegalArgumentException {
|
||||
public static Method[] getAllDeclaredMethods(Class<?> leafClass) {
|
||||
final List<Method> methods = new ArrayList<Method>(32);
|
||||
doWithMethods(leafClass, new MethodCallback() {
|
||||
public void doWith(Method method) {
|
||||
@@ -523,11 +522,12 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unique set of declared methods on the leaf class and all superclasses. Leaf
|
||||
* class methods are included first and while traversing the superclass hierarchy any methods found
|
||||
* with signatures matching a method already included are filtered out.
|
||||
* Get the unique set of declared methods on the leaf class and all superclasses.
|
||||
* Leaf class methods are included first and while traversing the superclass hierarchy
|
||||
* any methods found with signatures matching a method already included are filtered out.
|
||||
* @param leafClass the class to introspect
|
||||
*/
|
||||
public static Method[] getUniqueDeclaredMethods(Class<?> leafClass) throws IllegalArgumentException {
|
||||
public static Method[] getUniqueDeclaredMethods(Class<?> leafClass) {
|
||||
final List<Method> methods = new ArrayList<Method>(32);
|
||||
doWithMethods(leafClass, new MethodCallback() {
|
||||
public void doWith(Method method) {
|
||||
@@ -559,7 +559,7 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method retrieves {@link Class#getDeclaredMethods()} from a local cache
|
||||
* This variant retrieves {@link Class#getDeclaredMethods()} from a local cache
|
||||
* in order to avoid the JVM's SecurityManager check and defensive array copying.
|
||||
*/
|
||||
private static Method[] getDeclaredMethods(Class<?> clazz) {
|
||||
@@ -577,7 +577,7 @@ public abstract class ReflectionUtils {
|
||||
* @param clazz the target class to analyze
|
||||
* @param fc the callback to invoke for each field
|
||||
*/
|
||||
public static void doWithFields(Class<?> clazz, FieldCallback fc) throws IllegalArgumentException {
|
||||
public static void doWithFields(Class<?> clazz, FieldCallback fc) {
|
||||
doWithFields(clazz, fc, null);
|
||||
}
|
||||
|
||||
@@ -588,15 +588,12 @@ public abstract class ReflectionUtils {
|
||||
* @param fc the callback to invoke for each field
|
||||
* @param ff the filter that determines the fields to apply the callback to
|
||||
*/
|
||||
public static void doWithFields(Class<?> clazz, FieldCallback fc, FieldFilter ff)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
public static void doWithFields(Class<?> clazz, FieldCallback fc, FieldFilter ff) {
|
||||
// Keep backing up the inheritance hierarchy.
|
||||
Class<?> targetClass = clazz;
|
||||
do {
|
||||
Field[] fields = targetClass.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
// Skip static and final fields.
|
||||
if (ff != null && !ff.matches(field)) {
|
||||
continue;
|
||||
}
|
||||
@@ -604,7 +601,7 @@ public abstract class ReflectionUtils {
|
||||
fc.doWith(field);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new IllegalStateException("Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
|
||||
throw new IllegalStateException("Not allowed to access field '" + field.getName() + "': " + ex);
|
||||
}
|
||||
}
|
||||
targetClass = targetClass.getSuperclass();
|
||||
@@ -616,9 +613,8 @@ public abstract class ReflectionUtils {
|
||||
* Given the source object and the destination, which must be the same class
|
||||
* or a subclass, copy all fields, including inherited fields. Designed to
|
||||
* work on objects with public no-arg constructors.
|
||||
* @throws IllegalArgumentException if the arguments are incompatible
|
||||
*/
|
||||
public static void shallowCopyFieldState(final Object src, final Object dest) throws IllegalArgumentException {
|
||||
public static void shallowCopyFieldState(final Object src, final Object dest) {
|
||||
if (src == null) {
|
||||
throw new IllegalArgumentException("Source for field copy cannot be null");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -25,7 +25,6 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -33,7 +32,7 @@ import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
import org.springframework.tests.sample.objects.TestObject;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -67,8 +66,8 @@ public class ReflectionUtilsTests {
|
||||
|
||||
@Test
|
||||
public void setField() {
|
||||
final TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
|
||||
Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
|
||||
@@ -80,13 +79,6 @@ public class ReflectionUtilsTests {
|
||||
assertNull(testBean.getName());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setFieldIllegal() {
|
||||
final TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
|
||||
final Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
|
||||
ReflectionUtils.setField(field, testBean, "FooBar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeMethod() throws Exception {
|
||||
String rob = "Rob Harrop";
|
||||
@@ -95,65 +87,50 @@ public class ReflectionUtilsTests {
|
||||
bean.setName(rob);
|
||||
|
||||
Method getName = TestObject.class.getMethod("getName", (Class[]) null);
|
||||
Method setName = TestObject.class.getMethod("setName", new Class[] { String.class });
|
||||
Method setName = TestObject.class.getMethod("setName", String.class);
|
||||
|
||||
Object name = ReflectionUtils.invokeMethod(getName, bean);
|
||||
assertEquals("Incorrect name returned", rob, name);
|
||||
|
||||
String juergen = "Juergen Hoeller";
|
||||
ReflectionUtils.invokeMethod(setName, bean, new Object[] { juergen });
|
||||
ReflectionUtils.invokeMethod(setName, bean, juergen);
|
||||
assertEquals("Incorrect name set", juergen, bean.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void declaresException() throws Exception {
|
||||
Method remoteExMethod = A.class.getDeclaredMethod("foo", new Class[] { Integer.class });
|
||||
Method remoteExMethod = A.class.getDeclaredMethod("foo", Integer.class);
|
||||
assertTrue(ReflectionUtils.declaresException(remoteExMethod, RemoteException.class));
|
||||
assertTrue(ReflectionUtils.declaresException(remoteExMethod, ConnectException.class));
|
||||
assertFalse(ReflectionUtils.declaresException(remoteExMethod, NoSuchMethodException.class));
|
||||
assertFalse(ReflectionUtils.declaresException(remoteExMethod, Exception.class));
|
||||
|
||||
Method illegalExMethod = B.class.getDeclaredMethod("bar", new Class[] { String.class });
|
||||
Method illegalExMethod = B.class.getDeclaredMethod("bar", String.class);
|
||||
assertTrue(ReflectionUtils.declaresException(illegalExMethod, IllegalArgumentException.class));
|
||||
assertTrue(ReflectionUtils.declaresException(illegalExMethod, NumberFormatException.class));
|
||||
assertFalse(ReflectionUtils.declaresException(illegalExMethod, IllegalStateException.class));
|
||||
assertFalse(ReflectionUtils.declaresException(illegalExMethod, Exception.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void copySrcToDestinationOfIncorrectClass() {
|
||||
TestObject src = new TestObject();
|
||||
String dest = new String();
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Ok
|
||||
}
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullSrc() {
|
||||
TestObject src = null;
|
||||
String dest = new String();
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Ok
|
||||
}
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullDest() {
|
||||
TestObject src = new TestObject();
|
||||
String dest = null;
|
||||
try {
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Ok
|
||||
}
|
||||
ReflectionUtils.shallowCopyFieldState(src, dest);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -19,7 +19,7 @@ package org.springframework.http.converter;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* Thrown by {@link HttpMessageConverter} implementations when the conversion fails.
|
||||
* Thrown by {@link HttpMessageConverter} implementations when a conversion attempt fails.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
@@ -29,7 +29,6 @@ public class HttpMessageConversionException extends NestedRuntimeException {
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageConversionException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public HttpMessageConversionException(String msg) {
|
||||
@@ -38,7 +37,6 @@ public class HttpMessageConversionException extends NestedRuntimeException {
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageConversionException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause (if any)
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -18,7 +18,7 @@ package org.springframework.http.converter;
|
||||
|
||||
/**
|
||||
* Thrown by {@link HttpMessageConverter} implementations when the
|
||||
* {@link HttpMessageConverter#read(Class, org.springframework.http.HttpInputMessage) read} method fails.
|
||||
* {@link HttpMessageConverter#read} method fails.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
@@ -28,7 +28,6 @@ public class HttpMessageNotReadableException extends HttpMessageConversionExcept
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageNotReadableException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public HttpMessageNotReadableException(String msg) {
|
||||
@@ -37,11 +36,11 @@ public class HttpMessageNotReadableException extends HttpMessageConversionExcept
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageNotReadableException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause (if any)
|
||||
*/
|
||||
public HttpMessageNotReadableException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -17,9 +17,8 @@
|
||||
package org.springframework.http.converter;
|
||||
|
||||
/**
|
||||
* Thrown by {@link org.springframework.http.converter.HttpMessageConverter} implementations when the
|
||||
* {@link org.springframework.http.converter.HttpMessageConverter#write(Object, org.springframework.http.MediaType,
|
||||
* org.springframework.http.HttpOutputMessage) write} method fails.
|
||||
* Thrown by {@link HttpMessageConverter} implementations when the
|
||||
* {@link HttpMessageConverter#write} method fails.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
@@ -29,7 +28,6 @@ public class HttpMessageNotWritableException extends HttpMessageConversionExcept
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageNotWritableException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
*/
|
||||
public HttpMessageNotWritableException(String msg) {
|
||||
@@ -38,11 +36,11 @@ public class HttpMessageNotWritableException extends HttpMessageConversionExcept
|
||||
|
||||
/**
|
||||
* Create a new HttpMessageNotWritableException.
|
||||
*
|
||||
* @param msg the detail message
|
||||
* @param cause the root cause (if any)
|
||||
*/
|
||||
public HttpMessageNotWritableException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -51,7 +51,7 @@ import org.springframework.web.servlet.view.AbstractView;
|
||||
* </ul>
|
||||
*
|
||||
* <p>For working with the workbook in the subclass, see
|
||||
* <a href="http://jakarta.apache.org/poi/index.html">Jakarta's POI site</a>
|
||||
* <a href="http://poi.apache.org">Apache's POI site</a>
|
||||
*
|
||||
* <p>As an example, you can try this snippet:
|
||||
*
|
||||
@@ -194,7 +194,7 @@ public abstract class AbstractExcelView extends AbstractView {
|
||||
* <p>Creates the row and the cell if they still doesn't already exist.
|
||||
* Thus, the column can be passed as an int, the method making the needed downcasts.
|
||||
* @param sheet a sheet object. The first sheet is usually obtained by workbook.getSheetAt(0)
|
||||
* @param row thr row number
|
||||
* @param row the row number
|
||||
* @param col the column number
|
||||
* @return the HSSFCell
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -27,26 +27,26 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Jasper Reports view class that allows for the actual rendering format to be
|
||||
* specified at runtime using a parameter contained in the model.
|
||||
* JasperReports view class that allows for the actual rendering format
|
||||
* to be specified at runtime using a parameter contained in the model.
|
||||
*
|
||||
* <p>This view works on the concept of a format key and a mapping key.
|
||||
* The format key is used to pass the mapping key from your
|
||||
* {@code Controller} to Spring through as part of the model and the
|
||||
* mapping key is used to map a logical format to an actual JasperReports
|
||||
* view class. For example you might add the following code to your
|
||||
* {@code Controller}:
|
||||
* The format key is used to pass the mapping key from your {@code Controller}
|
||||
* to Spring through as part of the model and the mapping key is used to map
|
||||
* a logical format to an actual JasperReports view class.
|
||||
*
|
||||
* <pre>
|
||||
* <p>For example, you might add the following code to your {@code Controller}:
|
||||
*
|
||||
* <pre class="code">
|
||||
* Map<String, Object> model = new HashMap<String, Object>();
|
||||
* model.put("format", "pdf");</pre>
|
||||
*
|
||||
* Here {@code format} is the format key and {@code pdf} is
|
||||
* the mapping key. When rendering a report, this class looks for a
|
||||
* model parameter under the format key, which by default is
|
||||
* {@code format}. It then uses the value of this parameter to lookup
|
||||
* the actual {@code View} class to use. The default mappings for this
|
||||
* lookup are:
|
||||
* Here {@code format} is the format key and {@code pdf} is the mapping key.
|
||||
* When rendering a report, this class looks for a model parameter under the
|
||||
* format key, which by default is {@code format}. It then uses the value of
|
||||
* this parameter to lookup the actual {@code View} class to use.
|
||||
*
|
||||
* <p>The default mappings for the format lookup are:
|
||||
*
|
||||
* <p><ul>
|
||||
* <li>{@code csv} - {@code JasperReportsCsvView}</li>
|
||||
@@ -55,8 +55,8 @@ import org.springframework.util.CollectionUtils;
|
||||
* <li>{@code xls} - {@code JasperReportsXlsView}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>The format key can be changed using the {@code formatKey}
|
||||
* property and the mapping key to view class mappings can be changed using the
|
||||
* <p>The format key can be changed using the {@code formatKey} property.
|
||||
* The applicable key-to-view-class mappings can be configured using the
|
||||
* {@code formatMappings} property.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
@@ -102,6 +102,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
|
||||
this.formatMappings.put("xls", JasperReportsXlsView.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the key of the model parameter that holds the format discriminator.
|
||||
* Default is "format".
|
||||
|
||||
Reference in New Issue
Block a user