-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
6
-
x86
-
linux
Compile the following source tree using javac from b102 with -Xlint:
---%<--- testsuppwarnpkgs/package-info.java
@SuppressWarnings("deprecation")
package testsuppwarnpkgs;
---%<--- testsuppwarnpkgs/Main.java
package testsuppwarnpkgs;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception {
new File("").toURL();
}
}
---%<---
You get:
---%<---
.../testsuppwarnpkgs/Main.java:5: warning: [deprecation] toURL() in java.io.File has been deprecated
new File("").toURL();
1 warning
---%<---
1. Since a top-level class is "part of" a package, I would expect @SuppressWarnings on the package element to apply to it. But it seems it does not. It would be convenient if it did, e.g. if you wanted to ignore some warning irrelevant to you across an entire package, but not elsewhere in the compilation unit, or wanted to be able to compile all your sources with -Xlint and know that any exceptions are marked explicitly with annotations.
(Cf. Javadoc of SuppressWarnings: "Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements." Javadoc of PackageElement does not clarify whether it is a "container" or not.)
2. SuppressWarnings is specified to be
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
PACKAGE is not currently mentioned. So unless it is added, isn't it a bug in the compiler that it quietly accepts this annotation on a package element? I think it should be rejected. In fact it seems that more generally the compiler accepts any annotation on a package declaration regardless of its declared target kinds.
---%<--- testsuppwarnpkgs/package-info.java
@SuppressWarnings("deprecation")
package testsuppwarnpkgs;
---%<--- testsuppwarnpkgs/Main.java
package testsuppwarnpkgs;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception {
new File("").toURL();
}
}
---%<---
You get:
---%<---
.../testsuppwarnpkgs/Main.java:5: warning: [deprecation] toURL() in java.io.File has been deprecated
new File("").toURL();
1 warning
---%<---
1. Since a top-level class is "part of" a package, I would expect @SuppressWarnings on the package element to apply to it. But it seems it does not. It would be convenient if it did, e.g. if you wanted to ignore some warning irrelevant to you across an entire package, but not elsewhere in the compilation unit, or wanted to be able to compile all your sources with -Xlint and know that any exceptions are marked explicitly with annotations.
(Cf. Javadoc of SuppressWarnings: "Note that the set of warnings suppressed in a given element is a superset of the warnings suppressed in all containing elements." Javadoc of PackageElement does not clarify whether it is a "container" or not.)
2. SuppressWarnings is specified to be
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
PACKAGE is not currently mentioned. So unless it is added, isn't it a bug in the compiler that it quietly accepts this annotation on a package element? I think it should be rejected. In fact it seems that more generally the compiler accepts any annotation on a package declaration regardless of its declared target kinds.
- relates to
-
JDK-6993311 annotations on packages are not validated
- Closed
-
JDK-8280744 Allow SuppressWarnings to be used in all declaration contexts
- Resolved
-
JDK-8280745 Allow SuppressWarnings to be used in package declarations
- Closed