1 package com.github.javaparser.utils; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class ClassUtils { 7 /** 8 * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}. 9 */ 10 private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<Class<?>, Class<?>>(); 11 static { 12 primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); 13 primitiveWrapperMap.put(Byte.TYPE, Byte.class); 14 primitiveWrapperMap.put(Character.TYPE, Character.class); 15 primitiveWrapperMap.put(Short.TYPE, Short.class); 16 primitiveWrapperMap.put(Integer.TYPE, Integer.class); 17 primitiveWrapperMap.put(Long.TYPE, Long.class); 18 primitiveWrapperMap.put(Double.TYPE, Double.class); 19 primitiveWrapperMap.put(Float.TYPE, Float.class); 20 primitiveWrapperMap.put(Void.TYPE, Void.TYPE); 21 } 22 23 /** 24 * Maps wrapper {@code Class}es to their corresponding primitive types. 25 */ 26 private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<Class<?>, Class<?>>(); 27 static { 28 for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) { 29 final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass); 30 if (!primitiveClass.equals(wrapperClass)) { 31 wrapperPrimitiveMap.put(wrapperClass, primitiveClass); 32 } 33 } 34 } 35 36 /** 37 * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, 38 * {@link Character}, 39 * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}). 40 * 41 * @param type 42 * The class to query or null. 43 * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, 44 * {@link Character}, 45 * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}). 46 */ 47 public static boolean isPrimitiveOrWrapper(final Class<?> type) { 48 if (type == null) { 49 return false; 50 } 51 return type.isPrimitive() || isPrimitiveWrapper(type); 52 } 53 54 /** 55 * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, 56 * {@link Short}, 57 * {@link Integer}, {@link Long}, {@link Double}, {@link Float}). 58 * 59 * @param type 60 * The class to query or null. 61 * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, 62 * {@link Short}, 63 * {@link Integer}, {@link Long}, {@link Double}, {@link Float}). 64 * @since 3.1 65 */ 66 public static boolean isPrimitiveWrapper(final Class<?> type) { 67 return wrapperPrimitiveMap.containsKey(type); 68 } 69 } 70