Here you can find the source of classToString(Class c)
Parameter | Description |
---|---|
c | the class to turn into a string |
public static String classToString(Class c)
//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(); } }