test/tools/javac/processing/model/util/deprecation/TestDeprecation.java

Fri, 28 Dec 2012 18:39:09 -0800

author
lana
date
Fri, 28 Dec 2012 18:39:09 -0800
changeset 1469
467e4d9281bc
parent 1448
7d34e91f66bb
parent 1466
b52a38d4536c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

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

mercurial