Method with generic return

Asked

Viewed 2,470 times

2

How do I make a method that returns a type informed in the parameters?

public <E> E to(E e)
{
    return (E)obj;
}

String st = to(String);
Integer it = to(Integer);

But it creates error, there’s another way to do it?

2 answers

4

  • 1

    First, your question says nothing of this. I answered what was asked. Second, without complete information I can’t help more than that. Any other attempt would be a guess since I didn’t see the rest of your code. Third, it seems that you are trying to do something impossible. I just can’t guarantee for missing information from all the classes involved. Even if you can do it somehow, it seems to be a great gambit.

  • 1

    @Leonardosnt agree with the bigown, it is always important to give as much as context to the question, in particular to ask about the real problem you are facing and not about your attempt at resolution. I gave an alternative answer (still based on the original question, not on your Pastebin code - which I assume deals with several classes in the same hierarchy), but I honestly don’t see what problem this solves that couldn’t be solved with a simple cast...

4


This can be done using Class.cast:

public <E> E to(Class<E> e)
{
    return e.cast(obj);
}

Call it that:

String st = to(String.class);
Integer it = to(Integer.class);

Example. Always remembering that your obj must be of a type compatible with the class used as argument. It is also good to point out that this is an operation "insecure", no more insecure than simply making a cast for the kind you want:

public Object to() {
    return obj;
}

...

String st = (String)to();
Integer it = (Integer)to();

So in this case I don’t see much advantage in this, but someone with more experience in Java may know some case of more practical use for this Class.cast. Anyway there it is, for reference...

P.S. This solution even works with classes whose "name" is not known at compile time. This means that you can, for example, do to(Class.forName("Foo")) and obtain a reference of the type Foo. The problem is that - if your code already knew it was going to deal with a Foo, just make a cast for Foo... If you didn’t know, this "correct" type will end up going into a more general type reference...

Correction: due to type Erasure, this solution only works with classes whose type is known at compile time, and whose generic parameter be correct, i.e.:

Class<?> classe = Class.forName("String");
String st = to(classe);`

doesn’t work.

  • Type, have as user these generic types in methods, I know that in the class is so for example Arraylist<String> but type, have how to do this in a method type String a = method<String>(); ?

  • @Leonardosnt No, although a method may have generic types (like this one <E> from your example) you can only infer it from the parameter passed, not specify it the way you want it. What you could do (is ugly, but is the "normal" in Java...) is to create a generic class with a method that respects its parameter, type: class MinhaClasse<T>{ public T metodo() {... } } and use it as MinhaClasse<String>().metodo(); (maybe saving a reference to this instance so you don’t have to recreate it every time you need to call the method). But I still don’t understand the purpose of it...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.