Re: Generics: SupressWarnings("unchecked"). Why, and why not in java.util.ArrayList

Discussion in 'Java' started by Mayeul, Oct 21, 2009.

  1. Mayeul

    Mayeul Guest

    phi wrote:
    > Hello
    >
    > I have a problem with generics, which I can solve adding
    > '@SupressWarnings("unchecked")' before
    > the methods.


    Now that doesn't actually 'solve' anything, does it?

    Unless if you meant the problem is that you don't want warnings that are
    fully analyzed and understood to flood your project and risk making you
    miss an unnoticed warning. Which I consider this SupressWarnings is
    precisely meant to solve.

    Also, note that you can annotate a local variable declaration instead of
    the entire method if you prefer:

    @SupressWarnings("unchecked")
    E[] array = (E[])new Object[length];

    > I have seen, that the java.util.ArrayList uses a
    > similar code and does not use this
    > warning supression.


    And thus is the warning not suppressed unless your environment
    development is set up to ignore all these warnings regardless of the
    presence or absence of this annotation. Sometimes, often when dealing
    with arrays, this warning is inevitable.

    You'll notice that no warnings are ever expressed when browsing the JRE
    base library source code in Eclipse. I would expect differently if I was
    to actually import this source code for audit or maintenance, or if I
    were to just plainly copy/paste it in a class of mine.

    > What could be the problem in the following selfmade List?


    There is a risk the objects returned by the List and its Iterator do not
    actually satisfy the generics constraint defined for them.
    The compiler reported an error before you casted these Objects to (T),
    and now it warns you that it is unable to verify type integrity and that
    it will be done at runtime.

    I didn't monitor your code to verify whether it can actually happen. The
    compiler can't do that and it just warns you it can't tell.

    --
    Mayeul
    Mayeul, Oct 21, 2009
    #1
    1. Advertising

  2. Mayeul

    Lew Guest

    Re: Generics: SupressWarnings("unchecked"). Why, and why not injava.util.ArrayList

    Mayeul wrote:
    >> Unless if you meant the problem is that you don't want warnings that are
    >> fully analyzed and understood to flood your project and risk making you
    >> miss an unnoticed warning. Which I consider this SupressWarnings [sic] is
    >> precisely meant to solve.

    >


    That is not what 'SuppressWarnings' is meant to solve, precisely.

    'SuppressWarnings( "unchecked" )' is not meant as a hack to let you
    avoid analyzing and solving your type errors.

    Joshua Bloch, in the seminal /Effective Java/, free chapter on
    generics available from
    <http://java.sun.com/docs/books/effective/>,
    points out (in that free chapter) that you use 'SuppressWarnings
    ( "unchecked" )' in those rare cases when you cannot make generics
    completely work, but you can prove that there will be no type errors.
    (This happens, for example, when you mix generics and arrays.) That
    is a far, far cry from use when you cannot prove the lack of error but
    are too lazy or careless to fix it.

    Mr. Bloch advises that 'SuppressWarnings( "unchecked" )' should always
    carry source comments explaining why type safety is assured despite
    the suppression.

    Using 'SuppressWarnings( "unchecked" )' without that documented
    analysis is an invitation to 'ClassCastException' and other errors.

    --
    Lew
    Lew, Oct 21, 2009
    #2
    1. Advertising

  3. Mayeul

    Lew Guest

    Re: Generics: SupressWarnings("unchecked"). Why, and why not injava.util.ArrayList

    Mayeul wrote:
    >> Also, note that you can annotate a local variable declaration instead of
    >> the entire method if you prefer:

    >
    >> @SupressWarnings("unchecked")
    >> E[] array = (E[])new Object[length];

    >


    phi wrote:
    > Thanks: I have not known this.
    >


    It follows directly from the rules for annotations.
    > Annotations may be used as modifiers in any declaration,
    > whether package (§7.4), class (§8), interface, field (§8.3, §9.3),
    > method (§8.4, §9.4), parameter, constructor (§8.8),
    > or local variable (§14.4).

    - JLS s. 9.7

    It's a best practice to place annotations at the narrowest possible
    scope, especially 'SuppressWarnings'.

    It's a really good idea to review how annotations work if you plan to
    use them.

    Similarly for APIs, language constructs, tools, ...

    --
    Lew
    Lew, Oct 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    8,838
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  3. RVic
    Replies:
    19
    Views:
    1,368
  4. Andreas Leitgeb
    Replies:
    6
    Views:
    498
    Mike Schilling
    Oct 22, 2009
  5. Roedy Green
    Replies:
    2
    Views:
    450
    Mike Schilling
    Oct 23, 2009
Loading...

Share This Page