langtools/test/tools/javac/processing/model/util/deprecation/TestDeprecation.java
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    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  */
       
    34 
       
    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;
       
    44 
       
    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 {
       
    52 
       
    53     public boolean process(Set<? extends TypeElement> annotations,
       
    54                            RoundEnvironment roundEnv) {
       
    55         boolean failure = false;
       
    56         if (!roundEnv.processingOver()) {
       
    57             DeprecationChecker deprecationChecker = new DeprecationChecker();
       
    58 
       
    59             for(Element element: roundEnv.getRootElements() ) {
       
    60                 System.out.println("\nRoot Element: " + element.getSimpleName());
       
    61                 failure = deprecationChecker.scan(element);
       
    62             }
       
    63 
       
    64             if (failure)
       
    65                 processingEnv.getMessager().printMessage(ERROR, "Deprecation mismatch found!");
       
    66         }
       
    67         return true;
       
    68     }
       
    69 
       
    70     private class DeprecationChecker extends ElementScanner6<Boolean,Void> {
       
    71         private Elements elementUtils;
       
    72         private boolean failure;
       
    73         DeprecationChecker() {
       
    74             super(false);
       
    75             elementUtils = processingEnv.getElementUtils();
       
    76             failure = false;
       
    77         }
       
    78 
       
    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);
       
    86 
       
    87             System.out.printf("\tVisiting %s\t%s%n", e.getKind(), e.getSimpleName());
       
    88 
       
    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     }
       
   101 
       
   102     @Override
       
   103     public SourceVersion getSupportedSourceVersion() {
       
   104         return SourceVersion.latest();
       
   105     }
       
   106 }