test/tools/javac/multicatch/model/ModelChecker.java

Wed, 01 Dec 2010 11:02:38 -0800

author
bpatel
date
Wed, 01 Dec 2010 11:02:38 -0800
changeset 766
90af8d87741f
parent 735
f2048d9c666e
child 988
7ae6c0fd479b
permissions
-rw-r--r--

6851834: Javadoc doclet needs a structured approach to generate the output HTML.
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2010, 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 6993963
    27  * @summary Project Coin: Use precise exception analysis for effectively final catch parameters
    28  * @library ../../lib
    29  * @build JavacTestingAbstractProcessor ModelChecker
    30  * @compile -processor ModelChecker Model01.java
    31  */
    33 import com.sun.source.tree.VariableTree;
    34 import com.sun.source.util.TreePathScanner;
    35 import com.sun.source.util.Trees;
    36 import com.sun.source.util.TreePath;
    38 import java.util.Set;
    39 import javax.annotation.processing.RoundEnvironment;
    40 import javax.annotation.processing.SupportedAnnotationTypes;
    41 import javax.lang.model.element.Element;
    42 import javax.lang.model.element.ElementKind;
    43 import javax.lang.model.element.TypeElement;
    45 @SupportedAnnotationTypes("Check")
    46 public class ModelChecker extends JavacTestingAbstractProcessor {
    48     @Override
    49     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    50         if (roundEnv.processingOver())
    51             return true;
    53         Trees trees = Trees.instance(processingEnv);
    55         TypeElement testAnno = elements.getTypeElement("Check");
    56         for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) {
    57             TreePath p = trees.getPath(elem);
    58             new MulticatchParamTester(trees).scan(p, null);
    59         }
    60         return true;
    61     }
    63     class MulticatchParamTester extends TreePathScanner<Void, Void> {
    64         Trees trees;
    66         public MulticatchParamTester(Trees trees) {
    67             super();
    68             this.trees = trees;
    69         }
    71         @Override
    72         public Void visitVariable(VariableTree node, Void p) {
    73             Element ex = trees.getElement(getCurrentPath());
    74             if (ex.getSimpleName().contentEquals("ex")) {
    75                 assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind());
    76                 for (Element e : types.asElement(ex.asType()).getEnclosedElements()) {
    77                     Member m = e.getAnnotation(Member.class);
    78                     if (m != null) {
    79                         assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind());
    80                     }
    81                 }
    82                 assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount);
    83             }
    84             return super.visitVariable(node, p);
    85         }
    86     }
    88     private static void assertTrue(boolean cond, String msg) {
    89         assertionCount++;
    90         if (!cond)
    91             throw new AssertionError(msg);
    92     }
    94     static int assertionCount = 0;
    95 }

mercurial