0

My problem is that I'm trying to instantiate a list with a parameter that have the same class as my function's argument :

public <A extends CommunicationObject> List<A> Myfunction(A myObject){

    List<A> firstList;
    //do something

}

When I call the function :

List<person> persons = Myfunction(person myObject);

The first list take A as CommunicationObject and this is not what I want.

I also tried to do this :

public <A extends CommunicationObject> List<A> Myfunction(A myObject){

  List<myObject.getClass()> firstList;
    //do something
} 

but it is not allowed. Is there any chance that I can fix this ?

Update :

"person" is a subClass of "CumminicationObject". There is some attributes that exists in person and not in CommunicationObject. Actually this is just an example. What I'm trying to do is to convert a JSON to List"<"A">", and A can be "person" or other class that extends CommunicationObject.

The JSON contain the same attributes as the "A" class in List"<"A">".

In order to do the convertion, the parameter "A" in List"<"A">" have to be the same as my object Class in myfunction(A myObject).

1
  • I really don't understand your question. Is person a subtype of CommunicationObject? If no: why are you passing it to MyFunction? If yes: isn't the actual behavior identical to what you want? Commented Mar 27, 2014 at 11:39

1 Answer 1

3

Java uses type-erasure, which means your method declaration is somewhat pointless because there's no return type for the compiler to infer the type of A from.

This means that at runtime you've effectively got:

public void Myfunction(CommunicationObject myObject)

This method signature, when you think about it, is what its implementation would have to work with anyway and so your list should be a List<CommunicationObject>.

Following update to the question regarding de-serialization from JSON:

When de-serializing from JSON to a Java object you've got two choices:

  • declare the type to de-serialize into at the point of de-serialization like GSON does.
  • infer the type to de-serialize into within the JSON.

Due Java's type-erasure this is the only way to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand this answer. Due to the same type erasure, firstList is identical to List<Object> at runtime. But generics are there for compile-time safety. Of course, you cannot use any properties of A that are not defined by CommunicationObject, because the compiler cannot know them, but that does not mean that A is irrelevant. For instance, you can't do firstList.Add(new CommunicationObject()).
The list is largely irrelevant to be honest. The point is, including the generic is pointless because the compiler will complain if you wrote MyFunction("break") whether the method is declared with generics (as in the question) or without generics (as in the answer). And so using generics achieves nothing
Thank you for your interest. I resolved the problem with a similar way. It's to complicated to be exposed here. But your answer can inspire any one who got the same problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.