Bug 83739 - [1.5] support @SuppressWarnings
Summary: [1.5] support @SuppressWarnings
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 enhancement with 4 votes (vote)
Target Milestone: 3.1 M7   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 69505 71968 89436 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-01-26 13:40 EST by Adam Kiezun CLA
Modified: 2005-05-11 14:08 EDT (History)
13 users (show)

See Also:


Attachments
CompilerOptions collects warning related options (2.60 KB, patch)
2005-05-06 07:24 EDT, Maxime Daniel CLA
no flags Details | Diff
SuppressWarnings annotations tests (10.47 KB, patch)
2005-05-06 07:26 EDT, Maxime Daniel CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Kiezun CLA 2005-01-26 13:40:29 EST
3.1M4
I'd like to supress certain unchecked warnings. There's a special annotation for
that, @SupressWarnings
Comment 1 Philipe Mulet CLA 2005-01-26 17:00:17 EST
The tricky problem here is that the list of official warning names is not
spec'ed anywhere (afaik). So any decision we make is likely going to cause some
grief.

Unchecked ones are "unchecked", but there is little we can determine/reserve in
general.
Comment 2 Adam Kiezun CLA 2005-01-26 17:06:21 EST
99% of time it's going to be used for the unchecked stuff anyway.
So almost all uses will look like:

@SupressWarnings({"[unchecked]"})

By allowing this you cover most ground and are compatible with javac. (although
I think, as of now, it ignores the annotation, which is completely outrageous)

Comment 3 Philipe Mulet CLA 2005-01-26 17:16:45 EST
I discovered a few extra ones "deprecation", "finally". And given we have tons
of optional ones, we are likely going to spec our names, which may conflict with
the ones other vendors pick.

But I agree this isn't a reason to at least not supported the few spec'ed ones.
Comment 4 Eric Bodden CLA 2005-02-10 03:15:46 EST
As a comment on that: I realized that SuppressWarnings removes the warning from 
the Problems view in M4. However, the code annotation (icon at the left side of 
the editor and on the resource) remain. This is a bit missleading. I think 
either both should be removed or none (or there should at leats be an option in 
the preferences).
Comment 5 Brian Thorstad CLA 2005-02-24 10:29:52 EST
A related functionality which would be *really helpful* is the ability to 
suppress warnings for just a current line (or block).

Use case - I depend upon the "unused parameter" warning for my own, and my 
team's code.  Occasionally a parameter is intentionally unused (allowing for 
extending class use).  I would like to be able to markup the parameter as 
allowed.  Perhaps something like this:

protected int calcWeight(Object data /*NO-WARN*/) {
    return 0;
}
Comment 6 Jon Nall CLA 2005-02-24 11:04:19 EST
Brian,
The SuppressWarnings annotation has ElementType.PARAMETER listed as a legal
Target, so I don't think your unused parameter is a special case syntax-wise.

i believe your code would look something like like:

protected int calcWeight(@SuppressWarning("unused parameter") Object data)
{
    return 0;
}

although, i'm sure the "unused parameter" string is incorrect. The list of
acceptable strings is what they're discussing in the earlier comments.

Comment 7 Eric Bodden CLA 2005-02-24 16:13:52 EST
Hi again. Is there still no way to remove the exclamation marks and yellow lines 
at warnings that are actually suppressed?
Comment 8 Philipe Mulet CLA 2005-02-25 06:37:55 EST
Not yet. Only workaround is to turn off offending warnings (or filter them out
in problem view), which is not optimal.
Comment 9 Olivier Thomann CLA 2005-03-29 22:01:32 EST
*** Bug 89436 has been marked as a duplicate of this bug. ***
Comment 10 Philipe Mulet CLA 2005-04-07 07:32:23 EDT
*** Bug 69505 has been marked as a duplicate of this bug. ***
Comment 11 Philipe Mulet CLA 2005-04-07 09:46:48 EDT
*** Bug 71968 has been marked as a duplicate of this bug. ***
Comment 12 Adam Kiezun CLA 2005-05-03 15:28:52 EDT
supposedly, the javac -Xlint parameters coincide with supported warning types. 
So all of: 
unchecked, deprecation, path, serial, fallthrough, finally and all should be
supported.

would need to verify with javac that it's the case (quite likely).

javac website seems out of date.
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html seems to be
missing some warnings kinds that running "javac -X" shows
Comment 13 Philipe Mulet CLA 2005-05-03 15:37:59 EDT
Also see www.javac.info.
I am planning to support many more warnings there.
Comment 14 Philipe Mulet CLA 2005-05-04 09:28:23 EDT
Released initial support to HEAD. It currently recognizes: "all", "unchecked",
"deprecation", "finally" and "serial".
Comment 15 Philipe Mulet CLA 2005-05-04 10:04:24 EDT
Maxime - pls collect testcases from related defects.
Currently, the annotation is checked for type and method/constr declarations. 
Comment 16 Maxime Daniel CLA 2005-05-06 07:24:21 EDT
Created attachment 20767 [details]
CompilerOptions collects warning related options

Created for use by test cases.
In this version, the considered options are those that are related to an
updateSeverity call into CompilerOptions.set(). There are 43 of these.
Comment 17 Maxime Daniel CLA 2005-05-06 07:26:26 EDT
Created attachment 20768 [details]
SuppressWarnings annotations tests

Additional test cases.
Some prepared for future support in the code (aka SuppressWarnings annotations
on fields).
Comment 18 Maxime Daniel CLA 2005-05-06 07:27:29 EDT
Added partial coverage, including the cases explicited in comments #14 and #15.
Re. related bugs, prepared tests where support is not available yet, added 
tests where support is available.


Comment 19 Philipe Mulet CLA 2005-05-06 10:30:54 EDT
In order to ease quickfix integration (suggest specific @SuppressWarnings), I am
proposing to add following API to CorrectionEngine.

Jeem - do you buy this API addition ?

(sorry for indentation)
/**
 * Returns a token which can be used to suppress a given warning using
<code>@SuppressWarnings</code>
 * annotation, for a given problem ID ({@link IProblem }). If a particular
problem is not suppressable, 
 * <code>null</code> will be returned. 
 * <p>
 * Note: <code>@SuppressWarnings</code> can only suppress warnings, which means
that if certain 
 * problems got promoted to ERROR using certain compiler settings ({@link
IJavaProject#setOption(String, String)}),
 * the <code>@SuppressWarnings</code> annotation will be ineffective.
 * </p>
 * <p>
 * Note: <code>@SuppressWarnings</code> can be argumented with
<code>"all"</code> so as to suppress
 * all possible warnings at once.
 * </p>
 * @param problemID
 *         the ID of a given warning to suppress
 * @return a String which can be used in <code>@SuppressWarnings</code>
annotation, or <code>null</code>
 * if unable to suppress this warning.
 * @since 3.1
 */
public static String getWarningToken(int problemID)
Comment 20 Jim des Rivieres CLA 2005-05-06 10:49:09 EDT
Philippe,  I'm fine with adding this. However, I would include a big 
disclaimer along the following lines:
<p>
<b>Note:</b> The tokens returned are not necessarily standardized across Java 
compilers. If you were to use one of these tokens in an @SuppressWarnings 
annotation in the Java source code, the effects (if any) may vary from 
compiler to compiler.
</p>
Comment 21 Philipe Mulet CLA 2005-05-06 11:02:01 EDT
Good suggestion.
Comment 22 Philipe Mulet CLA 2005-05-06 17:49:05 EDT
Jeem - one more addition in the same area: I would like to offer an option to
turn off @SuppressWarnings so as to allow clients to quickly revive all hidden
warnings without editing any code.

* COMPILER / Determine whether @SuppressWarnings is effective
*    When enabled, the @SuppressWarnings annotation can be used to suppress some
compiler warnings. When disabled, all
*    @SupressWarnings are made inoperant, so as to revive all hidden warnings at
once without editing code.
*     - option id:         "org.eclipse.jdt.core.compiler.problem.suppressWarnings"
*     - possible values:   { "enabled", "disabled" }
*     - default:           "enabled"
Comment 23 Jim des Rivieres CLA 2005-05-06 18:00:17 EDT
Philippe, Sounds good.

re: "When disabled, all @SupressWarnings are made inoperant, so as to revive 
all hidden warnings at once without editing code."

I suggest wording it in a way that makes sense for batch compiling:
"When disabled, all @SupressWarnings annotations are ignored; i.e., warnings 
are reported."
Comment 24 Philipe Mulet CLA 2005-05-06 18:06:27 EDT
Read far better indeed. Thanks.
Comment 25 Philipe Mulet CLA 2005-05-07 09:16:41 EDT
Last proposed addition: an optional warning to flag unhandled warning tokens.
Given these are not standardized for current term, this could help users to find
broken assumptions.

* COMPILER / Reporting Unhandled Warning Token for @SuppressWarnings
*    When enabled, the compiler will issue an error or a warning when 
*    encountering a token it cannot handle inside a @SuppressWarnings annotation.
*     - option id:        
"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken"
*     - possible values:   { "error", "warning", "ignore" }
*     - default:           "ignore"
Comment 26 Jim des Rivieres CLA 2005-05-07 09:26:13 EDT
Sounds good. Given that all this is moot unless the source code contains 
@SuppressWarnings annotations, would it not be more helpful if the default 
is "warning"?
Comment 27 Philipe Mulet CLA 2005-05-07 09:29:54 EDT
I thought of it, but considering there is no standard set yet, it would likely
be a bit annoying in the short term. If others have an opinion on the default, I
am open to suggestion.
Comment 28 Philipe Mulet CLA 2005-05-07 17:46:56 EDT
All changes released to HEAD.
Added AnnotationTest#test127-139.
Fixed
Comment 29 Olivier Thomann CLA 2005-05-11 14:08:59 EDT
Verified in I20050510-0010