author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 29232 | jdk/src/java.base/share/classes/java/lang/SafeVarargs.java@1e69d3a5a305 |
permissions | -rw-r--r-- |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
1 |
/* |
29232
1e69d3a5a305
8074126: Correct @see cross-refs to the JLS in java.lang[.annotation]
darcy
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
4 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
10 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
15 |
* accompanied this code). |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
16 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
20 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
23 |
* questions. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
24 |
*/ |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
25 |
|
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
26 |
package java.lang; |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
27 |
|
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
28 |
import java.lang.annotation.*; |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
29 |
|
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
30 |
/** |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
31 |
* A programmer assertion that the body of the annotated method or |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
32 |
* constructor does not perform potentially unsafe operations on its |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
33 |
* varargs parameter. Applying this annotation to a method or |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
34 |
* constructor suppresses unchecked warnings about a |
9671
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
35 |
* <i>non-reifiable</i> variable arity (vararg) type and suppresses |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
36 |
* unchecked warnings about parameterized array creation at call |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
37 |
* sites. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
38 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
39 |
* <p> In addition to the usage restrictions imposed by its {@link |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
40 |
* Target @Target} meta-annotation, compilers are required to implement |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
41 |
* additional usage restrictions on this annotation type; it is a |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
42 |
* compile-time error if a method or constructor declaration is |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
43 |
* annotated with a {@code @SafeVarargs} annotation, and either: |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
44 |
* <ul> |
9671
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
45 |
* <li> the declaration is a fixed arity method or constructor |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
46 |
* |
9671
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
47 |
* <li> the declaration is a variable arity method that is neither |
25163
a7918e311ea4
8048014: Update java.lang.SafeVararags for private methods
darcy
parents:
16026
diff
changeset
|
48 |
* {@code static} nor {@code final} nor {@code private}. |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
49 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
50 |
* </ul> |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
51 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
52 |
* <p> Compilers are encouraged to issue warnings when this annotation |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
53 |
* type is applied to a method or constructor declaration where: |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
54 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
55 |
* <ul> |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
56 |
* |
9671
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
57 |
* <li> The variable arity parameter has a reifiable element type, |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
58 |
* which includes primitive types, {@code Object}, and {@code String}. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
59 |
* (The unchecked warnings this annotation type suppresses already do |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
60 |
* not occur for a reifiable element type.) |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
61 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
62 |
* <li> The body of the method or constructor declaration performs |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
63 |
* potentially unsafe operations, such as an assignment to an element |
9671
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
64 |
* of the variable arity parameter's array that generates an unchecked |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
65 |
* warning. Some unsafe operations do not trigger an unchecked |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
66 |
* warning. For example, the aliasing in |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
67 |
* |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
68 |
* <blockquote><pre> |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
69 |
* @SafeVarargs // Not actually safe! |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
70 |
* static void m(List<String>... stringLists) { |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
71 |
* Object[] array = stringLists; |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
72 |
* List<Integer> tmpList = Arrays.asList(42); |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
73 |
* array[0] = tmpList; // Semantically invalid, but compiles without warnings |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
74 |
* String s = stringLists[0].get(0); // Oh no, ClassCastException at runtime! |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
75 |
* } |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
76 |
* </pre></blockquote> |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
77 |
* |
61242d00821f
7011326: Add informative example to @SafeVarargs type or language discussion
darcy
parents:
9266
diff
changeset
|
78 |
* leads to a {@code ClassCastException} at runtime. |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
79 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
80 |
* <p>Future versions of the platform may mandate compiler errors for |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
81 |
* such unsafe operations. |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
82 |
* |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
83 |
* </ul> |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
84 |
* |
10589 | 85 |
* @since 1.7 |
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7551
diff
changeset
|
86 |
* @jls 4.7 Reifiable Types |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
7551
diff
changeset
|
87 |
* @jls 8.4.1 Formal Parameters |
29232
1e69d3a5a305
8074126: Correct @see cross-refs to the JLS in java.lang[.annotation]
darcy
parents:
25859
diff
changeset
|
88 |
* @jls 9.6.4.7 @SafeVarargs |
7551
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
89 |
*/ |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
90 |
@Documented |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
91 |
@Retention(RetentionPolicy.RUNTIME) |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
92 |
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) |
dc77388d186a
7006129: Project Coin: Annotation type to reduce varargs warnings
darcy
parents:
diff
changeset
|
93 |
public @interface SafeVarargs {} |