集册 Java实例教程 返回所传递成员的描述符

返回所传递成员的描述符

欢马劈雪     最近更新时间:2020-01-02 10:19:05

482
返回所传递成员的描述符
/*from 时 代 J     a    v  a - nowjava.com*/

//package com.nowjava;


import java.lang.reflect.Constructor;

import java.lang.reflect.Member;

import java.lang.reflect.Method;


public class Main {

    /**

     * Returns the descriptor for the passed member

     * @param m The class member

     * @return the member descriptor

     */

    public static String getMemberDescriptor(final Member m) {

        if (m instanceof Method) {

            return getMethodDescriptor((Method) m);

        } else if (m instanceof Constructor) {

            return getConstructorDescriptor((Constructor) m);

        } else {

            return m.toString();

        }

    }


    /**

     * Returns the descriptor corresponding to the given method.

     * @param m a {@link Method Method} object.

     * @return the descriptor of the given method.

     * (All credit to ObjectWeb ASM)

     * @author Eric Bruneton  

     * @author Chris Nokleberg

     */

    public static String getMethodDescriptor(final Method m) {

        Class<?>[] parameters = m.getParameterTypes();
        /*
        来 自*
         时代Java - nowjava.com
        */

        StringBuffer buf = new StringBuffer();

        buf.append('(');

        for (int i = 0; i < parameters.length; ++i) {

            getDescriptor(buf, parameters[i]);

        }

        buf.append(')');

        getDescriptor(buf, m.getReturnType());

        return buf.toString();

    }


    /**

     * Returns the descriptor corresponding to the given constructor.

     * @param c a {@link Constructor Constructor} object.

     * @return the descriptor of the given constructor.

     * (All credit to ObjectWeb ASM)

     * @author Eric Bruneton  

     * @author Chris Nokleberg

     */

    public static String getConstructorDescriptor(final Constructor<?> c) {

        Class<?>[] parameters = c.getParameterTypes();

        StringBuffer buf = new StringBuffer();

        buf.append('(');

        for (int i = 0; i < parameters.length; ++i) {

            getDescriptor(buf, parameters[i]);

        }

        return buf.append(")V").toString();

    }


    /**

     * Appends the descriptor of the given class to the given string buffer.

     * @param buf the string buffer to which the descriptor must be appended.

     * @param c the class whose descriptor must be computed.

     * (All credit to ObjectWeb ASM)

     * @author Eric Bruneton  

     * @author Chris Nokleberg

     */

    private static void getDescriptor(final StringBuffer buf,

            final Class<?> c) {

        Class<?> d = c;

        while (true) {

            if (d.isPrimitive()) {

                char car;

                if (d == Integer.TYPE) {

                    car = 'I';

                } else if (d == Void.TYPE) {

                    car = 'V';

                } else if (d == Boolean.TYPE) {

                    car = 'Z';

                } else if (d == Byte.TYPE) {

                    car = 'B';

                } else if (d == Character.TYPE) {

                    car = 'C';

                } else if (d == Short.TYPE) {

                    car = 'S';

                } else if (d == Double.TYPE) {

                    car = 'D';

                } else if (d == Float.TYPE) {

                    car = 'F';

                } else /* if (d == Long.TYPE) */{

                    car = 
展开阅读全文