Java Reflection Primitive getPrimitive(final String classname)

Here you can find the source of getPrimitive(final String classname)

Description

Returns the primitive Class object if the specified class name represents a primitive type; otherwise, returns null

License

Apache License

Parameter

Parameter Description
classname The name of the class (or primitive) for which to retrieve the Class

Return

the primitive object if the specified class name represents a primitive type; otherwise, returns null

Declaration

public static Class<?> getPrimitive(final String classname) 

Method Source Code

//package com.java2s;
/*//from   ww w  .j  a v  a2 s . co m
 * Copyright 2012 Robert Philipp
 * 
 * Licensed 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;
import java.util.Map;

public class Main {
    private static Map<String, Class<?>> TYPE_MAP = new HashMap<>();

    /**
     * Returns the primitive {@link Class} object if the specified class name represents
     * a primitive type; otherwise, returns <code>null</code>
     * @param classname The name of the class (or primitive) for which to retrieve the {@link Class}
     * @return the primitive {@link Class} object if the specified class name represents
     * a primitive type; otherwise, returns <code>null</code>
     */
    public static Class<?> getPrimitive(final String classname) {
        return TYPE_MAP.get(classname);
        //      if( Integer.TYPE.toString().equals( classname ) )
        //      {
        //         return Integer.TYPE;
        //      }
        //      if( Double.TYPE.toString().equals( classname ) )
        //      {
        //         return Double.TYPE;
        //      }
        //      if( Float.TYPE.toString().equals( classname ) )
        //      {
        //         return Float.TYPE;
        //      }
        //      if( Long.TYPE.toString().equals( classname ) )
        //      {
        //         return Long.TYPE;
        //      }
        //      if( Short.TYPE.toString().equals( classname ) )
        //      {
        //         return Short.TYPE;
        //      }
        //      if( Boolean.TYPE.toString().equals( classname ) )
        //      {
        //         return Boolean.TYPE;
        //      }
        //      if( Character.TYPE.toString().equals( classname ) )
        //      {
        //         return Character.TYPE;
        //      }
        //      if( Byte.TYPE.toString().equals( classname ) )
        //      {
        //         return Byte.TYPE;
        //      }
        //      return null;
    }
}

Related

  1. getBENameByPrimitiveClassName( String implementClassName)
  2. getDefaultPrimitiveValue(Class clazz)
  3. getDefaultValue(Class primitive)
  4. getDefaultValueForPrimitive(Class type)
  5. getNullOrPrimitiveDefault(Class to)
  6. getPrimitiveClass(Class wrapper)
  7. getPrimitiveClass(Class wrapperClazz)
  8. getPrimitiveClass(Class boxedClass)
  9. getPrimitiveClass(String name)