| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.utils; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | public class ClassUtils { |
| 28 | /** |
| 29 | * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}. |
| 30 | */ |
| 31 | private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>(); |
| 32 | |
| 33 | static { |
| 34 | primitiveWrapperMap.put(Boolean.TYPE, Boolean.class); |
| 35 | primitiveWrapperMap.put(Byte.TYPE, Byte.class); |
| 36 | primitiveWrapperMap.put(Character.TYPE, Character.class); |
| 37 | primitiveWrapperMap.put(Short.TYPE, Short.class); |
| 38 | primitiveWrapperMap.put(Integer.TYPE, Integer.class); |
| 39 | primitiveWrapperMap.put(Long.TYPE, Long.class); |
| 40 | primitiveWrapperMap.put(Double.TYPE, Double.class); |
| 41 | primitiveWrapperMap.put(Float.TYPE, Float.class); |
| 42 | primitiveWrapperMap.put(Void.TYPE, Void.TYPE); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Maps wrapper {@code Class}es to their corresponding primitive types. |
| 47 | */ |
| 48 | private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<>(); |
| 49 | |
| 50 | static { |
| 51 | for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) { |
| 52 | final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass); |
| 53 | if (!primitiveClass.equals(wrapperClass)) { |
| 54 | wrapperPrimitiveMap.put(wrapperClass, primitiveClass); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, |
| 61 | * {@link Character}, |
| 62 | * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}). |
| 63 | * |
| 64 | * @param type The class to query or null. |
| 65 | * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link |
| 66 | * Character}, {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}). |
| 67 | */ |
| 68 | public static boolean isPrimitiveOrWrapper(final Class<?> type) { |
| 69 | if (type == null) { |
| 70 | return false; |
| 71 | } |
| 72 | return type.isPrimitive() || isPrimitiveWrapper(type); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, |
| 77 | * {@link Short}, |
| 78 | * {@link Integer}, {@link Long}, {@link Double}, {@link Float}). |
| 79 | * |
| 80 | * @param type The class to query or null. |
| 81 | * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, |
| 82 | * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}). |
| 83 | * @since 3.1 |
| 84 | */ |
| 85 | public static boolean isPrimitiveWrapper(final Class<?> type) { |
| 86 | return wrapperPrimitiveMap.containsKey(type); |
| 87 | } |
| 88 | } |
| 89 |
Members