001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class ClassResolverUtil {
024    
025            public static Class<?> resolve(String className, ClassLoader classLoader)
026                    throws ClassNotFoundException {
027    
028                    try {
029                            return Class.forName(className, false, classLoader);
030                    }
031                    catch (ClassNotFoundException cnfe) {
032                            Class<?> clazz = _primitiveClasses.get(className);
033    
034                            if (clazz != null) {
035                                    return clazz;
036                            }
037                            else {
038                                    throw cnfe;
039                            }
040                    }
041            }
042    
043            private static final Map<String, Class<?>> _primitiveClasses =
044                    new HashMap<String, Class<?>>(9, 1.0F);
045    
046            static {
047                    _primitiveClasses.put("boolean", boolean.class);
048                    _primitiveClasses.put("byte", byte.class);
049                    _primitiveClasses.put("char", char.class);
050                    _primitiveClasses.put("short", short.class);
051                    _primitiveClasses.put("int", int.class);
052                    _primitiveClasses.put("long", long.class);
053                    _primitiveClasses.put("float", float.class);
054                    _primitiveClasses.put("double", double.class);
055                    _primitiveClasses.put("void", void.class);
056            }
057    
058    }