Package com.app55.util

Source Code of com.app55.util.ReflectionUtil

package com.app55.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

import com.googlecode.openbeans.IntrospectionException;
import com.googlecode.openbeans.Introspector;
import com.googlecode.openbeans.PropertyDescriptor;

public final class ReflectionUtil
{
  public static Map<String, PropertyDescriptor> getAllProperties(Object o)
  {
    return getAllProperties(o, null);
  }

  private static Map<String, PropertyDescriptor> getAllProperties(Object o, Map<String, Boolean> exclude)
  {
    try
    {
      PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
      Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();

      for (PropertyDescriptor p : propertyDescriptors)
      {
        if ("class".equals(p.getName()))
          continue;

        if (p.getReadMethod().getAnnotation(JsonIgnore.class) != null)
          continue;

        String name = p.getName();
        JsonProperty altName = p.getReadMethod().getAnnotation(JsonProperty.class);
        if (altName != null && altName.value().length() > 0)
          name = altName.value();

        if (exclude != null && exclude.containsKey(name) && exclude.get(name))
          continue;

        properties.put(name, p);
      }

      return properties;
    }
    catch (IntrospectionException e)
    {
      return null;
    }
  }

  public static Object invokeMethod(String name, Object object, Class<?>[] parameterTypes, Object[] params)
  {
    try
    {
      Method m = getMethod(name, object.getClass(), parameterTypes);
      m.setAccessible(true);
      return m.invoke(object, params);
    }
    catch (Exception e)
    {
      // Means that the method doesn't exist or we don't have access to it.
      return null;
    }
  }

  public static Object getMember(String name, Object object)
  {
    try
    {
      Field f = getField(name, object.getClass());
      if (f == null)
        return null;

      f.setAccessible(true);
      return f.get(object);
    }
    catch (Exception e)
    {
      // Means that the member doesn't exist or we don't have access to it.
      return null;
    }
  }

  @SuppressWarnings("rawtypes")
  private static Field getField(String fieldName, Class clazz) throws NoSuchFieldException
  {
    try
    {
      return clazz.getDeclaredField(fieldName);
    }
    catch (NoSuchFieldException e)
    {
      Class superClass = clazz.getSuperclass();
      if (superClass == null)
      {
        throw e;
      }
      else
      {
        return getField(fieldName, superClass);
      }
    }
  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
  private static Method getMethod(String methodName, Class clazz, Class<?>[] parameterTypes) throws NoSuchMethodException
  {
    try
    {
      return clazz.getDeclaredMethod(methodName, parameterTypes);
    }
    catch (NoSuchMethodException e)
    {
      Class superClass = clazz.getSuperclass();
      if (superClass == null)
      {
        throw e;
      }
      else
      {
        return getMethod(methodName, superClass, parameterTypes);
      }
    }
  }
}
TOP

Related Classes of com.app55.util.ReflectionUtil

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.