Java Reflection Constructor Get getConstructorDescriptor(final Constructor c)

Here you can find the source of getConstructorDescriptor(final Constructor c)

Description

Returns the descriptor corresponding to the given constructor.

License

LGPL

Parameter

Parameter Description
c a Constructor Constructor object.

Return

the descriptor of the given constructor. (All credit to ObjectWeb ASM)

Declaration

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

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.reflect.Constructor;

public class Main {
    /**/*from w  ww .  j  a  va2  s  . c  o  m*/
     * 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 = 'J';
                }
                buf.append(car);
                return;
            } else if (d.isArray()) {
                buf.append('[');
                d = d.getComponentType();
            } else {
                buf.append('L');
                String name = d.getName();
                int len = name.length();
                for (int i = 0; i < len; ++i) {
                    char car = name.charAt(i);
                    buf.append(car == '.' ? '/' : car);
                }
                buf.append(';');
                return;
            }
        }
    }
}

Related

  1. getConstructor(String string, Class... types)
  2. getConstructor(String type, Class[] paramTypes)
  3. getConstructor(String typeString)
  4. getConstructorAccessor(Class enumClass, Class[] additionalParameterTypes)
  5. getConstructorCalls(Class aClass)
  6. getConstructorForArguments( java.lang.reflect.Constructor[] constructors, Object... arguments)
  7. getConstructorFromSignature(Class clazz, String cstrSig)
  8. getConstructorIfAvailable(Class clazz, Class... paramTypes)
  9. getConstructorLabel(java.lang.reflect.Constructor con)