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