Java Class Simple Name Get simpleName(Class c)

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

Description

Produce the simple name of the given class, as specified by Class#getSimpleName , with an improved scheme for anonymous classes.

License

BSD License

Declaration

public static String simpleName(Class<?> c) 

Method Source Code

//package com.java2s;
/*BEGIN_COPYRIGHT_BLOCK*
    /*from   www .  jav a  2  s  . c o m*/
PLT Utilities BSD License
    
Copyright (c) 2007-2010 JavaPLT group at Rice University
All rights reserved.
    
Developed by:   Java Programming Languages Team
        Rice University
        http://www.cs.rice.edu/~javaplt/
    
Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:
    
- Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of 
  conditions and the following disclaimer in the documentation and/or other materials provided 
  with the distribution.
- Neither the name of the JavaPLT group, Rice University, nor the names of the library's 
  contributors may be used to endorse or promote products derived from this software without 
  specific prior written permission.
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
*END_COPYRIGHT_BLOCK*/

public class Main {
    /**
     * Produce the simple name of the given class, as specified by {@link Class#getSimpleName},
     * with an improved scheme for anonymous classes.  The simple name of a class is generally
     * the unqualified name used to declare it.  Arrays evaluate to the simple name of their
     * element type, followed by a pair of brackets.  Anonymous classes, rather than evaluating to an 
     * empty string, produce something like "anonymous Foo" (where Foo is the supertype).  Assumes
     * non-anonymous classes follow a naming convention in which the simple name is the suffix
     * of the full class name following all '.' and '$' characters, and immediately following a
     * (possibly empty) sequence of digits.
     */
    public static String simpleName(Class<?> c) {
        if (c.isArray()) {
            return simpleName(c.getComponentType()) + "[]";
        } else if (isAnonymousClass(c)) {
            if (c.getInterfaces().length > 0) {
                return "anonymous " + simpleName(c.getInterfaces()[0]);
            } else {
                return "anonymous " + simpleName(c.getSuperclass());
            }
        } else {
            String fullName = c.getName();
            int dot = fullName.lastIndexOf('.');
            int dollar = fullName.lastIndexOf('$');
            int nameStart = (dot > dollar) ? dot + 1 : dollar + 1;
            int length = fullName.length();
            while (nameStart < length && Character.isDigit(fullName.charAt(nameStart))) {
                nameStart++;
            }
            return fullName.substring(nameStart);
        }
    }

    /** An implementation of {@link Class#isAnonymousClass}, which is unavailable prior to Java 5.0 */
    public static boolean isAnonymousClass(Class<?> c) {
        String name = c.getName();
        String nameEnd = name.substring(name.lastIndexOf('$') + 1); // index is -1 if there is none
        for (int i = 0; i < nameEnd.length(); i++) {
            if (Character.isJavaIdentifierStart(nameEnd.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. simpleClassNameAndHashCodeString(Object object)
  2. simpleClassNameAndHashCodeString(Object object)
  3. simpleClassNameFrom(String fqClassName)
  4. simpleClassNameOf(Class c)
  5. simpleName(Class clazz)
  6. simpleName(final Class className)
  7. simpleName(final String fqn)
  8. simpleName(final String fullyQualifiedClassName)
  9. simpleName(final String name)