Java Reflection Constructor Get getConstructorSignatureWithLongTypeNames(Constructor init)

Here you can find the source of getConstructorSignatureWithLongTypeNames(Constructor init)

Description

get Constructor Signature With Long Type Names

License

Open Source License

Declaration

public static String getConstructorSignatureWithLongTypeNames(Constructor<?> init) 

Method Source Code


//package com.java2s;
/*/* w w  w. ja  v a 2s. c om*/
 * Copyright (C) 2008-2013 Universidade Federal de Pernambuco and 
 * University of Central Florida
 *
 * This file is part of AJML
 *
 * AJML is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * AJML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AJML; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: ReflectUtil.java,v 1.0 2013/05/17 11:37:06 henriquerebelo Exp $
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Main {
    public static String getConstructorSignatureWithLongTypeNames(Constructor<?> init) {
        return getSignature(init, true);
    }

    private static String getSignature(Constructor<?> init, boolean longTypeNames) {
        return init.getName() + "(" + parametersAsString(init, longTypeNames) + ")";
    }

    private static String getSignature(Method method, boolean longTypeNames) {
        return method.getName() + "(" + parametersAsString(method, longTypeNames) + ")";
    }

    private static String parametersAsString(Constructor<?> init, boolean longTypeNames) {
        String result = "";
        Class<?>[] parameterTypes = init.getParameterTypes();
        if (parameterTypes.length > 0) {
            StringBuilder paramString = new StringBuilder();
            paramString.append(longTypeNames ? parameterTypes[0].getName() : parameterTypes[0].getSimpleName());
            for (int i = 1; i < parameterTypes.length; i++) {
                paramString.append(",")
                        .append(longTypeNames ? parameterTypes[i].getName() : parameterTypes[i].getSimpleName());
            }
            result = paramString.toString();
        }
        return result;
    }

    private static String parametersAsString(Method method, boolean longTypeNames) {
        String result = "";
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length > 0) {
            StringBuilder paramString = new StringBuilder();
            paramString.append(longTypeNames ? parameterTypes[0].getName() : parameterTypes[0].getSimpleName());
            for (int i = 1; i < parameterTypes.length; i++) {
                paramString.append(",")
                        .append(longTypeNames ? parameterTypes[i].getName() : parameterTypes[i].getSimpleName());
            }
            result = paramString.toString();
        }
        return result;
    }
}

Related

  1. getConstructors(Class clazz, int args)
  2. getConstructors(Class clazz)
  3. getConstructors(Class clazz, int modifier)
  4. getConstructors(final Class cl, final int params)
  5. getConstructorSignature(final Constructor constructor)
  6. getConstructorSimpleName(Constructor constructor)
  7. getConstructorsOfLength(final Class clazz, final int length)
  8. getConstructorWithArgs(Class clazz, Object... args)
  9. getConstructorWithLeastParametersFromList( ArrayList constructorsFromType)