test/tools/javac/multicatch/model/ModelChecker.java

Print this page




   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
  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 
  51 @SupportedAnnotationTypes("Check")
  52 public class ModelChecker extends JavacTestingAbstractProcessor {
  53 
  54     @Override
  55     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  56         if (roundEnv.processingOver())
  57             return true;
  58 
  59         Trees trees = Trees.instance(processingEnv);
  60 
  61         TypeElement testAnno = elements.getTypeElement("Check");
  62         for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) {
  63             TreePath p = trees.getPath(elem);
  64             new MulticatchParamTester(trees).scan(p, null);
  65         }
  66         return true;
  67     }
  68 
  69     class MulticatchParamTester extends TreePathScanner<Void, Void> {


  90                 assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount);
  91             }
  92             return super.visitCatch(node, p);
  93         }
  94     }
  95 
  96     private void validateUnionTypeInfo(Element ex) {
  97         UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class);
  98         assertTrue(ut != null, "UnionType annotation must be present");
  99 
 100         TypeMirror expectedUnionType = ex.asType();
 101         assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected");
 102 
 103         try {
 104             new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedUnionType);
 105             throw new RuntimeException("Expected UnknownTypeException not thrown.");
 106         } catch (UnknownTypeException ute) {
 107             ; // Expected
 108         }
 109 
 110         UnionType unionType = new SimpleTypeVisitor7<UnionType, Void>(){
 111             @Override
 112             protected UnionType defaultAction(TypeMirror e, Void p) {return null;}
 113 
 114             @Override
 115             public UnionType visitUnion(UnionType t, Void p) {return t;}
 116         }.visit(expectedUnionType);
 117         assertTrue(unionType != null, "Must get a non-null union type.");
 118 
 119         assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match");
 120 
 121         String[] typeNames = ut.value();
 122         for(int i = 0; i < typeNames.length; i++) {
 123             TypeMirror typeFromAnnotation = nameToType(typeNames[i]);
 124             assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)),
 125                        "Types were not equal.");
 126         }
 127     }
 128 
 129     private TypeMirror nameToType(String name) {
 130         return elements.getTypeElement(name).asType();


   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> {


  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();