All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.framework.test.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * Document confidentiel - Diffusion interdite 
 */

package com.evasion.framework.test;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * Reflection utils.
 * 
 */
public final class ReflectionUtils {

	private static final String GETTER_PREFIX = "get";
	private static final String BOOLEAN_GETTER_PREFIX = "is";
	private static final String SETTER_PREFIX = "set";

	/**
	 * Private constructor to prevent instantiation.
	 */
	private ReflectionUtils() {
		// util class, prevent instantiation.
	}

	/**
	 * Returns all getter methods found in this bean (including non-public
	 * getters).
	 * 
	 * @param bean
	 *            the bean in which getters will be searched
	 * @return a list of getter methods
	 */
	public static final List findAllGetters(final Object bean) {
		return ReflectionUtils.findAllGetters(bean, true);
	}

	/**
	 * Returns all getter methods found in this bean.
	 * 
	 * @param bean
	 *            the bean in which getters will be searched
	 * 
	 * @param includeNonPublicGetters
	 *            if true the result will include non-public
	 *            methods.
	 * @return a list of methods
	 */
	public static final List findAllGetters(final Object bean,
			boolean includeNonPublicGetters) {
		final List result = new ArrayList();

		List methods;

		methods = findAllMethods(bean, includeNonPublicGetters);

		for (Method method : methods) {
			if (ReflectionUtils.isGetterMethod(method)) {
				result.add(method);
			}
		}

		return result;
	}

	/**
	 * Returns all getter methods found in this bean and his superclass.
	 * 
	 * @param bean
	 *            the bean in which methods will be searched
	 * 
	 * @param includeNonPublicMethods
	 *            if true the result will include non-public
	 *            methods.
	 * @return a list of methods
	 */
	private static final List findAllMethods(final Object bean,
			boolean includeNonPublicMethods) {
		List result = new ArrayList(Arrays.asList(bean
				.getClass().getMethods()));

		if (includeNonPublicMethods) {
			Method[] beanMethods = bean.getClass().getDeclaredMethods();
			result.addAll(Arrays.asList(beanMethods));
		}

		return result;
	}

	/**
	 * Returns true if the given method is a getter.
	 * 
	 * Note: a method is a getter if
	 * 
    *
  • its name starts with 'get' or 'is' and is followed by a uppercase * letter
  • *
  • it takes no argument
  • *
  • its return type is not void
  • *
* * @param method * the method to test * @return true if the method is a getter, false * otherwise. */ public static boolean isGetterMethod(final Method method) { boolean result = true; final String methodName = method.getName(); if (!methodName.startsWith(GETTER_PREFIX) && !methodName.startsWith(BOOLEAN_GETTER_PREFIX)) { // a getter method must starts with 'get' or 'is' result = false; } String propertyName = null; if (methodName.startsWith(GETTER_PREFIX)) { propertyName = methodName.substring(GETTER_PREFIX.length()); } else if (methodName.startsWith(BOOLEAN_GETTER_PREFIX)) { propertyName = methodName.substring(BOOLEAN_GETTER_PREFIX.length()); } if (StringUtils.isBlank(propertyName) || !propertyName.equals(StringUtils.capitalize(propertyName))) { // the first character after the prefix must be in upper case. result = false; } if (method.getReturnType() == null || method.getReturnType() == void.class) { // a getter method cannot return void result = false; } if (method.getParameterTypes().length > 0) { // a getter method takes no argument result = false; } return result; } /** * Returns the getter method for the property, or null if not * found. * * @param propertyName * the property name * @return the getter method for the property, or null if not * found. */ public static Method findGetter(final Object bean, final String propertyName) { Method getter = null; final String methodNameSuffix = StringUtils.capitalize(propertyName); final String getterName = new StringBuffer().append(GETTER_PREFIX) .append(methodNameSuffix).toString(); final String booleanGetterName = new StringBuffer().append( BOOLEAN_GETTER_PREFIX).append(methodNameSuffix).toString(); for (Method method : bean.getClass().getMethods()) { if ((getterName.equals(method.getName()) || booleanGetterName .equals(method.getName())) && isGetterMethod(method)) { getter = method; break; } } if (getter == null) { for (Method method : bean.getClass().getDeclaredMethods()) { if ((getterName.equals(method.getName()) || booleanGetterName .equals(method.getName())) && isGetterMethod(method)) { getter = method; break; } } } return getter; } /** * Returns the setter method for the property, or null if not * found. * * @param propertyName * the property name * @return the setter method for the property, or null if not * found. */ public static Method findSetter(final Object bean, final String propertyName) { Method setter = null; final String methodNameSuffix = StringUtils.capitalize(propertyName); final String setterName = new StringBuffer().append(SETTER_PREFIX) .append(methodNameSuffix).toString(); for (Method method : bean.getClass().getMethods()) { if (setterName.equals(method.getName()) && isSetterMethod(method)) { setter = method; break; } } if (setter == null) { for (Method method : bean.getClass().getDeclaredMethods()) { if (setterName.equals(method.getName()) && isSetterMethod(method)) { setter = method; break; } } } return setter; } /** * D�termine si une m�thode est un Setter : -commence par "set" -le premier * caract�re apr�s le suffixe est en majuscule -return void -n'a qu'un seul * argument * * @param method * la methode � tester * @return true si la methode est un setter, false sinon */ private static boolean isSetterMethod(final Method method) { boolean result = true; final String methodName = method.getName(); if (!methodName.startsWith(SETTER_PREFIX)) { // a setter method must starts with 'set' result = false; } String propertyName = methodName.substring(SETTER_PREFIX.length()); if (StringUtils.isBlank(propertyName) || !propertyName.equals(StringUtils.capitalize(propertyName))) { // the first character after the prefix must be in upper case. result = false; } if (!Void.TYPE.equals(method.getReturnType())) { // a setter method must return void result = false; } if (method.getParameterTypes().length != 1) { // a getter method takes one argument result = false; } return result; } /** * Returns the type of the property (i.e. the return type of the getter * method. * * @param propertyName * the name of the property * @return the class of the property */ public static Class getPropertyType(final Object bean, final String propertyName) { Class returnType = null; final Method getter = ReflectionUtils.findGetter(bean, propertyName); if (getter != null) { returnType = getter.getReturnType(); } return returnType; } /** * Make the given method accessible, explicitly setting it accessible if * necessary. The setAccessible(true) method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active). * * Note: borrowed from {@link org.springframework.util.ReflectionUtils} * * @param method * the method to make accessible * @see java.lang.reflect.Method#setAccessible */ public static void makeAccessible(final Method method) { // borrowed from org.springframework.util.ReflectionUtils if (!Modifier.isPublic(method.getModifiers()) || !Modifier .isPublic(method.getDeclaringClass().getModifiers())) { method.setAccessible(true); } } /** * Recherche l'ensemble des propri�t�s ayant au moin un getter et un setter * * @param bean * objet � parcourir;
* Si valeur transmise �gale � nul alors leve d'une exception de * type IllegalArgumentException * * @return Liste des nom des propri�t�s de l'objet � tester. Renvoi une * liste vide si aucune propriete trouv�. */ public static List findAllProperties(final Object bean) { if (bean == null) { throw new IllegalArgumentException("attribut can not be null"); } List getters = ReflectionUtils.findAllGetters(bean, true); List result = new ArrayList(); for (Method getter : getters) { String propertyName = ReflectionUtils.extractPropertiesName(getter .getName()); Method setter = ReflectionUtils.findSetter(bean, propertyName); if (setter != null && setter.getParameterTypes().length == 1 && getter.getReturnType() == setter.getParameterTypes()[0]) { result.add(propertyName); } } return result; } /** * * Methode permettant d'extraire le nom d'une propri�t� � partir du methode * * @param methodName * la methode � tester * @return le nom de la propri�t� ou un {@link IllegalArgumentException}si * la methode n'est pas un accesseur */ public static String extractPropertiesName(String methodName) { String result; if (methodName.startsWith(GETTER_PREFIX)) { result = StringUtils.removeStart(methodName, GETTER_PREFIX); } else if (methodName.startsWith(SETTER_PREFIX)) { result = StringUtils.removeStart(methodName, SETTER_PREFIX); } else if (methodName.startsWith(BOOLEAN_GETTER_PREFIX)) { result = StringUtils.removeStart(methodName, BOOLEAN_GETTER_PREFIX); } else { throw new IllegalArgumentException( "This method Name is not a getter or setter"); } result = StringUtils.uncapitalize(result); return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy