本文介绍了为什么返回通用化Map的代码在分配给通用Map时会生成编译器警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

用这个方法调用):

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

我得到这个编译器警告:

<$ p $警告:警告:行(136)[unchecked]未经检查的转换
找到:java.util.Map
必需:java.util.Map< java.lang.String ,java.lang.Object中>

将参数更改为非通用Map将删除编译器警告。为什么是这样以及如何避免(除了压制)?

编辑:这是JDK 1.5,第136行是上面的赋值语句。 ,它们只是有返回泛型类型的Map的方法。该方法中的返回对象也是通用化的。



编辑:超类的确是泛化的,尽管返回值与这些泛型没有任何关系。这里是该方法的代码,虽然有免责声明,我没有写这个,我知道它很丑:受保护的最终地图< String,Object> buildOutputMappings(AbstractDataObject ado,MDBase md)抛出DAOException {
尝试{
....
Map< String,Object> params = new HashMap< String,Object>(spc.getNumberInParams());
....
返回参数;
}
catch(Exception e){
logger.undeterminedError(e);
抛出新的DAOException(e.getMessage(),e);




$ b $ p $这些是类声明: b
$ b

  public abstract class DAOBase< T extends AbstractDataObject> 

public class RoleAssignmentDAO extends DAOBase


解决方案



简而言之,您的错误很可能是子类或父类中的某个放置类型参数。


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. , 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
解决方案

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.

这篇关于为什么返回通用化Map的代码在分配给通用Map时会生成编译器警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:00