Why is code that returns a genericized Map generating a compiler warning when assigned to a generic Map?
Asked Answered
L

2

1

I have a method with this signature:

   protected final Map<String, Object> buildOutputMappings(
                                 AbstractDataObject ado, MDBase md)

And called with this method (in a subclass):

   Map<String, Object> params = buildOutputMappings(ra, md);

I get this compiler warning:

      Warning:Warning:line (136)[unchecked] unchecked conversion
found   : java.util.Map
required: java.util.Map<java.lang.String,java.lang.Object>

Changing params to an ungenericized Map removes the compiler warning. Why is this and how can it be avoided (other than suppression)?

EDIT: This is JDK 1.5, and line 136 is the assignment statement above. Neither class is paramterized, they just have methods that return a Map of a generic type. The returned object within the method is also genericized.

EDIT: The superclass is indeed genericized, although return value has nothing to do with those generics. Here is the code of the method, although with the disclaimer that I didn't write this and I know it is ugly:

protected final Map<String, Object> buildOutputMappings(AbstractDataObject ado, MDBase md) throws DAOException {
  try {
     ....
     Map<String,Object> params = new HashMap<String, Object>(spc.getNumberInParams());
     ....
     return params;
  }
  catch (Exception e) {
     logger.undeterminedError(e);
     throw new DAOException(e.getMessage(), e);
  }
}

Here are the class declarations:

public abstract class DAOBase<T extends AbstractDataObject>

public class RoleAssignmentDAO extends DAOBase
Licentious answered 24/6, 2009 at 17:2 Comment(8)
What version of Java are you using?Forthwith
what is the line 136? is one of these two you showed?Disassemble
No warning here, tested with both Java 5 and Java 6Tieback
Could you include your return code? I have a feeling it's your return that's causing you issues.Abuse
there's something we don't know about your code. for what I can see, your code is fine. if you post your method's code here, it will help.Disassemble
is this like a puzzler challenge? we get the edit with a few more tidbits of information but not much else? general tip to question posters: the more information, the more likely someone will be able to help you!Vermination
@james, I was away from my office computer when I put in the first edit, so I didn't have the actual code in front of me to put in.Licentious
@yishai, no problem, glad i could help. i was a little confused by the first edit.Vermination
V
5

my guess is that you are not using generics correctly in the subclass, and the compiler is disabling generics for the class. thus the return type for the buildOutputMappings call is being converted to the raw type and the warning is being generated. is the parent class parameterized? does the subclass include types for the parent classes parameters?

In short, your error is most likely a dropped type parameter somewhere in the subclass or parent class.

Vermination answered 24/6, 2009 at 17:30 Comment(2)
+1 for being right, and I wish I could give you another +1 for being psychic.Adkins
This was the correct answer. The compiler removes generics from the super class given that the subclass doesn't use the parameter type.Licentious
A
4

I have a feeling that your actual return statement is not matching your returning the correct type as indicated by your method definition. I can't be sure because you don't have the code included.

Abuse answered 24/6, 2009 at 17:9 Comment(1)
I'm almost certain you're right. I don't see anything else that could be the problem.Adkins

© 2022 - 2024 — McMap. All rights reserved.