How do I avoid @SuppressWarnings ({"unchecked", "rawtypes"}) everywhere?

I have a generic parameter interface and its specific ParameterImpl class. T will be Float, Long, Integer, or String.

public interface Parameter<T extends Comparable<T>>  {}  
public class ParameterImpl<T extends Comparable<T>> implements Parameter<T> {}

      

I have several other regular classes that will use the above generic Type Parameter with warnings , such as the following:

public class ProcessParameter() {

    Map<String, Parameter> params; //use <?> or @SuppressWarnings   ???

    //use <?> or @SuppressWarnings or <T extends Comparable>   ???
    public Parameter getParameter(String key, Map<String, Parameter> paramMap)
    {

    }

    //use <?> or @SuppressWarnings or <T extends Comparable>   ???
    public void addParameter(Parameter param)
    {

    }

    //use <?> or @SuppressWarnings or <T extends Comparable>   ???
    public Map<String, Parameter> getParameterMap()
    {

    }

    //many more cases with Parameter presents
}

      

  • In case of a variable, Map<String, Parameter> params;

    should you use <?>

    or @SuppressWarnings

    ? Why did you suggest a better way?

  • For method cases, if I use <?>

    or <T extends Comparable>

    (in the method signature) it will affect all other related methods. @SuppressWarnings

    will only close this issue here. So which option should I choose? Why is your proposed option better than others?

  • In most cases, Eclipse offers a way @SuppressWarnings

    . Is this correct or is @SuppressWarnings

    it a temporary solution with a potential bug inside?

+3


source to share


1 answer


I would use <?>

if the types of the parameters might be different. Using raw types and therefore @SuppressWarnings can cause the compiler to disable generic type checks further down the line (depending on how you use those options) and probably not what you want.

Of course, you cannot change the value of the instance Parameter<?>

, but this is probably not that tricky since you no longer know the type.



For more information on raw types, take a look here: What is a raw type and why shouldn't we use it?

+1


source







All Articles