Skip to content
Advertisement

Tag: generics

Java erasure with generic overloading (not overriding)

I have FinanceRequests and CommisionTransactions in my domain. If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need to be clawed back. Dont worry how exactly that is done. The class below (very bottom) makes me feel all fuzzy and warm since its succint and reuses existing code nicely. One problem Type erasure. They both

Getting the class of the components of an array

If I have how can I get T.class from array? If I do array.getClass() that gets me T[].class instead. Answer Component Type Use this: Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null. Reference: Class.getComponentType() Safe / Unsafe casting Is there a way I can cast

Why doesn’t this generic cast fail?

I’d expect this code to throw a ClassCastException: But it doesn’t. Casting String to T doesn’t fail, until I use the returned object somehow, like: Background: I created a Class which uses JAXB to unmarshal an XML file. It looks like this: Depending on whether the root-Element is an anonymous type or not, either T or JAXBElement is being returned.

Java generics super keyword

I went through these topics Generics..? Super T Bounding generics with ‘super’ keyword However, I still seem to be kind of lost with super keyword: When we declare a collection like that: shouldn’t it be the opposite – we have a list that contains some objects (of unknown type) which are parents of Number. So Object should fit (since it

java generics covariance

I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant the author states, Because ln is a List, adding a Float to it seems perfectly legal. But if ln were aliased with li, then it would break the type-safety promise implicit in the definition of li — that it is a list of integers, which is

Advertisement