1/*
2 * Copyright (c) 2006, 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 6392818
27 * @summary Tests Elements.isDeprecated(Element)
28 * @author  Joseph D. Darcy
29 * @library /tools/javac/lib
30 * @modules java.compiler
31 *          jdk.compiler
32 * @build JavacTestingAbstractProcessor
33 * @compile TestDeprecation.java
34 * @compile -processor TestDeprecation -proc:only Dep1.java
35 * @compile Dep1.java
36 * @compile -processor TestDeprecation -proc:only Dep1 TestDeprecation.java
37 */
38
39import java.util.Set;
40import java.util.HashSet;
41import java.util.Arrays;
42import javax.annotation.processing.*;
43import javax.lang.model.SourceVersion;
44import javax.lang.model.element.*;
45import javax.lang.model.util.*;
46import static javax.tools.Diagnostic.Kind.*;
47import java.io.Writer;
48
49/**
50 * This processor verifies that the information returned by
51 * getElementsAnnotatedWith is consistent with the expected results
52 * stored in an AnnotatedElementInfo annotation.
53 */
54public class TestDeprecation extends JavacTestingAbstractProcessor {
55
56    public boolean process(Set<? extends TypeElement> annotations,
57                           RoundEnvironment roundEnv) {
58        boolean failure = false;
59        if (!roundEnv.processingOver()) {
60            DeprecationChecker deprecationChecker = new DeprecationChecker();
61
62            for(Element element: roundEnv.getRootElements() ) {
63                System.out.println("\nRoot Element: " + element.getSimpleName());
64                failure = deprecationChecker.scan(element);
65            }
66
67            if (failure)
68                processingEnv.getMessager().printMessage(ERROR, "Deprecation mismatch found!");
69        }
70        return true;
71    }
72
73    private class DeprecationChecker extends ElementScanner<Boolean,Void> {
74        private Elements elementUtils;
75        private boolean failure;
76        DeprecationChecker() {
77            super(false);
78            elementUtils = processingEnv.getElementUtils();
79            failure = false;
80        }
81
82        @Override
83        public Boolean scan(Element e, Void p) {
84            boolean expectedDeprecation = false;
85            ExpectedDeprecation tmp = e.getAnnotation(ExpectedDeprecation.class);
86            if (tmp != null)
87                expectedDeprecation = tmp.value();
88            boolean actualDeprecation = elementUtils.isDeprecated(e);
89
90            System.out.printf("\tVisiting %s\t%s%n", e.getKind(), e.getSimpleName());
91
92            if (expectedDeprecation != actualDeprecation) {
93                failure = true;
94                java.io.StringWriter w = new java.io.StringWriter();
95                elementUtils.printElements(w, e);
96                System.out.printf("For the deprecation of %n\t%s\t, expected %b, got %b.%n",
97                                  w.getBuffer().toString(),
98                                  expectedDeprecation, actualDeprecation);
99            }
100            super.scan(e, p);
101            return failure;
102        }
103    }
104}
105