Java Class Name Format classToString(Class c)

Here you can find the source of classToString(Class c)

Description

Turns a class into a string.

License

Open Source License

Parameter

Parameter Description
c the class to turn into a string

Return

the string

Declaration

public static String classToString(Class c) 

Method Source Code

//package com.java2s;
/*//  ww w . ja  v  a2  s .  co  m
 *   This program 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 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /** the indicator for arrays. */
    public static final String ARRAY_INDICATOR = "[]";

    /**
     * Turns a class into a string.
     *
     * @param c      the class to turn into a string
     * @return      the string
     */
    public static String classToString(Class c) {
        String result;
        int dim;
        int i;

        if (c.isArray()) {
            dim = getArrayDimensions(c);
            result = getArrayClass(c).getName();
            for (i = 0; i < dim; i++)
                result += ARRAY_INDICATOR;
        } else {
            result = c.getName();
        }

        return result;
    }

    /**
     * Returns the dimensions of the given array. Even though the
     * parameter is of type "Object" one can hand over primitve arrays, e.g.
     * int[3] or double[2][4].
     *
     * @param array       the array to determine the dimensions for
     * @return            the dimensions of the array
     */
    public static int getArrayDimensions(Class array) {
        if (array.getComponentType().isArray())
            return 1 + getArrayDimensions(array.getComponentType());
        else
            return 1;
    }

    /**
     * Returns the dimensions of the given array. Even though the
     * parameter is of type "Object" one can hand over primitve arrays, e.g.
     * int[3] or double[2][4].
     *
     * @param array       the array to determine the dimensions for
     * @return            the dimensions of the array
     */
    public static int getArrayDimensions(Object array) {
        return getArrayDimensions(array.getClass());
    }

    /**
     * Returns the basic class of an array class (handles multi-dimensional
     * arrays).
     * @param c        the array to inspect
     * @return         the class of the innermost elements
     */
    public static Class getArrayClass(Class c) {
        if (c.getComponentType().isArray())
            return getArrayClass(c.getComponentType());
        else
            return c.getComponentType();
    }
}

Related

  1. classToResource(String className)
  2. classToResourceKeyPrefix(final Class clazz)
  3. classToResourceName(Class clazz)
  4. classToSignatureIdentifier(Class clazz)
  5. classToSlashed(String className)
  6. classToString(Class clazz)
  7. classToString(Class[] array, String separator, String defaultValue)
  8. classToString(Class[] cArr)
  9. classToString(int clasS)