Java Class to Primitive getPrimitiveClass(String name)

Here you can find the source of getPrimitiveClass(String name)

Description

get Primitive Class

License

Apache License

Declaration

public static Class<?> getPrimitiveClass(String name) 

Method Source Code

//package com.java2s;
/*/*from w  w w.  j a v  a  2s .c  om*/
 *  Copyright 2009-2016 Weibo, Inc.
 *
 *    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.*;

public class Main {
    private static final String[] PRIMITIVE_NAMES = new String[] { "boolean", "byte", "char", "double", "float",
            "int", "long", "short", "void" };
    private static final Class<?>[] PRIMITIVE_CLASSES = new Class[] { boolean.class, byte.class, char.class,
            double.class, float.class, int.class, long.class, short.class, Void.TYPE };
    private static final int PRIMITIVE_CLASS_NAME_MAX_LENGTH = 7;

    public static Class<?> getPrimitiveClass(String name) {
        // check if is primitive class
        if (name.length() <= PRIMITIVE_CLASS_NAME_MAX_LENGTH) {
            int index = Arrays.binarySearch(PRIMITIVE_NAMES, name);
            if (index >= 0) {
                return PRIMITIVE_CLASSES[index];
            }
        }
        return null;
    }
}

Related

  1. getPrimitiveDftValue(Class clazz)
  2. getPrimitiveTypeByWrapper(Class clazz)
  3. getPrimitiveWrapper(Class c)
  4. getWrapper(String primitiveClassName)