⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reflectionutils.java

📁 这是一个关于工作流管理的系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Class valueClass = value.getClass();        Long longValue = null;        if (targetClass.equals(java.sql.Date.class) ||            targetClass.equals(java.util.Date.class))        {            valueClass = Long.class;            longValue = new Long(((java.util.Date)value).getTime());            /*            log.debug                ("buildInstance() - valueClass is set to 'Long'");            log.debug                ("buildInstance() - longValue is set to '"+                 longValue.longValue()+"'");            */        }        for (int i=0; i<constructors.length; i++)        {            java.lang.reflect.Constructor c = constructors[i];            if (c.getParameterTypes().length != 1) continue;            //if (valueClass.isAssignableFrom(c.getParameterTypes()[0]))            if (c.getParameterTypes()[0].isAssignableFrom(valueClass))            {                constructor = c;                break;            }            if (c.getParameterTypes()[0].equals(String.class))            {                secondMatchConstructor = c;            }        }        if (constructor == null &&            secondMatchConstructor == null)        {            throw new NoSuchMethodException                ("Couldn't find any constructor in class '"+                 targetClass.getName()+                 "' that could accept 'java.lang.String' or '"+                 valueClass.getName()+"' as parameter type");        }        if (constructor == null)        {            constructor = secondMatchConstructor;            value = value.toString();        }        if (longValue != null)        {            value = longValue;        }        return             constructor.newInstance(new Object[] { value });    }    /**      * Returns a new instance of the given object.     * The object must have a constructor with no parameters, else     * this method will throw an IllegalArgumentException     */    public static Object newInstance (final Object o)    {        try        {            return o.getClass().newInstance();        }        catch (final Throwable t)        {            throw new IllegalArgumentException                ("Failed to build new instance of class "+o.getClass()+                 "because\n"+t);        }    }    /**     * Builds an instance for a given class name.     */    public static Object buildNewInstance (final String className)    {        try        {            final Class clazz = Class.forName(className);            return clazz.newInstance();        }        catch (final Throwable t)        {            throw new IllegalArgumentException                ("Failed to build new instance of class '"+className+                 "' because of "+t);        }    }    /**     * Invokes the given method on the target object and return the     * result if any.     */    public static Object invokeStatic        (final Class target,          final String methodName,          final Class[] argClasses,         final Object[] args)    throws         Exception    {        final java.lang.reflect.Method method =             target.getMethod(methodName, argClasses);        return method.invoke(null, args);    }    /**     * Invokes the given method on the target object and return the     * result if any.     */    public static Object invoke         (final Object target,          final String methodName,          final Class[] argClasses,         final Object[] args)    throws         Exception    {        final java.lang.reflect.Method method =             target.getClass().getMethod(methodName, argClasses);        return method.invoke(target, args);    }    /**     * Invoke the given method in its own thread : don't wait on its output.     * Problems with running the invocation only appear in the logs.     * This method is especially used by the ExpressionPool implementations to     * decrease reply time.     */    public static void invokeInOwnThread        (final Object target,          final String methodName,          final Class[] argClasses,         final Object[] args,         final String potentialErrorMessage)    {        (new Thread ()         {             public void run ()             {                 try                 {                     invoke(target, methodName, argClasses, args);                 }                 catch (Throwable t)                 {                     log.warn(potentialErrorMessage, t);                 }             }         }).start();    }    /**     * Invoke the given method in its own thread : don't wait on its output.     * This variant uses a default potential error message.     * Problems with running the invocation only appear in the logs.     * This method is especially used by the ExpressionPool implementations to     * decrease reply time.     */    public static void invokeInOwnThread        (final Object target,          final String methodName,          final Class[] argClasses,         final Object[] args)    {        final String potentialErrorMessage =             "invoking the method '"+methodName+            "' on object of class '"+target.getClass().getName()+            "' failed";        invokeInOwnThread            (target, methodName, argClasses, args, potentialErrorMessage);    }    /**     * Returns true if the given class has a constructor with no parameters.     * (a potential bean class).     */    public static boolean hasNoParamConstructor (final Class c)    {        try        {            c.getConstructor(new Class[] {});        }        catch (final NoSuchMethodException e)        {            return false;        }        return true;    }    /**     * Returns a list of Method[] arrays that contain the get/set pair for     * each read/write field of the given instance.     * This method is heavily used by openwfe.org.xml.XmlCoder.     */    public static java.util.List listReadWriteFields         (final Object instance)    {        Class clazz = instance.getClass();        if (instance instanceof Class) clazz = (Class)instance;        final java.util.List cachedResult =             (java.util.List)fieldCache.get(clazz);        if (cachedResult != null)             return cachedResult;        final java.util.List result = new java.util.ArrayList(10);        final Method[] ms = clazz.getMethods();        for (int i=0; i<ms.length; i++)        {            final Method getMethod = ms[i];            final Class returnType = getMethod.getReturnType();            final String getMethodName = getMethod.getName();            //log.debug("listReadWriteFields() considering >"+getMethodName+"<");            if (returnType == null) continue;            if (getMethod.getParameterTypes().length > 1) continue;            //log.debug("listReadWriteFields() return and params ok");            if (( ! getMethodName.startsWith(GET)) &&                ( ! getMethodName.startsWith(IS)))            {                continue;            }            //log.debug("listReadWriteFields() prefix ok");            String rawFieldName = getMethodName.substring(3);            if (getMethodName.startsWith(IS))                 rawFieldName = getMethodName.substring(2);            Method setMethod = null;            try            {                setMethod = clazz.getMethod                    (SET + rawFieldName, new Class[] { returnType });                final String sReturnType = ""+setMethod.getReturnType();                if (( ! sReturnType.equals("void")) &&                    ( ! sReturnType.equals("null")))                {                    continue;                }            }            catch (final NoSuchMethodException nsme)            {                continue;            }            //log.debug("listReadWriteFields() adding >"+rawFieldName+"<");            result.add(new Method[] { getMethod, setMethod });        }        //        // cache result                fieldCache.put(instance.getClass(), result);        //        // that's all folks        return result;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -