DeprecatedTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8042261
27 * @summary Checking what attribute is generated by annotation Deprecated
28 *          or javadoc deprecated for field, method, class(inner/local), interface.
29 * @library /tools/lib /tools/javac/lib ../lib
30 * @modules jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.main
32 *          jdk.compiler/com.sun.tools.javac.util
33 *          jdk.jdeps/com.sun.tools.classfile
34 *          jdk.jdeps/com.sun.tools.javap
35 * @build ToolBox TestBase TestResult InMemoryFileManager
36 * @run main DeprecatedTest
37 */
38
39import com.sun.tools.classfile.Attribute;
40import com.sun.tools.classfile.ClassFile;
41import com.sun.tools.classfile.ConstantPoolException;
42import com.sun.tools.classfile.Deprecated_attribute;
43import com.sun.tools.classfile.Field;
44import com.sun.tools.classfile.InnerClasses_attribute;
45import com.sun.tools.classfile.InnerClasses_attribute.Info;
46import com.sun.tools.classfile.Method;
47
48import javax.tools.JavaFileObject;
49import java.io.IOException;
50import java.util.Map;
51
52public class DeprecatedTest extends TestResult {
53
54    private static final String[] sources = new String[]{
55            "@Deprecated public class deprecated {\n"
56            + "@Deprecated class deprecatedInner01 {}\n"
57            + "@Deprecated interface deprecatedInner02 {}\n"
58            + "@Deprecated enum deprecatedInner03 {}\n"
59            + "@Deprecated @interface deprecatedInner04 {}\n"
60            + "class notDeprecatedInner01 {}\n"
61            + "interface notDeprecatedInner02 {}\n"
62            + "enum notDeprecatedInner03 {}\n"
63            + "@interface notDeprecatedInner04 {}\n"
64            + "@Deprecated public void deprecated() {}\n"
65            + "@Deprecated public int deprecated;\n"
66            + "public void notDeprecated() {}\n"
67            + "public int notDeprecated;\n"
68            + "public void f() {\n"
69            + "    @Deprecated class deprecatedLocal {\n"
70            + "        @Deprecated int deprecated;\n"
71            + "        @Deprecated void deprecated() {}\n"
72            + "        int notDeprecated;\n"
73            + "        void notDeprecated(){}\n"
74            + "    }\n"
75            + "    class notDeprecatedLocal {\n"
76            + "        @Deprecated int deprecated;\n"
77            + "        @Deprecated void deprecated() {}\n"
78            + "        int notDeprecated;\n"
79            + "        void notDeprecated(){}\n"
80            + "    }}\n"
81            + "}",
82            "@Deprecated public interface deprecated {\n"
83            + "@Deprecated class deprecatedInner01 {}\n"
84            + "@Deprecated interface deprecatedInner02 {}\n"
85            + "@Deprecated enum deprecatedInner03 {}\n"
86            + "@Deprecated @interface deprecatedInner04 {}\n"
87            + "class notDeprecatedInner01 {}\n"
88            + "interface notDeprecatedInner02 {}\n"
89            + "enum notDeprecatedInner03 {}\n"
90            + "@interface notDeprecatedInner04 {}\n"
91            + "@Deprecated void deprecated01();\n"
92            + "void notDeprecated01();\n"
93            + "@Deprecated default void deprecated02() {}\n"
94            + "default void notDeprecated02() {}\n"
95            + "@Deprecated int deprecated = 0;\n"
96            + "int notDeprecated = 0;\n"
97            + "}",
98            "@Deprecated public enum deprecated {\n"
99            + "@Deprecated deprecated, notDeprecated;\n"
100            + "@Deprecated class deprecatedInner01 {}\n"
101            + "@Deprecated interface deprecatedInner02 {}\n"
102            + "@Deprecated enum deprecatedInner03 {}\n"
103            + "@Deprecated @interface deprecatedInner04 {}\n"
104            + "class notDeprecatedInner01 {}\n"
105            + "interface notDeprecatedInner02 {}\n"
106            + "enum notDeprecatedInner03 {}\n"
107            + "@interface notDeprecatedInner04 {}\n"
108            + "@Deprecated public void deprecated() {}\n"
109            + "public void notDeprecated() {}\n"
110            + "public void f() {\n"
111            + "    @Deprecated class deprecatedLocal {\n"
112            + "        @Deprecated int deprecated;\n"
113            + "        @Deprecated void deprecated() {}\n"
114            + "        int notDeprecated;\n"
115            + "        void notDeprecated(){}\n"
116            + "    }\n"
117            + "    class notDeprecatedLocal {\n"
118            + "        @Deprecated int deprecated;\n"
119            + "        @Deprecated void deprecated() {}\n"
120            + "        int notDeprecated;\n"
121            + "        void notDeprecated(){}\n"
122            + "    }}\n"
123            + "}",
124            "@Deprecated public @interface deprecated {\n"
125            + "@Deprecated class deprecatedInner01 {}\n"
126            + "@Deprecated interface deprecatedInner02 {}\n"
127            + "@Deprecated enum deprecatedInner03 {}\n"
128            + "@Deprecated @interface deprecatedInner04 {}\n"
129            + "class notDeprecatedInner01 {}\n"
130            + "interface notDeprecatedInner02 {}\n"
131            + "enum notDeprecatedInner03 {}\n"
132            + "@interface notDeprecatedInner04 {}\n"
133            + "@Deprecated int deprecated() default 0;\n"
134            + "int notDeprecated() default 0;\n"
135            + "@Deprecated int deprecated = 0;\n"
136            + "int notDeprecated = 0;\n"
137            + "}",
138            "public class notDeprecated {\n"
139            + "@Deprecated class deprecatedInner01 {}\n"
140            + "@Deprecated interface deprecatedInner02 {}\n"
141            + "@Deprecated enum deprecatedInner03 {}\n"
142            + "@Deprecated @interface deprecatedInner04 {}\n"
143            + "class notDeprecatedInner01 {}\n"
144            + "interface notDeprecatedInner02 {}\n"
145            + "enum notDeprecatedInner03 {}\n"
146            + "@interface notDeprecatedInner04 {}\n"
147            + "@Deprecated public void deprecated() {}\n"
148            + "@Deprecated public int deprecated;\n"
149            + "public void notDeprecated() {}\n"
150            + "public int notDeprecated;\n"
151            + "public void f() {\n"
152            + "    @Deprecated class deprecatedLocal {\n"
153            + "        @Deprecated int deprecated;\n"
154            + "        @Deprecated void deprecated() {}\n"
155            + "        int notDeprecated;\n"
156            + "        void notDeprecated(){}\n"
157            + "    }\n"
158            + "    class notDeprecatedLocal {\n"
159            + "        @Deprecated int deprecated;\n"
160            + "        @Deprecated void deprecated() {}\n"
161            + "        int notDeprecated;\n"
162            + "        void notDeprecated(){}\n"
163            + "    }}\n"
164            + "}",
165            "public interface notDeprecated {\n"
166            + "@Deprecated class deprecatedInner01 {}\n"
167            + "@Deprecated interface deprecatedInner02 {}\n"
168            + "@Deprecated enum deprecatedInner03 {}\n"
169            + "@Deprecated @interface deprecatedInner04 {}\n"
170            + "class notDeprecatedInner01 {}\n"
171            + "interface notDeprecatedInner02 {}\n"
172            + "enum notDeprecatedInner03 {}\n"
173            + "@interface notDeprecatedInner04 {}\n"
174            + "@Deprecated void deprecated01();\n"
175            + "void notDeprecated01();\n"
176            + "@Deprecated default void deprecated02() {}\n"
177            + "default void notDeprecated02() {}\n"
178            + "@Deprecated int deprecated = 0;\n"
179            + "int notDeprecated = 0;\n"
180            + "}",
181            "public enum notDeprecated {\n"
182            + "@Deprecated deprecated, notDeprecated;\n"
183            + "@Deprecated class deprecatedInner01 {}\n"
184            + "@Deprecated interface deprecatedInner02 {}\n"
185            + "@Deprecated enum deprecatedInner03 {}\n"
186            + "@Deprecated @interface deprecatedInner04 {}\n"
187            + "class notDeprecatedInner01 {}\n"
188            + "interface notDeprecatedInner02 {}\n"
189            + "enum notDeprecatedInner03 {}\n"
190            + "@interface notDeprecatedInner04 {}\n"
191            + "@Deprecated public void deprecated() {}\n"
192            + "public void notDeprecated() {}\n"
193            + "public void f() {\n"
194            + "    @Deprecated class deprecatedLocal {\n"
195            + "        @Deprecated int deprecated;\n"
196            + "        @Deprecated void deprecated() {}\n"
197            + "        int notDeprecated;\n"
198            + "        void notDeprecated(){}\n"
199            + "    }\n"
200            + "    class notDeprecatedLocal {\n"
201            + "        @Deprecated int deprecated;\n"
202            + "        @Deprecated void deprecated() {}\n"
203            + "        int notDeprecated;\n"
204            + "        void notDeprecated(){}\n"
205            + "    }}\n"
206            + "}",
207            "public @interface notDeprecated {\n"
208            + "@Deprecated class deprecatedInner01 {}\n"
209            + "@Deprecated interface deprecatedInner02 {}\n"
210            + "@Deprecated enum deprecatedInner03 {}\n"
211            + "@Deprecated @interface deprecatedInner04 {}\n"
212            + "class notDeprecatedInner01 {}\n"
213            + "interface notDeprecatedInner02 {}\n"
214            + "enum notDeprecatedInner03 {}\n"
215            + "@interface notDeprecatedInner04 {}\n"
216            + "@Deprecated int deprecated() default 0;\n"
217            + "int notDeprecated() default 0;\n"
218            + "@Deprecated int deprecated = 0;\n"
219            + "int notDeprecated = 0;\n"
220            + "}"};
221
222    public static void main(String[] args) throws TestFailedException {
223        new DeprecatedTest().test();
224    }
225
226    public void test() throws TestFailedException {
227        try {
228            for (String src : sources) {
229                test(src);
230                test(src.replaceAll("@Deprecated", "/** @deprecated */"));
231            }
232        } catch (Exception e) {
233            addFailure(e);
234        } finally {
235            checkStatus();
236        }
237    }
238
239    private void test(String src) {
240        addTestCase(src);
241        printf("Testing test case :\n%s\n", src);
242        try {
243            Map<String, ? extends JavaFileObject> classes = compile(src).getClasses();
244            String outerClassName = classes.containsKey("deprecated")
245                    ? "deprecated"
246                    : "notDeprecated";
247            echo("Testing outer class : " + outerClassName);
248            ClassFile cf = readClassFile(classes.get(outerClassName));
249            Deprecated_attribute attr = (Deprecated_attribute)
250                    cf.getAttribute(Attribute.Deprecated);
251            testAttribute(outerClassName, attr, cf);
252            testInnerClasses(cf, classes);
253            testMethods(cf);
254            testFields(cf);
255        } catch (Exception e) {
256            addFailure(e);
257        }
258    }
259
260    private void testInnerClasses(ClassFile cf, Map<String, ? extends JavaFileObject> classes)
261            throws ConstantPoolException, IOException {
262        InnerClasses_attribute innerAttr = (InnerClasses_attribute)
263                cf.getAttribute(Attribute.InnerClasses);
264        for (Info innerClass : innerAttr.classes) {
265            String innerClassName = cf.constant_pool.
266                    getClassInfo(innerClass.inner_class_info_index).getName();
267            echo("Testing inner class : " + innerClassName);
268            ClassFile innerCf = readClassFile(classes.get(innerClassName));
269            Deprecated_attribute attr = (Deprecated_attribute)
270                    innerCf.getAttribute(Attribute.Deprecated);
271            String innerClassSimpleName = innerClass.getInnerName(cf.constant_pool);
272            testAttribute(innerClassSimpleName, attr, innerCf);
273            if (innerClassName.contains("Local")) {
274                testMethods(innerCf);
275                testFields(innerCf);
276            }
277        }
278    }
279
280    private void testMethods(ClassFile cf)
281            throws ConstantPoolException {
282        for (Method m : cf.methods) {
283            String methodName = cf.constant_pool.getUTF8Value(m.name_index);
284            echo("Testing method : " + methodName);
285            Deprecated_attribute attr = (Deprecated_attribute)
286                    m.attributes.get(Attribute.Deprecated);
287            testAttribute(methodName, attr, cf);
288        }
289    }
290
291    private void testFields(ClassFile cf) throws ConstantPoolException {
292        for (Field f : cf.fields) {
293            String fieldName = cf.constant_pool.getUTF8Value(f.name_index);
294            echo("Testing field : " + fieldName);
295            Deprecated_attribute attr = (Deprecated_attribute)
296                    f.attributes.get(Attribute.Deprecated);
297            testAttribute(fieldName, attr, cf);
298        }
299    }
300
301    private void testAttribute(String name, Deprecated_attribute attr, ClassFile cf)
302            throws ConstantPoolException {
303        if (name.contains("deprecated")) {
304            testDeprecatedAttribute(name, attr, cf);
305        } else {
306            checkNull(attr, name + " should not have deprecated attribute");
307        }
308    }
309
310    private void testDeprecatedAttribute(String name, Deprecated_attribute attr, ClassFile cf)
311            throws ConstantPoolException {
312        if (checkNotNull(attr, name + " must have deprecated attribute")) {
313            checkEquals(0, attr.attribute_length,
314                    "attribute_length should equal to 0");
315            checkEquals("Deprecated",
316                    cf.constant_pool.getUTF8Value(attr.attribute_name_index),
317                    name + " attribute_name_index");
318        }
319    }
320}
321