首页

关于el-impl源码包中ReflectionUtil类映射工具类源码分析说明

标签:ReflectionUtil,el,映射工具类     发布时间:2018-03-15   

一、前言

关于el-impl源码包中的com.sun.el.util.ReflectionUtil类映射工具类,对class对象的初始化、字符串数组对于类数组的转换toTypeArray、指定类对象及实例方法getMethod等。

二、源码说明

package com.sun.el.util;@b@@b@import com.sun.el.lang.ELSupport;@b@import java.beans.BeanInfo;@b@import java.beans.IntrospectionException;@b@import java.beans.Introspector;@b@import java.beans.PropertyDescriptor;@b@import java.lang.reflect.Array;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Modifier;@b@import java.util.Arrays;@b@import javax.el.ELException;@b@import javax.el.MethodNotFoundException;@b@import javax.el.PropertyNotFoundException;@b@@b@public class ReflectionUtil@b@{@b@  protected static final String[] EMPTY_STRING = new String[0];@b@  protected static final String[] PRIMITIVE_NAMES = { "boolean", "byte", "char", "double", "float", "int", "long", "short", "void" };@b@  protected static final Class[] PRIMITIVES = { Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE, Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE, Void.TYPE };@b@@b@  public static Class forName(String name)@b@    throws ClassNotFoundException@b@  {@b@    if ((null == name) || ("".equals(name)))@b@      return null;@b@@b@    Class c = forNamePrimitive(name);@b@    if (c == null)@b@      if (name.endsWith("[]")) {@b@        String nc = name.substring(0, name.length() - 2);@b@        c = Class.forName(nc, true, Thread.currentThread().getContextClassLoader());@b@        c = Array.newInstance(c, 0).getClass();@b@      } else {@b@        c = Class.forName(name, true, Thread.currentThread().getContextClassLoader());@b@      }@b@@b@    return c;@b@  }@b@@b@  protected static Class forNamePrimitive(String name) {@b@    if (name.length() <= 8) {@b@      int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);@b@      if (p >= 0)@b@        return PRIMITIVES[p];@b@    }@b@@b@    return null;@b@  }@b@@b@  public static Class[] toTypeArray(String[] s)@b@    throws ClassNotFoundException@b@  {@b@    if (s == null)@b@      return null;@b@    Class[] c = new Class[s.length];@b@    for (int i = 0; i < s.length; ++i)@b@      c[i] = forName(s[i]);@b@@b@    return c;@b@  }@b@@b@  public static String[] toTypeNameArray(Class[] c)@b@  {@b@    if (c == null)@b@      return null;@b@    String[] s = new String[c.length];@b@    for (int i = 0; i < c.length; ++i)@b@      s[i] = c[i].getName();@b@@b@    return s;@b@  }@b@@b@  public static Method getMethod(Object base, Object property, Class[] paramTypes)@b@    throws MethodNotFoundException@b@  {@b@    if ((base == null) || (property == null)) {@b@      throw new MethodNotFoundException(MessageFactory.get("error.method.notfound", base, property, paramString(paramTypes)));@b@    }@b@@b@    String methodName = property.toString();@b@@b@    Method method = getMethod(base.getClass(), methodName, paramTypes);@b@    if (method == null) {@b@      throw new MethodNotFoundException(MessageFactory.get("error.method.notfound", base, property, paramString(paramTypes)));@b@    }@b@@b@    return method;@b@  }@b@@b@  private static Method getMethod(Class cl, String methodName, Class[] paramTypes)@b@  {@b@    Method m = null;@b@    try {@b@      m = cl.getMethod(methodName, paramTypes);@b@    } catch (NoSuchMethodException ex) {@b@      return null;@b@    }@b@@b@    Class dclass = m.getDeclaringClass();@b@    if (Modifier.isPublic(dclass.getModifiers())) {@b@      return m;@b@    }@b@@b@    Class[] arr$ = dclass.getInterfaces(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Class c = arr$[i$];@b@      m = getMethod(c, methodName, paramTypes);@b@      if (m != null)@b@        return m;@b@    }@b@@b@    Class c = dclass.getSuperclass();@b@    if (c != null) {@b@      m = getMethod(c, methodName, paramTypes);@b@      if (m != null)@b@        return m;@b@    }@b@@b@    return null;@b@  }@b@@b@  protected static final String paramString(Class[] types) {@b@    if (types != null) {@b@      StringBuffer sb = new StringBuffer();@b@      for (int i = 0; i < types.length; ++i)@b@        sb.append(types[i].getName()).append(", ");@b@@b@      if (sb.length() > 2)@b@        sb.setLength(sb.length() - 2);@b@@b@      return sb.toString();@b@    }@b@    return null;@b@  }@b@@b@  public static PropertyDescriptor getPropertyDescriptor(Object base, Object property)@b@    throws ELException, PropertyNotFoundException@b@  {@b@    PropertyDescriptor[] desc;@b@    int i;@b@    String name = ELSupport.coerceToString(property);@b@    PropertyDescriptor p = null;@b@    try {@b@      desc = Introspector.getBeanInfo(base.getClass()).getPropertyDescriptors();@b@@b@      for (i = 0; i < desc.length; ++i)@b@        if (desc[i].getName().equals(name))@b@          return desc[i];@b@    }@b@    catch (IntrospectionException ie)@b@    {@b@      throw new ELException(ie);@b@    }@b@    throw new PropertyNotFoundException(MessageFactory.get("error.property.notfound", base, name));@b@  }@b@@b@  public static Method findMethod(Object base, Object property, Object[] params)@b@    throws ELException@b@  {@b@    String methodName = property.toString();@b@    Method[] arr$ = base.getClass().getMethods(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Method m = arr$[i$];@b@      if ((m.getName().equals(methodName)) && (((m.isVarArgs()) || (m.getParameterTypes().length == params.length))))@b@      {@b@        return m;@b@      }@b@    }@b@    throw new ELException("Method " + methodName + " not Found");@b@  }@b@@b@  public static Object invokeMethod(Object base, Object property, Object[] params)@b@    throws ELException@b@  {@b@    int i;@b@    Method m = findMethod(base, property, params);@b@    Class[] parameterTypes = m.getParameterTypes();@b@    Object[] parameters = null;@b@    if (parameterTypes.length > 0) {@b@      if (m.isVarArgs()) { break label74:@b@      }@b@@b@      parameters = new Object[parameterTypes.length];@b@      for (i = 0; i < parameterTypes.length; ++i)@b@        parameters[i] = ELSupport.coerceToType(params[i], parameterTypes[i]);@b@@b@    }@b@@b@    try@b@    {@b@      label74: return m.invoke(base, parameters);@b@    } catch (IllegalAccessException iae) {@b@      throw new ELException(iae);@b@    } catch (InvocationTargetException ite) {@b@      throw new ELException(ite.getCause());@b@    }@b@  }@b@}