Description
Convert the given String to the given class.
License
Open Source License
Parameter
Parameter | Description |
---|
clazz | The destination class. |
value | The String value to convert. |
Exception
Parameter | Description |
---|
Exception | If the given value cannot be transformed. |
Return
The converted value.
Declaration
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T fromString(final Class<T> clazz, final String value) throws Exception
Method Source Code
//package com.java2s;
/**/*from ww w .j a va2s. c o m*/
* Copyright (c) 2010 Ignasi Barrera
*
* 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.
*/
public class Main {
/**
* Convert the given String to the given class.
*
* @param clazz The destination class.
* @param value The String value to convert.
* @return The converted value.
* @throws Exception If the given value cannot be transformed.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T fromString(final Class<T> clazz, final String value) throws Exception {
if (value == null) {
return null;
} else if (clazz.equals(String.class)) {
return (T) value;
} else if (clazz.equals(Integer.class)) {
return (T) Integer.valueOf(value);
} else if (clazz.equals(Double.class)) {
return (T) Double.valueOf(value);
} else if (clazz.equals(Float.class)) {
return (T) Float.valueOf(value);
} else if (clazz.equals(Boolean.class)) {
return (T) Boolean.valueOf(value);
} else if (clazz.equals(Byte.class)) {
return (T) Byte.valueOf(value);
} else if (clazz.equals(Long.class)) {
return (T) Long.valueOf(value);
} else if (clazz.equals(Short.class)) {
return (T) Short.valueOf(value);
} else if (clazz.isEnum()) {
return (T) Enum.valueOf((Class<Enum>) clazz, value);
}
throw new Exception("Could not transform [" + value + "] to an object of class [" + clazz.getName() + "]");
}
}
Related
- fromString(Class> clazz, String stringValue)
- fromString(Class clazz, String name)
- fromString(Class clz, String value, T defaultVal)
- fromString(Class enumClass, String s, T defaultValue)
- fromString(Class enumType, String text)
- fromString(String string, Class arrayClass)
- getClass(Class clazz)
- getClass(String className)
- getClass(String className)