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

Wed, 02 Jun 2010 19:08:47 -0700

author
darcy
date
Wed, 02 Jun 2010 19:08:47 -0700
changeset 575
9a7c998bf2fc
parent 554
9d9f26857129
child 699
d2aaaec153e8
permissions
-rw-r--r--

6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
Reviewed-by: jjg

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

mercurial