Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid @SuppressWarnings({ "unchecked", "rawtypes" }) everywhere?

Tags:

java

generics

I have a generic Parameter interface and its ParameterImpl concrete class. The T will be a 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 ordinary classes which will use above generic Parameter type with warnings, like the one followed:

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
}
  1. For the Map<String, Parameter> params; variable case, should I use <?> or @SuppressWarnings? Why you suggested way is better?

  2. For the method cases, if I use <?> or <T extends Comparable> (in the method signature), then it will affect all other methods related. The @SuppressWarnings will cover this issue just here. So, which option I should choose? Why the option you suggested is better than others?

  3. In most of cases, the Eclipse suggests the @SuppressWarnings way. Is the @SuppressWarnings a right way or it is a temporary solution with potential bug inside?

like image 315
5YrsLaterDBA Avatar asked Dec 22 '14 15:12

5YrsLaterDBA


People also ask

What is the meaning of @SuppressWarnings unchecked?

In other words, it basically, tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

Which annotation should be used to remove warnings from compilation?

The SuppressWarning annotation is used to suppress compiler warnings for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts.

What is the use of @SuppressWarnings unchecked in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.


1 Answers

I would use <?> if the parameter types might be different. Using raw types and thus @SuppressWarnings might cause the compiler to disable generic type checks further down the line (depends on how you use those parameters) and that's probably not what you want.

Of course, you can't alter the value of an instance of Parameter<?> but that's probably not wise anyways, since you don't know the type anymore.

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

like image 91
Thomas Avatar answered Oct 06 '22 20:10

Thomas