2

I have the following class:

public final class Param<T>
{
    private final String name;
    private T value;
    public Param(String name)
    {
        this.name = name;
    }   

    public void setValue(T value) { this.value = value;}
    public T getValue() { return value; }
}

I would instantiate it in the following way:

Param<Long> foo = new Param<>("bar");
Param<String> baz = new Param<>("foo");

I want to add a public boolean validate(String value) method to this class, which would do something like the following:

public boolean validate(String value)
{
    try
    {
        if (this.value instanceof String)
            return true;

        if (this.value instanceof Long)
            this.value = (T) Long.valueOf(value);
        else if (this.value instanceof Integer)
            this.value = (T) Integer.valueOf(value);
        //Snip.. a few more such conditions for Double, Float, and Booleans
        return true;
    }
    catch (NumberFormatException e)
    {
        return false;
    }
}

My question is, will the above method work, for correctly determining the 'type' of T and for correctly casting string value to T value? Or should I use a different approach for validating the parameters?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ali
  • 261,656
  • 265
  • 575
  • 769
  • Duplicate of [SO: get type of a generic parameter in java with reflection](http://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection)? – Mr. Polywhirl Mar 05 '14 at 19:43
  • @Mr.Polywhirl That question is completely different, mine doesn't have anything to do with reflection. In fact, I want to do it without reflection. – Ali Mar 05 '14 at 19:43
  • Do you seriously intend on manually checking every single type known to humankind? – 2rs2ts Mar 05 '14 at 19:46
  • @2rs2ts No, I only have a few possible types that i'll be assigning to this class, so I would have only checked against them. – Ali Mar 05 '14 at 19:54
  • Have you tried to actually use this class? Since I think it's rather useless: you should delete the type parameter `T` altogether. It isn't going to serve any purpose. – Marko Topolnik Mar 05 '14 at 20:08

2 Answers2

5

Java generic types are not reified, meaning you need to have some other mechanism for keeping track of them if you need runtime information about them.

A common approach is to pass in a Class<? extends T> token in the constructor:

public final class Param<T>
{
    private final String name;
    private T value;
    private Class<? extends T> type;
    public Param(String name, Class<? extends T> type)
    {
        this.name = name;
        this.type = type;
    }

    public boolean isString() {
        return String.class.isAssignableFrom(type);
    } 
}
Daniel
  • 4,481
  • 14
  • 34
3

So, uh, you want to:

if (this.value instanceof Long)
    this.value = (T) Long.valueOf(value);

If value is a Long, convert it to a Long, cast it to a Long, and store it back in value. What?

No. No, I don't think that's going to work.

David Conrad
  • 15,432
  • 2
  • 42
  • 54