Java Class Name Get getClassName(Class type)

Here you can find the source of getClassName(Class type)

Description

Map a class object back to a class name.

License

Apache License

Parameter

Parameter Description
type The class object we convert into name form.

Return

A string representation of the class name, in method signature format.

Declaration

public static String getClassName(Class type) 

Method Source Code

//package com.java2s;
/*//w  ww  .  ja v  a  2s  .  c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

import java.util.HashMap;

public class Main {
    /**
     * Table for mapping primitive classes back to their name signature type, which
     * allows a reverse mapping to be performed from a class object into a resolvable
     * signature.
     */
    private static final HashMap CLASS_TO_SIGNATURE_MAP = new HashMap();

    /**
     * Map a class object back to a class name.  The returned class object
     * must be "round trippable", which means
     * <p/>
     * type == ClassLoading.loadClass(ClassLoading.getClassName(type), classLoader)
     * <p/>
     * must be true.  To ensure this, the class name is always returned in
     * method signature format.
     *
     * @param type The class object we convert into name form.
     * @return A string representation of the class name, in method signature
     *         format.
     */
    public static String getClassName(Class type) {
        StringBuffer name = new StringBuffer();

        // we test these in reverse order from the resolution steps,
        // first handling arrays, then primitive types, and finally
        // "normal" class objects.

        // First handle arrays.  If a class is an array, the type is
        // element stored at that level.  So, for a 2-dimensional array
        // of ints, the top-level type will be "[I".  We need to loop
        // down the hierarchy until we hit a non-array type.
        while (type.isArray()) {
            // add another array indicator at the front of the name,
            // and continue with the next type.
            name.append('[');
            type = type.getComponentType();
        }

        // we're down to the base type.  If this is a primitive, then
        // we poke in the single-character type specifier.
        if (type.isPrimitive()) {
            name.append((String) CLASS_TO_SIGNATURE_MAP.get(type));
        }
        // a "normal" class.  This gets expressing using the "Lmy.class.name;" syntax.
        else {
            name.append('L');
            name.append(type.getName());
            name.append(';');
        }
        return name.toString();
    }
}

Related

  1. className(String underScore)
  2. classNames(String description)
  3. classNameSubName(String className)
  4. classNameSubPackage(String className)
  5. classNameWithType(String className, String type)
  6. getClassName(Integer typeValue)
  7. getClassName(String string)
  8. getClassName(String typeName)
  9. getClassName(String upnpDataType)