alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (ReflectionTestUtils.java)

This example Spring Framework source code file (ReflectionTestUtils.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

class, class, could, exception, illegalargumentexception, invoking, method, object, object, reflection, reflectiontestutils, string, string, the, the

The Spring Framework ReflectionTestUtils.java source code

/*
 * Copyright 2007 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.test.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 * ReflectionTestUtils is a collection of reflection-based utility methods for
 * use in unit and integration testing scenarios.
 * </p>
 * <p>
 * There are often situations in which it would be beneficial to be able to set
 * a non-<code>public field or invoke a non-public
 * setter method when testing code involving, for example:
 * </p>
 * <ul>
 * <li>ORM frameworks such as JPA and Hibernate which condone the usage of
 * <code>private or protected field access as opposed to
 * <code>public setter methods for properties in a domain entity.
 * <li>Spring's support for annotations such as
 * {@link org.springframework.beans.factory.annotation.Autowired @Autowired} and
 * {@link javax.annotation.Resource @Resource} which provides dependency
 * injection for <code>private or protected fields,
 * setter methods, and configuration methods.</li>
 * </ul>
 *
 * @author Sam Brannen
 * @since 2.5
 * @see ReflectionUtils
 */
public class ReflectionTestUtils {

	/** Class Logger. */
	private static final Log logger = LogFactory.getLog(ReflectionTestUtils.class);


	/**
	 * <p>
	 * Sets the {@link Field field} with the given <code>name on the
	 * provided {@link Object target object} to the supplied <code>value.
	 * </p>
	 * <p>
	 * This method traverses the class hierarchy in search of the desired field.
	 * In addition, an attempt will be made to make non-<code>public
	 * fields <em>accessible, thus allowing one to set
	 * <code>protected, private, and
	 * <em>package-private fields.
	 * </p>
	 *
	 * @param target The target object on which to set the field.
	 * @param name The name of the field to set.
	 * @param value The value to set; may be <code>null unless the
	 *        field type is a primitive type.
	 * @param type The type of the field.
	 * @throws IllegalArgumentException if the target object or field type is
	 *         <code>null; if the field name is empty; or
	 *         if the field could not be found on the target object.
	 * @throws Exception Allows all other exceptions to propagate.
	 * @see ReflectionUtils#findField(Class, String, Class)
	 * @see ReflectionUtils#makeAccessible(Field)
	 * @see ReflectionUtils#setField(Field, Object, Object)
	 */
	public static final void setField(final Object target, final String name, final Object value, final Class type)
			throws Exception {

		Assert.notNull(target, "The target object supplied to setField() can not be null.");
		Assert.hasText(name, "The field name supplied to setField() can not be empty.");
		Assert.notNull(type, "The field type supplied to setField() can not be null.");

		if (logger.isDebugEnabled()) {
			logger.debug("Setting field [" + name + "] on target [" + target + "] with value [" + value
					+ "] and target type [" + type + "].");
		}

		final Field field = ReflectionUtils.findField(target.getClass(), name, type);
		if (field == null) {
			throw new IllegalArgumentException("Could not find field [" + name + "] on target [" + target
					+ "] with type [" + type + "].");
		}

		ReflectionUtils.makeAccessible(field);
		ReflectionUtils.setField(field, target, value);
	}

	/**
	 * <p>
	 * Invokes the {@link Method setter method} with the given <code>name
	 * on the supplied {@link Object target object} with the supplied
	 * <code>value.
	 * </p>
	 * <p>
	 * This method traverses the class hierarchy in search of the desired
	 * method. In addition, an attempt will be made to make non-<code>public
	 * methods <em>accessible, thus allowing one to invoke
	 * <code>protected, private, and
	 * <em>package-private setter methods.
	 * </p>
	 * <p>
	 * In addition, this method supports JavaBean-style <em>property
	 * names. For example, if you wish to set the <code>name property
	 * on the target object, you may pass either "name" or
	 * "setName" as the method name.
	 * </p>
	 *
	 * @param target The target object on which to invoke the specified setter
	 *        method.
	 * @param name The name of the setter method to invoke or the corresponding
	 *        property name.
	 * @param value The value to provide to the setter method; may be
	 *        <code>null unless the parameter type is a primitive type.
	 * @param type The formal parameter type declared by the setter method.
	 * @throws IllegalArgumentException if the target object or parameter type
	 *         is <code>null; if the name is empty; or if
	 *         the method could not be found on the target object.
	 * @throws Exception Allows all other exceptions to propagate.
	 * @see ReflectionUtils#findMethod(Class, String, Class[])
	 * @see ReflectionUtils#makeAccessible(Method)
	 * @see ReflectionUtils#invokeMethod(Method, Object, Object[])
	 */
	public static final void invokeSetterMethod(final Object target, final String name, final Object value,
			final Class type) throws Exception {

		Assert.notNull(target, "The target object supplied to invokeSetterMethod() can not be null.");
		Assert.hasText(name, "The method name supplied to invokeSetterMethod() can not be empty.");
		Assert.notNull(type, "The parameter type supplied to invokeSetterMethod() can not be null.");

		String setterMethodName = name;
		if (!setterMethodName.startsWith("set")) {
			setterMethodName = "set" + StringUtils.capitalize(setterMethodName);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Invoking setter method [" + setterMethodName + "] on target [" + target + "] with value ["
					+ value + "] and parameter type [" + type + "].");
		}

		Method method = ReflectionUtils.findMethod(target.getClass(), setterMethodName, new Class[] { type });
		if (method == null) {
			throw new IllegalArgumentException("Could not find setter method [" + setterMethodName + "] on target ["
					+ target + "] with parameter type [" + type + "].");
		}

		ReflectionUtils.makeAccessible(method);
		ReflectionUtils.invokeMethod(method, target, new Object[] { value });
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework ReflectionTestUtils.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.