Java Class Name Clean classNameOnly(Object o)

Here you can find the source of classNameOnly(Object o)

Description

Given an Object instance, returns just the classname with no package

License

Open Source License

Parameter

Parameter Description
o PARAM

Return

Returns

Declaration

public static String classNameOnly(Object o) 

Method Source Code

//package com.java2s;
/*/*from   w  ww. j a  v  a2s  . c o m*/
 * {{{ header & license
 * GeneralUtil.java
 * Copyright (c) 2004, 2005 Patrick Wright
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */

public class Main {
    /**
     * Given an Object instance, returns just the classname with no package
     *
     * @param o PARAM
     * @return Returns
     */
    public static String classNameOnly(Object o) {
        String s = "[null object ref]";
        if (o != null) {
            s = classNameOnly(o.getClass().getName());
        }
        return s;
    }

    /**
     * Given a String classname, returns just the classname with no package
     *
     * @param cname PARAM
     * @return Returns
     */
    public static String classNameOnly(String cname) {
        String s = "[null object ref]";
        if (cname != null) {
            s = cname.substring(cname.lastIndexOf('.') + 1);
        }
        return s;
    }
}

Related

  1. classNameFromQualifiedClassName(String qualifiedName)
  2. classNameFromQualifiedName(String qualifiedCalssName)
  3. classNameWithoutGeneric(String s)
  4. classNameWithoutPackage(Class objectClass)
  5. classNameWithoutPackage(Class objectClass)
  6. classNameWithoutPackage(String classname)