Package org.strecks.util

Source Code of org.strecks.util.PropertyValueGetter

/*
* Copyright 2005-2006 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.strecks.util;

import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.strecks.exceptions.ApplicationRuntimeException;

/**
* Utility class with methods mostly related to getting object and property values using reflection
* @author Phil Zoio
*/
public class PropertyValueGetter
{

  private static Log log = LogFactory.getLog(PropertyValueGetter.class);

  public static Object getPropertyValue(Object containingBean, String propertyName)
  {

    Assert.notNull(containingBean);
    Assert.notNull(propertyName);

    Object propertyValue;
    try
    {
      // properties with bind annotation must be Strings
      propertyValue = PropertyUtils.getProperty(containingBean, propertyName);
    }
    catch (IllegalAccessException e)
    {
      throw new ApplicationRuntimeException("Illegal getter method access attempted from "
          + containingBean.getClass() + " using property expression " + propertyName, e);
    }
    catch (NoSuchMethodException e)
    {
      throw new ApplicationRuntimeException("Required getter methods not found from " + containingBean.getClass()
          + " using property expression " + propertyName, e);
    }
    catch (Exception e)
    {
      throw new ApplicationRuntimeException("Unable to read property for bean "
          + containingBean.getClass().getName() + ", property " + propertyName, e);
    }
    return propertyValue;

  }

  public static PropertyDescriptor getPropertyDescriptorSilently(Object containingBean, String propertyName)
  {
    try
    {
      // properties with bind annotation must be Strings
      return getPropertyDescriptor(containingBean, propertyName);
    }
    catch (Exception e)
    {
      log.error(e);
      return null;
    }
  }

  public static Class<?> getPropertyTypeSilently(Object containingBean, String propertyName)
  {
    try
    {
      // properties with bind annotation must be Strings
      PropertyDescriptor propertyDescriptor = getPropertyDescriptor(containingBean, propertyName);

      if (propertyDescriptor != null)
      {
        return propertyDescriptor.getPropertyType();
      }
    }
    catch (Exception e)
    {
      log.error(e);
    }
    return null;
  }

  public static PropertyDescriptor getPropertyDescriptor(Object containingBean, String propertyName)
  {

    Assert.notNull(containingBean);
    Assert.notNull(propertyName);

    PropertyDescriptor descriptor;
    try
    {
      // properties with bind annotation must be Strings
      descriptor = PropertyUtils.getPropertyDescriptor(containingBean, propertyName);
    }
    catch (Exception e)
    {
      throw new ApplicationRuntimeException("Unable to read property descriptor for bean "
          + containingBean.getClass().getName() + ", property " + propertyName, e);
    }
    return descriptor;

  }

  /**
   * Uses <code>beanLocatingExpression</code> to find a property, potentially nested within
   * other properties, from <code>target</code>
   */
  public static Object getTargetBean(Object target, String beanLocatingExpression)
  {

    Object targetBean;

    if (beanLocatingExpression != null)
    {
      try
      {
        targetBean = PropertyUtils.getNestedProperty(target, beanLocatingExpression);
      }
      catch (NestedNullException e)
      {
        // property does not exist or is null
        targetBean = null;
      }
      catch (IllegalAccessException e)
      {
        throw new ApplicationRuntimeException("Illegal getter method access attempted from "
            + target.getClass() + " using expression " + beanLocatingExpression, e);
      }
      catch (NoSuchMethodException e)
      {
        throw new ApplicationRuntimeException("Required getter methods not found from " + target.getClass()
            + " using expression " + beanLocatingExpression, e);
      }
      catch (Exception e)
      {
        throw new ApplicationRuntimeException("Unable to obtain bean from " + target.getClass()
            + " using expression " + beanLocatingExpression, e);
      }
    }
    else
    {
      targetBean = target;
    }

    return targetBean;

  }

}
TOP

Related Classes of org.strecks.util.PropertyValueGetter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.