Thu, 04 Nov 2010 12:57:48 +0000
6993963: Project Coin: Use precise exception analysis for effectively final catch parameters
Summary: More precise rethrow analysis should be extended to effectively-final exception parameters. Multicatch parameters should be made implicitly final.
Reviewed-by: jjg, darcy
mcimadamore@735 | 1 | /* |
mcimadamore@735 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@735 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@735 | 4 | * |
mcimadamore@735 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@735 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@735 | 7 | * published by the Free Software Foundation. |
mcimadamore@735 | 8 | * |
mcimadamore@735 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@735 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@735 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@735 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@735 | 13 | * accompanied this code). |
mcimadamore@735 | 14 | * |
mcimadamore@735 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@735 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@735 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@735 | 18 | * |
mcimadamore@735 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@735 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@735 | 21 | * questions. |
mcimadamore@735 | 22 | */ |
mcimadamore@735 | 23 | |
mcimadamore@735 | 24 | /* |
mcimadamore@735 | 25 | * @test |
mcimadamore@735 | 26 | * @bug 6993963 |
mcimadamore@735 | 27 | * @summary Project Coin: Use precise exception analysis for effectively final catch parameters |
mcimadamore@735 | 28 | * @library ../../lib |
mcimadamore@735 | 29 | * @build JavacTestingAbstractProcessor ModelChecker |
mcimadamore@735 | 30 | * @compile -processor ModelChecker Model01.java |
mcimadamore@735 | 31 | */ |
mcimadamore@735 | 32 | |
mcimadamore@735 | 33 | import com.sun.source.tree.VariableTree; |
mcimadamore@735 | 34 | import com.sun.source.util.TreePathScanner; |
mcimadamore@735 | 35 | import com.sun.source.util.Trees; |
mcimadamore@735 | 36 | import com.sun.source.util.TreePath; |
mcimadamore@735 | 37 | |
mcimadamore@735 | 38 | import java.util.Set; |
mcimadamore@735 | 39 | import javax.annotation.processing.RoundEnvironment; |
mcimadamore@735 | 40 | import javax.annotation.processing.SupportedAnnotationTypes; |
mcimadamore@735 | 41 | import javax.lang.model.element.Element; |
mcimadamore@735 | 42 | import javax.lang.model.element.ElementKind; |
mcimadamore@735 | 43 | import javax.lang.model.element.TypeElement; |
mcimadamore@735 | 44 | |
mcimadamore@735 | 45 | @SupportedAnnotationTypes("Check") |
mcimadamore@735 | 46 | public class ModelChecker extends JavacTestingAbstractProcessor { |
mcimadamore@735 | 47 | |
mcimadamore@735 | 48 | @Override |
mcimadamore@735 | 49 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
mcimadamore@735 | 50 | if (roundEnv.processingOver()) |
mcimadamore@735 | 51 | return true; |
mcimadamore@735 | 52 | |
mcimadamore@735 | 53 | Trees trees = Trees.instance(processingEnv); |
mcimadamore@735 | 54 | |
mcimadamore@735 | 55 | TypeElement testAnno = elements.getTypeElement("Check"); |
mcimadamore@735 | 56 | for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { |
mcimadamore@735 | 57 | TreePath p = trees.getPath(elem); |
mcimadamore@735 | 58 | new MulticatchParamTester(trees).scan(p, null); |
mcimadamore@735 | 59 | } |
mcimadamore@735 | 60 | return true; |
mcimadamore@735 | 61 | } |
mcimadamore@735 | 62 | |
mcimadamore@735 | 63 | class MulticatchParamTester extends TreePathScanner<Void, Void> { |
mcimadamore@735 | 64 | Trees trees; |
mcimadamore@735 | 65 | |
mcimadamore@735 | 66 | public MulticatchParamTester(Trees trees) { |
mcimadamore@735 | 67 | super(); |
mcimadamore@735 | 68 | this.trees = trees; |
mcimadamore@735 | 69 | } |
mcimadamore@735 | 70 | |
mcimadamore@735 | 71 | @Override |
mcimadamore@735 | 72 | public Void visitVariable(VariableTree node, Void p) { |
mcimadamore@735 | 73 | Element ex = trees.getElement(getCurrentPath()); |
mcimadamore@735 | 74 | if (ex.getSimpleName().contentEquals("ex")) { |
mcimadamore@735 | 75 | assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); |
mcimadamore@735 | 76 | for (Element e : types.asElement(ex.asType()).getEnclosedElements()) { |
mcimadamore@735 | 77 | Member m = e.getAnnotation(Member.class); |
mcimadamore@735 | 78 | if (m != null) { |
mcimadamore@735 | 79 | assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); |
mcimadamore@735 | 80 | } |
mcimadamore@735 | 81 | } |
mcimadamore@735 | 82 | assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount); |
mcimadamore@735 | 83 | } |
mcimadamore@735 | 84 | return super.visitVariable(node, p); |
mcimadamore@735 | 85 | } |
mcimadamore@735 | 86 | } |
mcimadamore@735 | 87 | |
mcimadamore@735 | 88 | private static void assertTrue(boolean cond, String msg) { |
mcimadamore@735 | 89 | assertionCount++; |
mcimadamore@735 | 90 | if (!cond) |
mcimadamore@735 | 91 | throw new AssertionError(msg); |
mcimadamore@735 | 92 | } |
mcimadamore@735 | 93 | |
mcimadamore@735 | 94 | static int assertionCount = 0; |
mcimadamore@735 | 95 | } |