Description
Like Class.forName, but works for primitive types too
License
Open Source License
Parameter
Parameter | Description |
---|
name | a parameter |
Exception
Parameter | Description |
---|
ClassNotFoundException | an exception |
Declaration
public static Class forName(String name) throws ClassNotFoundException
Method Source Code
//package com.java2s;
/**//from ww w .j a v a 2s .c o m
* Copyright (C) 2000-2015 The Software Conservancy and Original Authors.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Nothing in this notice shall be deemed to grant any rights to trademarks,
* copyrights, patents, trade secrets or any other intellectual property of the
* licensor or any contributor except as expressly stated herein. No patent
* license is granted separate from the Software, for code that you delete from
* the Software, or for combinations of the Software with other software or
* hardware.
*/
public class Main {
/**
* Like Class.forName, but works for primitive types too
*
* @param name
* @return
* @throws ClassNotFoundException
*/
public static Class forName(String name) throws ClassNotFoundException {
if (name.equals("int"))
return int.class;
if (name.equals("double"))
return double.class;
if (name.equals("boolean"))
return boolean.class;
if (name.equals("long"))
return long.class;
if (name.equals("float"))
return float.class;
if (name.equals("char"))
return char.class;
if (name.equals("short"))
return short.class;
if (name.equals("byte"))
return byte.class;
return Class.forName(name);
}
}
Related
- forName(String className, Class xface)
- forName(String className, ClassLoader classLoader)
- forName(String className, ClassLoader defaultClassLoader)
- forName(String name)
- forName(String name)
- forName(String name)
- forName(String name)
- forName(String name)
- forName(String name, Class> caller)