ClassUtils.java revision 9985c47e3b2a6ce9b434e787a2a035e2a7aadb4a
19985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidpackage com.github.javaparser.utils;
29985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
39985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidimport java.util.HashMap;
49985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidimport java.util.Map;
59985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
69985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoidpublic class ClassUtils {
79985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
89985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}.
99985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
109985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<Class<?>, Class<?>>();
119985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    static {
129985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
139985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Byte.TYPE, Byte.class);
149985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Character.TYPE, Character.class);
159985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Short.TYPE, Short.class);
169985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Integer.TYPE, Integer.class);
179985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Long.TYPE, Long.class);
189985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Double.TYPE, Double.class);
199985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Float.TYPE, Float.class);
209985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
219985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
229985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
239985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
249985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Maps wrapper {@code Class}es to their corresponding primitive types.
259985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
269985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<Class<?>, Class<?>>();
279985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    static {
289985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) {
299985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass);
309985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            if (!primitiveClass.equals(wrapperClass)) {
319985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid                wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
329985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            }
339985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        }
349985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
359985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
369985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
379985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
389985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Character},
399985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
409985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *
419985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @param type
429985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *            The class to query or null.
439985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
449985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *         {@link Character},
459985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *         {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
469985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
479985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    public static boolean isPrimitiveOrWrapper(final Class<?> type) {
489985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        if (type == null) {
499985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid            return false;
509985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        }
519985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        return type.isPrimitive() || isPrimitiveWrapper(type);
529985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
539985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid
549985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    /**
559985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
569985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Short},
579985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
589985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *
599985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @param type
609985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *            The class to query or null.
619985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
629985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *         {@link Short},
639985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     *         {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
649985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     * @since 3.1
659985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid     */
669985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    public static boolean isPrimitiveWrapper(final Class<?> type) {
679985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid        return wrapperPrimitiveMap.containsKey(type);
689985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid    }
699985c47e3b2a6ce9b434e787a2a035e2a7aadb4amatozoid}
70