1

I'm having hard time googling this. I found a lot of articles but I can't still solve my problem.

Here is my code:

List<MyMainClass> mySource = (List<MyMainClass>) session.getAttribute("myAttribute");

the session.getAttribute("myAttribute") may return List<MyObject1> or List<MyObject2>. Both MyObject1 and MyObject2 are subclasses of MyMainClass Now I have 2 functions. The first one accepts List<MyObject1> and the other one accepts List<MyObject2>. Now Im getting error in eclipse

The method myMethod1(List<MyObject1>) in the type MyAction is not applicable for the arguments (List<MyMainClass>)

1
  • Can you add the code where myMethod1 is defined, and how you are invoking it? It seems myMethod1 needs a MyObject1 list, but you are passing it a MyMainClass list, which will not be accepted. Commented Oct 30, 2012 at 3:23

4 Answers 4

1

You can't safely store different generic types with the same erasure (List) in a session attribute. So: don't do it.

Instead either refactor your code so that it is compatible regardless of the list type of that session attribute. This may be hard, but long-term seems less smelly to me. IME, it's generally a smell of poor design when you need to store two things of potentially different type in a single variable.

You could alternately use two different session attributes so that you know which more-specific list type to cast to.

List<MyObject1> mySource1 = (List<MyObject1>) session.getAttribute("myAttribute1");
if (mySource1 == null) {
    List<MyObject2> mySource2 = (List<MyObject2>) session.getAttribute("myAttribute2");
    if (mySource2 == null) {
        // ???
    } else {
        // rock and roll
        myMethod2(mySource2);
    }
} else {
    // proceed
    myMethod1(mySource1);
}

If you take the latter approach, I'd recommend writing a wrapper object or method that manages those details for you.

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

Comments

0

That is correct. Generics provides only compile time safeguarding. You are trying to pass a more generic type of list to a more specific type of function of type list.

Understand that an instance of List<MyMainClass> can hold instances of both MyObject1 and MyObject2. When you call function1(List<MyObject1>) this function expects all the elements the list to be of type MyObject1. Therefore you cannot pass a list of type List<MyMainClass>.

You need to have distinguished variables to store different types of lists.

Comments

0

As advised, this is bit tricky. If you really want to do it, I think you can do this as below:

 List<MyMainClass> mySource = 
                           (List<MyMainClass>) session.getAttribute("myAttribute");
 if(mySource != null && !mySource.isEmpty()){
      //Get the first element and check the type
      MyMainClass firstElement = mySource.get(0);
      if(firstElement instanceof MyObject1){
          List<MyObject1> mySourceObj1 = (List<MyObject1>)mySource;
          myMethod1(mySourceObj1);
      }else{
          List<MyObject3> mySourceObj2 = (List<MyObject2>)mySource;
          myMethod1(mySourceObj2);
      }
  } 

Comments

0

I would suggest you change the way you declare mySource and your function signature if its possible .

Declare this way:

    List<? extends MyMainClass> mySource = (List<MyMainClass>) 

session.getAttribute("myAttribute");

And change your function signature from

returnType fun(List<Object1> object1List){
}

to

returnType fun(List<? extends MyMainClass> object1List){
}

Comments

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.