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

📄 classmap.java

📁 一个非常简洁的java脚本引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    arg=arg.getClass();
                }
            }

            methodKey.append(((Class)arg).getName());
        }

        return methodKey.toString();
    }

    /**
     * Retrieves public methods for a class. In case the class is not
     * public, retrieves methods with same signature as its public methods
     * from public superclasses and interfaces (if they exist). Basically
     * upcasts every method to the nearest acccessible method.
     */
    private static Method[] getAccessibleMethods(Class clazz)
    {
        Method[] methods = clazz.getMethods();

        /*
         *  Short circuit for the (hopefully) majority of cases where the
         *  clazz is public
         */

        if (Modifier.isPublic(clazz.getModifiers()))
        {
            return methods;
        }

        /*
         *  No luck - the class is not public, so we're going the longer way.
         */

        MethodInfo[] methodInfos = new MethodInfo[methods.length];

        for(int i = methods.length; i-- > 0; )
        {
            methodInfos[i] = new MethodInfo(methods[i]);
        }

        int upcastCount = getAccessibleMethods(clazz, methodInfos, 0);

        /*
         *  Reallocate array in case some method had no accessible counterpart.
         */

        if(upcastCount < methods.length)
        {
            methods = new Method[upcastCount];
        }

        int j = 0;
        for(int i = 0; i < methodInfos.length; ++i)
        {
            MethodInfo methodInfo = methodInfos[i];
            if(methodInfo.upcast)
            {
                methods[j++] = methodInfo.method;
            }
        }
        return methods;
    }

    /**
     *  Recursively finds a match for each method, starting with the class, and then
     *  searching the superclass and interfaces.
     *
     *  @param clazz Class to check
     *  @param methodInfos array of methods we are searching to match
     *  @param upcastCount current number of methods we have matched
     *  @return count of matched methods
     */
    private static int getAccessibleMethods( Class clazz, MethodInfo[] methodInfos, int upcastCount)
    {
        int l = methodInfos.length;

        /*
         *  if this class is public, then check each of the currently
         *  'non-upcasted' methods to see if we have a match
         */

        if( Modifier.isPublic(clazz.getModifiers()) )
        {
            for(int i = 0; i < l && upcastCount < l; ++i)
            {
                try
                {
                    MethodInfo methodInfo = methodInfos[i];

                    if(!methodInfo.upcast)
                    {
                        methodInfo.tryUpcasting(clazz);
                        upcastCount++;
                    }
                }
                catch(NoSuchMethodException e)
                {
                    /*
                     *  Intentionally ignored - it means
                     *  it wasn't found in the current class
                     */
                }
            }

            /*
             *  Short circuit if all methods were upcast
             */

            if(upcastCount == l)
            {
                return upcastCount;
            }
        }

        /*
         *   Examine superclass
         */

        Class superclazz = clazz.getSuperclass();

        if(superclazz != null)
        {
            upcastCount = getAccessibleMethods(superclazz , methodInfos, upcastCount);

            /*
             *  Short circuit if all methods were upcast
             */

            if(upcastCount == l)
            {
                return upcastCount;
            }
        }

        /*
         *  Examine interfaces. Note we do it even if superclazz == null.
         *  This is redundant as currently java.lang.Object does not implement
         *  any interfaces, however nothing guarantees it will not in future.
         */

        Class[] interfaces = clazz.getInterfaces();

        for(int i = interfaces.length; i-- > 0; )
        {
            upcastCount = getAccessibleMethods(interfaces[i], methodInfos, upcastCount);

            /*
             *  Short circuit if all methods were upcast
             */

            if(upcastCount == l)
            {
                return upcastCount;
            }
        }

        return upcastCount;
    }

    /**
     *  For a given method, retrieves its publicly accessible counterpart.
     *  This method will look for a method with same name
     *  and signature declared in a public superclass or implemented interface of this
     *  method's declaring class. This counterpart method is publicly callable.
     *
     *  @param method a method whose publicly callable counterpart is requested.
     *  @return the publicly callable counterpart method. Note that if the parameter
     *  method is itself declared by a public class, this method is an identity
     *  function.
     */
    public static Method getPublicMethod(Method method)
    {
        Class clazz = method.getDeclaringClass();

        /*
         *   Short circuit for (hopefully the majority of) cases where the declaring
         *   class is public.
         */

        if((clazz.getModifiers() & Modifier.PUBLIC) != 0)
        {
            return method;
        }

        return getPublicMethod(clazz, method.getName(), method.getParameterTypes());
    }

    /**
     *  Looks up the method with specified name and signature in the first public
     *  superclass or implemented interface of the class.
     *
     *  @param clazz the class whose method is sought
     *  @param name the name of the method
     *  @param paramTypes the classes of method parameters
     */
    private static Method getPublicMethod(Class clazz, String name, Class[] paramTypes)
    {
        /*
         *  if this class is public, then try to get it
         */

        if((clazz.getModifiers() & Modifier.PUBLIC) != 0)
        {
            try
            {
                return clazz.getMethod(name, paramTypes);
            }
            catch(NoSuchMethodException e)
            {
                /*
                 *  If the class does not have the method, then neither its
                 *  superclass nor any of its interfaces has it so quickly return
                 *  null.
                 */
                 return null;
            }
        }

        /*
         *  try the superclass
         */


        Class superclazz = clazz.getSuperclass();

        if ( superclazz != null )
        {
            Method superclazzMethod = getPublicMethod(superclazz, name, paramTypes);

            if(superclazzMethod != null)
            {
                return superclazzMethod;
            }
        }

        /*
         *  and interfaces
         */

        Class[] interfaces = clazz.getInterfaces();

        for(int i = 0; i < interfaces.length; ++i)
        {
            Method interfaceMethod = getPublicMethod(interfaces[i], name, paramTypes);

            if(interfaceMethod != null)
            {
                return interfaceMethod;
            }
        }

        return null;
    }

    /**
     *  Used for the iterative discovery process for public methods.
     */
    private static final class MethodInfo
    {
        Method method;
        String name;
        Class[] parameterTypes;
        boolean upcast;

        MethodInfo(Method method)
        {
            this.method = null;
            name = method.getName();
            parameterTypes = method.getParameterTypes();
            upcast = false;
        }

        void tryUpcasting(Class clazz)
            throws NoSuchMethodException
        {
            method = clazz.getMethod(name, parameterTypes);
            name = null;
            parameterTypes = null;
            upcast = true;
        }
    }
}

⌨️ 快捷键说明

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