10

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?

5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • 1
    According to Effective JAVA, The better approach will be to suppress the warnings on the field itself. But you can annotate the class itself. – Aditya Peshave Dec 22 '14 at 15:45
  • So basically, the suppress is the way to go? And there is no problem in the future release of JDK? – 5YrsLaterDBA Dec 22 '14 at 16:01
  • 2
    Why not `public class ProcessParameter>()`, and then simply add `` to your methods? – GriffeyDog Dec 22 '14 at 16:02
  • `And there is no problem in the future release of JDK?` - according to the [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.8) there _might_ be problems with future JDK releases when using raw rypes and if you'd not use them you'd not need to suppress those warnings. – Thomas Dec 22 '14 at 16:11
  • @GriffeyDog, That means all my classes using the Parameter will be generic classes. Then half of the classes in the project will be generic classes. Is this a good practice? – 5YrsLaterDBA Dec 22 '14 at 16:13
  • Classes that _make use of_ a generic class are not necessarily themselves generic. Your `Parameter` interface and relates classes seem to make sense for generics, so places that use them should use them as generics. I can't really comment on other classes in your project without seeing them, but I would say that using `@SuppressWarnings` all over the place is probably _not_ a good practice. – GriffeyDog Dec 22 '14 at 17:02

1 Answers1

1

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?

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157