Conversion from string to generic type
Asked Answered
W

3

7

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
        }
        ...
    }
}
Walter answered 5/7, 2014 at 18:43 Comment(10)
if you dont know what T is, you can't convert to it. You can cast, but it can easily fail. Poor design, I say.Bicyclic
What is a "specific type of generic variable"?Swane
This doesn't make sense, how can it not be a String if the argument is of type string.Metamorphosis
I mean type of T which i recognize with instanceofWalter
@Walter Generics buys you nothing in what you have coded. What are you trying to achieve?Cyprio
@Walter Look now into my answer - there is a working solution using your approach. Not very nice, but works.Bicyclic
@Bicyclic You are right. This is what I was looking for and its ugly.Walter
@Walter see also the third solution. It's some real witchcraft now. Actually, you dont have to pass the class.Bicyclic
@Bicyclic It's a bit "i have no idea what i'm doing" for me.Walter
@Walter I won't lie, I found it by trial & error. It's very obscure for me as well. But I don't mind dirty tricks, as long as they do my bidding xDBicyclic
B
14

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

Solution with polymorphism

Base generic (and abstract) property

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

Property for Strings

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

Property for integers

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.

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


Solution with Class<T> as argument

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.");
        }
    }
}

Used like so:

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);

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.

Bicyclic answered 5/7, 2014 at 18:49 Comment(3)
I have this code before but I tried to avoid it. I think I'll parse string on higher level and then call method setValue(T input) with type T. But thanks.Walter
You would have to pass the class of T as argument to constructor of Property. Then you could compare this class against some known types and branch upon it - but it is UGLY. Polumorphism is there to be used, not avoided.Bicyclic
Well, there is indeed a use case man. Imagine a simple text configuration file, XML, webservice result (whatever) gives you a <string,string> map and you actually need a single generic method, let's say <V> V GetValueByKey(string key) which gives you appropriate value for appropriate key but of a desired type - instead of the string.Endless
O
1

I found a new way by using Spring.

public static <T> T convertJsonToObject(String json, Class<T> valueType) {
    try {
        return json != null ? new ObjectMapper().readValue(json, valueType) : null;
    } catch (Exception e) {
        return null;
    }
}
private static void processValidate(Class<?> valueClass, String value) {
     Object parsedValue = Utils.convertJsonToObject(value, valueClass);
     // logic
}
Oech answered 21/5, 2021 at 6:34 Comment(0)
J
1

Another way using reflection is

public <T> T setValue(String value, Class<T> valueType) {

    try {
        return (T) valueType.getDeclaredMethod("valueOf", String.class).invoke(null, value);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}
Jane answered 13/3, 2023 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.