Bug 186217 - Documented warning types don't work with SuppressWarnings
Summary: Documented warning types don't work with SuppressWarnings
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-09 14:26 EDT by David Witherspoon CLA
Modified: 2008-05-06 12:01 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Witherspoon CLA 2007-05-09 14:26:02 EDT
Build ID: I20070503-1400 (3.3 M7)

Steps To Reproduce:
class Foo {
   public blah() throws SomeException {  // not really thrown
      int foo;  // unused
   }
}

More information:
http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm shows the list of warning types that ought to be supported by SuppressWarnings.  There are 2 warnings output with the example code: declared exception not thrown and unused local variable.  I should be able to hide just the unused exception warning by adding @SuppressWarnings("unusedThrown") above the method.  But this isn't recognized.  Adding @SuppressWarnings("unused") hides ALL the unused* types of warnings.  I need to be able to hide the unused exception, but continue to see the unused local variable warning.
Comment 1 Olivier Thomann CLA 2007-05-09 15:30:05 EDT
This would mean "unusedThrown" should be added in the list of supported tokens by SuppressWarnings.
Too late for 3.3.
Comment 2 David Witherspoon CLA 2007-05-10 08:14:10 EDT
The list referenced by the http link shows a BUNCH of warning types that should be supported.  While this bug report targets just one, the entire list should be supported.
Comment 3 Olivier Thomann CLA 2007-05-10 19:01:16 EDT
The list was reduced on purpose.
This would be an enhancement for 3.4.
Comment 4 Philipe Mulet CLA 2007-05-11 03:54:00 EDT
The idea behind was to decide whether a warning should be suppressable, or simply fixed. Some are rather plain things to fix, and for these obvious ones we did not introduce warning tokens. Some are arguable, and for these we generally introduced  a warning token (unless we forgot a few).

As for 'unusedException', I wouldn't go that specific, if we don't want to end up with  myriad of warning tokens.
For unused thrown exceptions, I think we should consider a solution like the one we chose for unused params (i.e. consider documented references from javadoc).

/**
 * @throw SomeException
 */

If documented, then we would stop complaining.
Comment 5 David Witherspoon CLA 2007-05-11 07:24:07 EDT
In my particular case, I have an interface method that declares 3 exceptions to be thrown.  One class that implements the interface throws all 3 of the exceptions.  But another class that implements the interface throws only 2 of them, so I constantly have this one exception declared but not thrown.  Suppressing "unused" hides that, as well as any unused locals - which we really do want to catch.

Another way to "forgive" this would be to look up the hierarchy to see if the method is inherited.  Seems like that would be a better basis for forgiving the unused exception than to rely on Javadoc.
Comment 6 Philipe Mulet CLA 2007-05-11 09:12:44 EDT
Inheritance situation is normally supported already, see compiler preferences. There is a sub-option named "Check overriding and implementing methods". If it is disabled it will behave exactly as you described (or at least it should, unless you found a bug, and if so pls enter a separate bug).

Now, one more thing, assuming your scenario looks like this:

interface I {
  void foo() throws SomeException;
}

class A implements I {
  void foo() throws SomeException {}
}

and you enabled the mentionned suboption, you will get a complaint on A#foo().
One thing you can do is remove the throws clause, since the language allows throwing less exceptions than super method.

i.e. the following is legite:

interface I {
  void foo() throws SomeException;
}

class A implements I {
  void foo() {}
}

Now, this may be troublesome for subsequent subclasses of A which cannot throw any longer... so it is likely preferrable for you to ensure the suboption is disabled.
Comment 7 David Witherspoon CLA 2007-05-14 13:03:34 EDT
Thx for the heads up on the compiler option.  I also have this scenario:

public class RS {
   protected foo() throws AEx, BEx, CEx {
      // AEx and BEx are thrown; CEx is not
   }
}

public class FS extends RS {
   protected foo() throws AEx, BEx, CEx {
      // All 3 exceptions are thrown
   }
}

I still get the unused exception warning in RS. This is a base class, so it's not an overriding method, so the compiler option has no effect. If I remove CEx from the throws clause, then we get an error, as expected.  So I think something like unusedThrown is my only way to forgive this condition.
Comment 8 Philipe Mulet CLA 2007-05-31 04:21:08 EDT
Wouldn't you rather have the javadoc clause tell the compiler that the exception is part of the API, and thus stop barfing ?
We implemented this behavior for unused parameters, which can meet the same issue. If you like it, we could also leverage this for unthrown exceptions (consistent story).

i.e.
public class RS {
   /**
    * @throws AEx
    * @throws BEx
    * @throws CEx
    */
   protected foo() throws AEx, BEx, CEx {
      // AEx and BEx are thrown; CEx is not
   }
}
Comment 9 David Witherspoon CLA 2007-05-31 07:04:34 EDT
I guess I'm a purist, so having something in the documentation that causes the compiler to forgive various warnings just kind of rubs me the wrong way.  Especially if there's something in that language itself that provides a way to handle that (annotations).

I don't like the fact there's not a standard set of annotations for these these warnings.  We use Eclipse on our dev machines but javac to actually build the code we ship.  (Silly, I know.)  So we'll get rid of the warnings in Eclipse just to have them pop out again on the build machine.

That being said...if you've solved with javadoc for unused params, and I understand the desire for consistency, then that could work just as well.
Comment 10 Philipe Mulet CLA 2007-05-31 07:52:40 EDT
Javac is not diagnosing unthrown exceptions afaik. Now, you can disable this for Eclipse compiler as well (and btw, you can run Eclipse compiler in batch/command line as well to be consistent).

The reasoning for using javadoc, is that it is part of your API specification. Forcing users to use annotation (and you have one today for suppressing warning which is going to work, just doesn't have the granularity you'd expect) is first requiring them to use 1.5 or better; which isn't a solution for everybody, besides the fact there is no standard set of warning tokens (i.e. you could imagine various compilers to define their own custom warning tokens).



Comment 11 David Witherspoon CLA 2007-05-31 09:15:26 EDT
I understand the logic...makes sense.  Thanks very much for the comments.

Possibly would want to update the doc so that the list of warning types referenced in the help (in the original bug report) is not misinterpreted as usable in annotations as I did.
Comment 12 Philipe Mulet CLA 2008-05-06 12:01:35 EDT
Closing, no further action planned.