Here you can find the source of toClassName(Object obj)
Parameter | Description |
---|---|
obj | the object, may be null |
public static String toClassName(Object obj)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w .j a v a 2 s. co m * Returns the name of the class of the object in a short and readable form. * * @param obj the object, may be null * @return the name */ public static String toClassName(Object obj) { if (obj == null) { return toClassName(Void.class); } return toClassName(obj.getClass()); } /** * Returns the name of the class in a short and readable form. * * @param type the class, may be null * @return the name */ public static String toClassName(Class<?> type) { if (type == null) { return "?"; } String name = ""; while (type.isArray()) { name = "[]" + name; type = type.getComponentType(); } if (type.isPrimitive()) { name = type.getName() + name; } else { name = getShortName(type.getName()) + name; } return name; } private static String getShortName(String currentName) { int beginIndex = currentName.lastIndexOf('.'); if (beginIndex >= 0) { currentName = currentName.substring(beginIndex + 1); } return currentName; } }