Here you can find the source of convertStringToObject(String value, Class> type)
Not support negative numeric value: - http://en.wikipedia.org/wiki/Double-precision_floating-point_format
Parameter | Description |
---|---|
value | a parameter |
type | a parameter |
public static Object convertStringToObject(String value, Class<?> type)
//package com.java2s; /*/*from ww w.j a v a 2s .c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Not support negative numeric value: * - http://en.wikipedia.org/wiki/Double-precision_floating-point_format * * @param value * @param type * @return */ public static Object convertStringToObject(String value, Class<?> type) { Object obj = null; try { if (String.class.equals(type)) { obj = value; } if (Long.class.equals(type) || long.class.equals(type)) { obj = Long.parseLong(value); // if((Long) obj < 0) throw new IllegalArgumentException("Don't support negative Long yet: "+obj); } else if (Integer.class.equals(type) || int.class.equals(type)) { obj = Integer.parseInt(value); // if((Integer) obj < 0) throw new IllegalArgumentException("Don't support negative Integer yet: "+obj); } else if (Double.class.equals(type) || double.class.equals(type)) { obj = Double.parseDouble(value); // if((Double) obj < 0) throw new IllegalArgumentException("Don't support negative Double yet: "+obj); } else if (Float.class.equals(type) || float.class.equals(type)) { obj = Float.parseFloat(value); // if((Double) obj < 0) throw new IllegalArgumentException("Don't support negative Float yet: "+obj); } if (obj != null) return obj; } catch (NumberFormatException ex) { throw new IllegalArgumentException("Fail to convert string: " + value + " into type of " + type, ex); } throw new IllegalArgumentException( "Fail to convert string: " + value + " into type of " + type + ", illegal type: " + type); } }