简体   繁体   English

如何将值解析为适当的目标类型

[英]How can I parse a value into the appropriate target type

I have a method which has a two parameters value:Object and initialValueType:String. 我有一个方法,它有两个参数值:Object和initialValueType:String。

public Object parseValueForInitialValueType(Object value, String initialValueType) {
    Class<?> clazz = Class.forName(initialValueType);
    Constructor<?> constructor = clazz.getConstructor(String.class);
    return constructor.newInstance(value);
}

This parameters may have following values: 此参数可能具有以下值:

value -> "test":String; initialValueType -> "java.lang.String"; method output -> "test":String
value -> 123.0:Double; initialValueType -> "java.lang.Double"; method output -> 123.0:Double
value -> "456.0":String; initialValueType -> "java.lang.Double"; method output -> "426.0:Double
value -> "123.000":String; initialValueType -> "java.math.BigDecimal"; method output -> 123.000:BigDecimal

Issue is that, I get exception: 问题是,我得到了例外:

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

How can I parse value into desired initialValueType? 如何将值解析为所需的initialValueType?

Basically, I want to construct a new object for initialValueType. 基本上,我想为initialValueType构造一个新对象。

Seems like solution: 似乎解决方案:

public Object parseValueForInitialValueType(Object value, String initialValueType) {
    Class<?> clazz = Class.forName(initialValueType);
    Constructor<?> constructor = clazz.getConstructor(String.class);
    return constructor.newInstance(String.valueOf(value));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值为 Object 的 Map 转换为适当的类型? - How can I transform a Map with a value of Object to the appropriate type? 如何声明具有适当类型的64位整数的ARRAY_SIZE的arr1 - How can I declare an arr1 of ARRAY_SIZE with the appropriate type 64 bit integer 如何从 Google Spanner 的变异中获取适当的数据类型值? - How to get appropriate data type value from mutation in Google Spanner? 如何检查值是否为整数类型? - How can I check if a value is of type Integer? 如何解析可以是对象或字符串的值-Java-Jackson - How do I parse an value that can be an object or a string - Java - Jackson 如何从 Java 中的 ElasticSearch 响应中解析 GeoPoint 值? - How can I parse a GeoPoint value out of an ElasticSearch response in Java? 如何为MERGE语句发送适当的HTTP状态? - How can I send an appropriate HTTP status for MERGE statements? 如何将ASCII值映射到适当的密钥代码? - How can I map ASCII values to appropriate key codes? 如何将注释的目标限制为“ ElementType.TYPE_USE”, - How can I limit an annotation 's target only to be 'ElementType.TYPE_USE', 如何消除 Unsupported target type: int QueryDsl 动态搜索运算符错误 - How can I eliminate the Unsupported target type : int error in QueryDsl dynamic search for operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM