Java Class Name Get getClassNameAsSrc(Class clazz)

Here you can find the source of getClassNameAsSrc(Class clazz)

Description

Gets the name of the given class as it would be declared in Java source code.

License

Open Source License

Parameter

Parameter Description
clazz a parameter

Declaration

public static String getClassNameAsSrc(Class<?> clazz) 

Method Source Code

//package com.java2s;

import java.util.Iterator;

public class Main {
    /**/* ww  w . j a v  a 2s.co  m*/
     * Gets the name of the given class as it would be declared in Java source code.
     * 
     * @param clazz
     * @return
     */
    public static String getClassNameAsSrc(Class<?> clazz) {

        return getClassNameAsSrc(clazz, 0);
    }

    private static String getClassNameAsSrc(Class<?> clazz, int numArrayDimensions) {

        if (clazz.isArray()) {
            return getClassNameAsSrc(clazz.getComponentType(), numArrayDimensions + 1);
        } else {
            if (numArrayDimensions == 0) {
                return clazz.getName().replace('$', '.');
            } else {
                StringBuilder classJavaSrcName = new StringBuilder(clazz.getName().replace('$', '.'));
                for (int n = 0; n < numArrayDimensions; n++) {
                    classJavaSrcName.append("[]");
                }
                return classJavaSrcName.toString();
            }
        }
    }

    public static String toString(Iterable<String> strs) {

        StringBuilder ret = new StringBuilder();
        for (Iterator<String> i = strs.iterator(); i.hasNext();) {
            ret.append(i.next());
            if (i.hasNext())
                ret.append(",");
        }
        return ret.toString();
    }
}

Related

  1. getClassName(Class type)
  2. getClassName(Integer typeValue)
  3. getClassName(String string)
  4. getClassName(String typeName)
  5. getClassName(String upnpDataType)
  6. getClassNameFromConfig(Object config)
  7. getClassNames( StackTraceElement[] currentStack)
  8. getClassNames(Class[] ifaces)
  9. getClassNameWithoutPackage(Object object)