从字符串转换为通用类型 [英] Conversion from string to generic type

查看:78
本文介绍了从字符串转换为通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字符串转换为变量值。我发现仅针对C#的解决方案。我需要用Java。

I need to convert string to variable value. I found solution only for C#. I need it in Java.

public class Property<T> {

    T value;

    public void setValue(String input){
        if(value instanceof String){
           value= input; // value is type of T not type of string (compilation error)
                         // incompatible types: String cannot be converted to T
        }
        if(value instanceof int){
           //parse string
        }
        if(value instanceof boolean){
           //parse string
        }
        ...
    }
}


推荐答案

那不是它的工作原理。但是,您可以使用多态来获得有用的结果。

That is not how it works. You can, however, use polymorphism, to achieve a useful result.

基本通用(和摘要)property

Base generic (and abstract) property

public abstract class Property<T> {
    T value;
    public abstract void setValue(String input);
}

字符串属性

public class StringProperty extends Property<String> {
    @Override
    public void setValue(String input) {
        this.value = input;
    }
}

整数属性

public class IntegerProperty extends Property<Integer> {
    @Override
    public void setValue(String input) {
        this.value = Integer.valueOf(input);
    }
}

不确定您的实际目标是什么,但是这种方法可能有效。

Not sure what your actual goal is, but this approach might work.

请注意,由于类型擦除, T 的输入实例将失败。

Note, that input instanceof T will fail, because of type erasure. It's not gonna work.

要详细说明您的方法,这可以工作-但这很丑。

To elaborate more on your approach, this would work - but it's UGLY.

丑陋而不是丑陋很方便。

Ugly and not very convenient. No idea why you'd want it, tbh.

class Property<T> {

    public T value;
    private final Class<T> clazz;

    public Property(Class<T> clazz) {
        super();
        this.clazz = clazz;
    }       

    @SuppressWarnings("unchecked")
    public void setValue(String input) {
        if (clazz.isAssignableFrom(String.class)) {
            value = (T) input;
        } else if (clazz.isAssignableFrom(Integer.class)) {
            value = (T) Integer.valueOf(input);
        } else if (clazz.isAssignableFrom(Boolean.class)) {
            value = (T) Boolean.valueOf(input);
        } else if (clazz.isAssignableFrom(Double.class)) {
            value = (T) Double.valueOf(input);
        } else {
            throw new IllegalArgumentException("Bad type.");
        }
    }
}

用法如下:

Property<String> ff = new Property<>(String.class);
ff.setValue("sdgf");

Property<Integer> sdg = new Property<>(Integer.class);
sdg.setValue("123");

System.out.println(ff.value);
System.out.println(sdg.value);






具有 Reflection



显然,可以找出用于实例化属性的参数。


Solution with Reflection

Apparently, it's possible to figure out the parameter used to instantiate property.

这个小小的魔术公式可以为您提供以下信息:

This little magic formula gives you just that:

(Class<?>) getClass().getTypeParameters()[0].getBounds()[0]

我什至不知道如何找到它。好吧,我们开始:

I don't even know how I managed to find it. Well, here we go:

class Property<T> {

    T value;    

    @SuppressWarnings("unchecked")
    public void setValue(String input)
    {
        // BEHOLD, MAGIC!
        Class<?> clazz = (Class<?>) getClass().getTypeParameters()[0].getBounds()[0];

        if (clazz.isAssignableFrom(String.class)) {
            value = (T) input;
        } else if (clazz.isAssignableFrom(Integer.class)) {
            value = (T) Integer.valueOf(input);
        } else if (clazz.isAssignableFrom(Boolean.class)) {
            value = (T) Boolean.valueOf(input);
        } else if (clazz.isAssignableFrom(Double.class)) {
            value = (T) Double.valueOf(input); 
        } else {
            throw new IllegalArgumentException("Bad type.");
        }
    }
}

不要看我,我不会用那个。我有一些常识。

And don't look at me, I wouldn't use that. I have some common sense.

这篇关于从字符串转换为通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆