mcimadamore@735: /* katleman@1013: * Copyright (c) 2010, 2011, 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 darcy@1054: * @bug 6993963 7025809 mcimadamore@735: * @summary Project Coin: Use precise exception analysis for effectively final catch parameters darcy@1466: * @library /tools/javac/lib mcimadamore@735: * @build JavacTestingAbstractProcessor ModelChecker mcimadamore@735: * @compile -processor ModelChecker Model01.java mcimadamore@735: */ mcimadamore@735: jjg@988: import com.sun.source.tree.CatchTree; 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; jjg@988: import javax.lang.model.type.TypeMirror; jjg@988: import javax.lang.model.type.TypeKind; jjg@988: import javax.lang.model.type.UnionType; jjg@988: import javax.lang.model.type.UnknownTypeException; jjg@988: import javax.lang.model.util.SimpleTypeVisitor6; jjg@988: import javax.lang.model.util.SimpleTypeVisitor7; 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 jjg@988: public Void visitCatch(CatchTree node, Void p) { jjg@988: TreePath param = new TreePath(getCurrentPath(), node.getParameter()); jjg@988: Element ex = trees.getElement(param); jjg@988: validateUnionTypeInfo(ex); mcimadamore@735: if (ex.getSimpleName().contentEquals("ex")) { mcimadamore@735: assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); jjg@988: for (Element e : types.asElement(trees.getLub(node)).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: } jjg@988: assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount); mcimadamore@735: } jjg@988: return super.visitCatch(node, p); mcimadamore@735: } mcimadamore@735: } mcimadamore@735: jjg@988: private void validateUnionTypeInfo(Element ex) { jjg@988: UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class); jjg@988: assertTrue(ut != null, "UnionType annotation must be present"); jjg@988: jjg@988: TypeMirror expectedUnionType = ex.asType(); jjg@988: assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected"); jjg@988: jjg@988: try { jjg@988: new SimpleTypeVisitor6(){}.visit(expectedUnionType); jjg@988: throw new RuntimeException("Expected UnknownTypeException not thrown."); jjg@988: } catch (UnknownTypeException ute) { jjg@988: ; // Expected jjg@988: } jjg@988: darcy@1054: UnionType unionType = new SimpleTypeVisitor(){ jjg@988: @Override jjg@988: protected UnionType defaultAction(TypeMirror e, Void p) {return null;} jjg@988: jjg@988: @Override jjg@988: public UnionType visitUnion(UnionType t, Void p) {return t;} jjg@988: }.visit(expectedUnionType); jjg@988: assertTrue(unionType != null, "Must get a non-null union type."); jjg@988: jjg@988: assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match"); jjg@988: jjg@988: String[] typeNames = ut.value(); jjg@988: for(int i = 0; i < typeNames.length; i++) { jjg@988: TypeMirror typeFromAnnotation = nameToType(typeNames[i]); jjg@988: assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), jjg@988: "Types were not equal."); jjg@988: } jjg@988: } jjg@988: jjg@988: private TypeMirror nameToType(String name) { jjg@988: return elements.getTypeElement(name).asType(); jjg@988: } jjg@988: 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: }