Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 6993963 7025809 |
aoqi@0 | 27 | * @summary Project Coin: Use precise exception analysis for effectively final catch parameters |
aoqi@0 | 28 | * @library /tools/javac/lib |
aoqi@0 | 29 | * @build JavacTestingAbstractProcessor ModelChecker |
aoqi@0 | 30 | * @compile -processor ModelChecker Model01.java |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | import com.sun.source.tree.CatchTree; |
aoqi@0 | 34 | import com.sun.source.util.TreePathScanner; |
aoqi@0 | 35 | import com.sun.source.util.Trees; |
aoqi@0 | 36 | import com.sun.source.util.TreePath; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import java.util.Set; |
aoqi@0 | 39 | import javax.annotation.processing.RoundEnvironment; |
aoqi@0 | 40 | import javax.annotation.processing.SupportedAnnotationTypes; |
aoqi@0 | 41 | import javax.lang.model.element.Element; |
aoqi@0 | 42 | import javax.lang.model.element.ElementKind; |
aoqi@0 | 43 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 44 | import javax.lang.model.type.TypeMirror; |
aoqi@0 | 45 | import javax.lang.model.type.TypeKind; |
aoqi@0 | 46 | import javax.lang.model.type.UnionType; |
aoqi@0 | 47 | import javax.lang.model.type.UnknownTypeException; |
aoqi@0 | 48 | import javax.lang.model.util.SimpleTypeVisitor6; |
aoqi@0 | 49 | import javax.lang.model.util.SimpleTypeVisitor7; |
aoqi@0 | 50 | |
aoqi@0 | 51 | @SupportedAnnotationTypes("Check") |
aoqi@0 | 52 | public class ModelChecker extends JavacTestingAbstractProcessor { |
aoqi@0 | 53 | |
aoqi@0 | 54 | @Override |
aoqi@0 | 55 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
aoqi@0 | 56 | if (roundEnv.processingOver()) |
aoqi@0 | 57 | return true; |
aoqi@0 | 58 | |
aoqi@0 | 59 | Trees trees = Trees.instance(processingEnv); |
aoqi@0 | 60 | |
aoqi@0 | 61 | TypeElement testAnno = elements.getTypeElement("Check"); |
aoqi@0 | 62 | for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { |
aoqi@0 | 63 | TreePath p = trees.getPath(elem); |
aoqi@0 | 64 | new MulticatchParamTester(trees).scan(p, null); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | return true; |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | class MulticatchParamTester extends TreePathScanner<Void, Void> { |
aoqi@0 | 70 | Trees trees; |
aoqi@0 | 71 | |
aoqi@0 | 72 | public MulticatchParamTester(Trees trees) { |
aoqi@0 | 73 | super(); |
aoqi@0 | 74 | this.trees = trees; |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | @Override |
aoqi@0 | 78 | public Void visitCatch(CatchTree node, Void p) { |
aoqi@0 | 79 | TreePath param = new TreePath(getCurrentPath(), node.getParameter()); |
aoqi@0 | 80 | Element ex = trees.getElement(param); |
aoqi@0 | 81 | validateUnionTypeInfo(ex); |
aoqi@0 | 82 | if (ex.getSimpleName().contentEquals("ex")) { |
aoqi@0 | 83 | assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); |
aoqi@0 | 84 | for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) { |
aoqi@0 | 85 | Member m = e.getAnnotation(Member.class); |
aoqi@0 | 86 | if (m != null) { |
aoqi@0 | 87 | assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | return super.visitCatch(node, p); |
aoqi@0 | 93 | } |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | private void validateUnionTypeInfo(Element ex) { |
aoqi@0 | 97 | UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class); |
aoqi@0 | 98 | assertTrue(ut != null, "UnionType annotation must be present"); |
aoqi@0 | 99 | |
aoqi@0 | 100 | TypeMirror expectedUnionType = ex.asType(); |
aoqi@0 | 101 | assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected"); |
aoqi@0 | 102 | |
aoqi@0 | 103 | try { |
aoqi@0 | 104 | new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedUnionType); |
aoqi@0 | 105 | throw new RuntimeException("Expected UnknownTypeException not thrown."); |
aoqi@0 | 106 | } catch (UnknownTypeException ute) { |
aoqi@0 | 107 | ; // Expected |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | UnionType unionType = new SimpleTypeVisitor<UnionType, Void>(){ |
aoqi@0 | 111 | @Override |
aoqi@0 | 112 | protected UnionType defaultAction(TypeMirror e, Void p) {return null;} |
aoqi@0 | 113 | |
aoqi@0 | 114 | @Override |
aoqi@0 | 115 | public UnionType visitUnion(UnionType t, Void p) {return t;} |
aoqi@0 | 116 | }.visit(expectedUnionType); |
aoqi@0 | 117 | assertTrue(unionType != null, "Must get a non-null union type."); |
aoqi@0 | 118 | |
aoqi@0 | 119 | assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match"); |
aoqi@0 | 120 | |
aoqi@0 | 121 | String[] typeNames = ut.value(); |
aoqi@0 | 122 | for(int i = 0; i < typeNames.length; i++) { |
aoqi@0 | 123 | TypeMirror typeFromAnnotation = nameToType(typeNames[i]); |
aoqi@0 | 124 | assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), |
aoqi@0 | 125 | "Types were not equal."); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | private TypeMirror nameToType(String name) { |
aoqi@0 | 130 | return elements.getTypeElement(name).asType(); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | private static void assertTrue(boolean cond, String msg) { |
aoqi@0 | 134 | assertionCount++; |
aoqi@0 | 135 | if (!cond) |
aoqi@0 | 136 | throw new AssertionError(msg); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | static int assertionCount = 0; |
aoqi@0 | 140 | } |