Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics and typecasting

I have a badly created container object that holds together values of different java types(String, Boolean etc ..)

public class BadlyCreatedClass {
    public Object get(String property) {
        ...;
    }
};

And we extract values from it in this way

String myStr = (String) badlyCreatedObj.get("abc");
Date myDate = (Date) badlyCreatedObj.get("def");

I am forced to write some new code using this object and I am trying to see if there is clean way to do this. More specifically which method from the below is preferred ?

Explicit Cast

String myStr = (String) badlyCreatedObj.get("abc")
Date myDate = (Date) badlyCreatedObj.get("def");

Using generic cast

public <X> X genericGet(String property) {

}

public String getString(String property) { 
return genericGet(property); 
}

public Date getDate(String property) { 
return genericGet(property); 
}

Using Class.cast

<T> T get(String property, Class<T> cls) {
    ;
}

I have gone through several related questions on SO Java generic function: how to return Generic type , Java generic return type all of them seem to say the such typecasting is dangerous, ALthough I dont see much difference between the three, given this which method would you prefer ?

Thanks

like image 217
jack_carver Avatar asked Apr 20 '13 09:04

jack_carver


People also ask

What is generic type casting in Java?

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time. Before generics, we can store any type of objects in the collection, i.e., non-generic. Now generics force the java programmer to store a specific type of objects.

Do generics prevent type cast errors?

Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.

Can you cast to generic in Java?

The Java compiler won't let you cast a generic type across its type parameters because the target type, in general, is neither a subtype nor a supertype.

What is meant by generics in Java?

Java Generics is a set of related methods or a set of similar types. Generics allow types Integer, String, or even user-defined types to be passed as a parameter to classes, methods, or interfaces. Generics are mostly used by classes like HashSet or HashMap.


1 Answers

To give a quick answer, without going in deep about good-programming-practice...

I would use:

private <X> X genericGet(String property) {

}

public String getString(String property) { 
//... checks on property (String specific)...
Object obj = genericGet(property);
//... checks if obj is what is expected and if good return it
return obj; 
}

public Date getDate(String property) { 
//... checks on property (Date specific)...
Object obj = genericGet(property);
//... checks if obj is what is expected and if good return it
return obj
}

make notice to the private genericGet. This way I can check if the get property is what I m waiting to receive and handle it in a correct way.

I could add checks in the getString depends on the property to make sure that the answer will be a String object.

I could do other checks for getDate in property to make sure it will be a date as will be returned.

Etc...

like image 124
MrSimpleMind Avatar answered Oct 01 '22 11:10

MrSimpleMind