mcimadamore@735: /* mcimadamore@735: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. mcimadamore@735: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@735: * mcimadamore@735: * This code is free software; you can redistribute it and/or modify it mcimadamore@735: * under the terms of the GNU General Public License version 2 only, as mcimadamore@735: * published by the Free Software Foundation. mcimadamore@735: * mcimadamore@735: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@735: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@735: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@735: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@735: * accompanied this code). mcimadamore@735: * mcimadamore@735: * You should have received a copy of the GNU General Public License version mcimadamore@735: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@735: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@735: * mcimadamore@735: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@735: * or visit www.oracle.com if you need additional information or have any mcimadamore@735: * questions. mcimadamore@735: */ mcimadamore@735: mcimadamore@735: /* mcimadamore@735: * @test mcimadamore@735: * @bug 6993963 mcimadamore@735: * @summary Project Coin: Use precise exception analysis for effectively final catch parameters mcimadamore@735: * @library ../../lib mcimadamore@735: * @build JavacTestingAbstractProcessor ModelChecker mcimadamore@735: * @compile -processor ModelChecker Model01.java mcimadamore@735: */ mcimadamore@735: mcimadamore@735: import com.sun.source.tree.VariableTree; mcimadamore@735: import com.sun.source.util.TreePathScanner; mcimadamore@735: import com.sun.source.util.Trees; mcimadamore@735: import com.sun.source.util.TreePath; mcimadamore@735: mcimadamore@735: import java.util.Set; mcimadamore@735: import javax.annotation.processing.RoundEnvironment; mcimadamore@735: import javax.annotation.processing.SupportedAnnotationTypes; mcimadamore@735: import javax.lang.model.element.Element; mcimadamore@735: import javax.lang.model.element.ElementKind; mcimadamore@735: import javax.lang.model.element.TypeElement; mcimadamore@735: mcimadamore@735: @SupportedAnnotationTypes("Check") mcimadamore@735: public class ModelChecker extends JavacTestingAbstractProcessor { mcimadamore@735: mcimadamore@735: @Override mcimadamore@735: public boolean process(Set annotations, RoundEnvironment roundEnv) { mcimadamore@735: if (roundEnv.processingOver()) mcimadamore@735: return true; mcimadamore@735: mcimadamore@735: Trees trees = Trees.instance(processingEnv); mcimadamore@735: mcimadamore@735: TypeElement testAnno = elements.getTypeElement("Check"); mcimadamore@735: for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { mcimadamore@735: TreePath p = trees.getPath(elem); mcimadamore@735: new MulticatchParamTester(trees).scan(p, null); mcimadamore@735: } mcimadamore@735: return true; mcimadamore@735: } mcimadamore@735: mcimadamore@735: class MulticatchParamTester extends TreePathScanner { mcimadamore@735: Trees trees; mcimadamore@735: mcimadamore@735: public MulticatchParamTester(Trees trees) { mcimadamore@735: super(); mcimadamore@735: this.trees = trees; mcimadamore@735: } mcimadamore@735: mcimadamore@735: @Override mcimadamore@735: public Void visitVariable(VariableTree node, Void p) { mcimadamore@735: Element ex = trees.getElement(getCurrentPath()); mcimadamore@735: if (ex.getSimpleName().contentEquals("ex")) { mcimadamore@735: assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); mcimadamore@735: for (Element e : types.asElement(ex.asType()).getEnclosedElements()) { mcimadamore@735: Member m = e.getAnnotation(Member.class); mcimadamore@735: if (m != null) { mcimadamore@735: assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); mcimadamore@735: } mcimadamore@735: } mcimadamore@735: assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount); mcimadamore@735: } mcimadamore@735: return super.visitVariable(node, p); mcimadamore@735: } mcimadamore@735: } mcimadamore@735: mcimadamore@735: private static void assertTrue(boolean cond, String msg) { mcimadamore@735: assertionCount++; mcimadamore@735: if (!cond) mcimadamore@735: throw new AssertionError(msg); mcimadamore@735: } mcimadamore@735: mcimadamore@735: static int assertionCount = 0; mcimadamore@735: }