734N/A/*
1011N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
734N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
734N/A *
734N/A * This code is free software; you can redistribute it and/or modify it
734N/A * under the terms of the GNU General Public License version 2 only, as
734N/A * published by the Free Software Foundation.
734N/A *
734N/A * This code is distributed in the hope that it will be useful, but WITHOUT
734N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
734N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
734N/A * version 2 for more details (a copy is included in the LICENSE file that
734N/A * accompanied this code).
734N/A *
734N/A * You should have received a copy of the GNU General Public License version
734N/A * 2 along with this work; if not, write to the Free Software Foundation,
734N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
734N/A *
734N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
734N/A * or visit www.oracle.com if you need additional information or have any
734N/A * questions.
734N/A */
734N/A
734N/A/*
734N/A * @test
734N/A * @bug 6993963
734N/A * @summary Project Coin: Use precise exception analysis for effectively final catch parameters
734N/A * @library ../../lib
734N/A * @build JavacTestingAbstractProcessor ModelChecker
734N/A * @compile -processor ModelChecker Model01.java
734N/A */
734N/A
987N/Aimport com.sun.source.tree.CatchTree;
734N/Aimport com.sun.source.util.TreePathScanner;
734N/Aimport com.sun.source.util.Trees;
734N/Aimport com.sun.source.util.TreePath;
734N/A
734N/Aimport java.util.Set;
734N/Aimport javax.annotation.processing.RoundEnvironment;
734N/Aimport javax.annotation.processing.SupportedAnnotationTypes;
734N/Aimport javax.lang.model.element.Element;
734N/Aimport javax.lang.model.element.ElementKind;
734N/Aimport javax.lang.model.element.TypeElement;
987N/Aimport javax.lang.model.type.TypeMirror;
987N/Aimport javax.lang.model.type.TypeKind;
987N/Aimport javax.lang.model.type.UnionType;
987N/Aimport javax.lang.model.type.UnknownTypeException;
987N/Aimport javax.lang.model.util.SimpleTypeVisitor6;
987N/Aimport javax.lang.model.util.SimpleTypeVisitor7;
734N/A
734N/A@SupportedAnnotationTypes("Check")
734N/Apublic class ModelChecker extends JavacTestingAbstractProcessor {
734N/A
734N/A @Override
734N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
734N/A if (roundEnv.processingOver())
734N/A return true;
734N/A
734N/A Trees trees = Trees.instance(processingEnv);
734N/A
734N/A TypeElement testAnno = elements.getTypeElement("Check");
734N/A for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) {
734N/A TreePath p = trees.getPath(elem);
734N/A new MulticatchParamTester(trees).scan(p, null);
734N/A }
734N/A return true;
734N/A }
734N/A
734N/A class MulticatchParamTester extends TreePathScanner<Void, Void> {
734N/A Trees trees;
734N/A
734N/A public MulticatchParamTester(Trees trees) {
734N/A super();
734N/A this.trees = trees;
734N/A }
734N/A
734N/A @Override
987N/A public Void visitCatch(CatchTree node, Void p) {
987N/A TreePath param = new TreePath(getCurrentPath(), node.getParameter());
987N/A Element ex = trees.getElement(param);
987N/A validateUnionTypeInfo(ex);
734N/A if (ex.getSimpleName().contentEquals("ex")) {
734N/A assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind());
987N/A for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) {
734N/A Member m = e.getAnnotation(Member.class);
734N/A if (m != null) {
734N/A assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind());
734N/A }
734N/A }
987N/A assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount);
734N/A }
987N/A return super.visitCatch(node, p);
987N/A }
987N/A }
987N/A
987N/A private void validateUnionTypeInfo(Element ex) {
987N/A UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class);
987N/A assertTrue(ut != null, "UnionType annotation must be present");
987N/A
987N/A TypeMirror expectedUnionType = ex.asType();
987N/A assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected");
987N/A
987N/A try {
987N/A new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedUnionType);
987N/A throw new RuntimeException("Expected UnknownTypeException not thrown.");
987N/A } catch (UnknownTypeException ute) {
987N/A ; // Expected
734N/A }
987N/A
987N/A UnionType unionType = new SimpleTypeVisitor7<UnionType, Void>(){
987N/A @Override
987N/A protected UnionType defaultAction(TypeMirror e, Void p) {return null;}
987N/A
987N/A @Override
987N/A public UnionType visitUnion(UnionType t, Void p) {return t;}
987N/A }.visit(expectedUnionType);
987N/A assertTrue(unionType != null, "Must get a non-null union type.");
987N/A
987N/A assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match");
987N/A
987N/A String[] typeNames = ut.value();
987N/A for(int i = 0; i < typeNames.length; i++) {
987N/A TypeMirror typeFromAnnotation = nameToType(typeNames[i]);
987N/A assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)),
987N/A "Types were not equal.");
987N/A }
987N/A }
987N/A
987N/A private TypeMirror nameToType(String name) {
987N/A return elements.getTypeElement(name).asType();
734N/A }
734N/A
734N/A private static void assertTrue(boolean cond, String msg) {
734N/A assertionCount++;
734N/A if (!cond)
734N/A throw new AssertionError(msg);
734N/A }
734N/A
734N/A static int assertionCount = 0;
734N/A}