test/tools/javac/cast/intersection/model/ModelChecker.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 2050
09301757bb32
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

     1 /*
     2  * Copyright (c) 2012, 2013, 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  */
    24 import com.sun.source.tree.ExpressionTree;
    25 import com.sun.source.tree.Tree;
    26 import com.sun.source.tree.TypeCastTree;
    27 import com.sun.source.tree.VariableTree;
    28 import com.sun.source.util.TreePathScanner;
    29 import com.sun.source.util.Trees;
    30 import com.sun.source.util.TreePath;
    31 import com.sun.tools.javac.tree.JCTree.JCExpression;
    33 import java.util.Set;
    35 import javax.annotation.processing.RoundEnvironment;
    36 import javax.annotation.processing.SupportedAnnotationTypes;
    37 import javax.lang.model.element.Element;
    38 import javax.lang.model.element.TypeElement;
    39 import javax.lang.model.type.TypeMirror;
    40 import javax.lang.model.type.TypeKind;
    41 import javax.lang.model.type.IntersectionType;
    42 import javax.lang.model.type.UnknownTypeException;
    43 import javax.lang.model.util.SimpleTypeVisitor6;
    44 import javax.lang.model.util.SimpleTypeVisitor7;
    46 @SupportedAnnotationTypes("Check")
    47 public class ModelChecker extends JavacTestingAbstractProcessor {
    49     @Override
    50     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    51         if (roundEnv.processingOver())
    52             return true;
    54         Trees trees = Trees.instance(processingEnv);
    56         TypeElement testAnno = elements.getTypeElement("Check");
    57         for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) {
    58             TreePath p = trees.getPath(elem);
    59             new IntersectionCastTester(trees).scan(p, null);
    60         }
    61         return true;
    62     }
    64     class IntersectionCastTester extends TreePathScanner<Void, Void> {
    65         Trees trees;
    67         public IntersectionCastTester(Trees trees) {
    68             super();
    69             this.trees = trees;
    70         }
    72         @Override
    73         public Void visitVariable(VariableTree node, Void p) {
    75             TreePath varPath = new TreePath(getCurrentPath(), node);
    76             Element v = trees.getElement(varPath);
    78             IntersectionTypeInfo it = v.getAnnotation(IntersectionTypeInfo.class);
    79             assertTrue(it != null, "IntersectionType annotation must be present");
    81             ExpressionTree varInit = node.getInitializer();
    82             assertTrue(varInit != null && varInit.getKind() == Tree.Kind.TYPE_CAST,
    83                     "variable must have be initialized to an expression containing an intersection type cast");
    85             TypeMirror t = ((JCExpression)((TypeCastTree)varInit).getType()).type;
    87             validateIntersectionTypeInfo(t, it);
    89             for (Element e2 : types.asElement(t).getEnclosedElements()) {
    90                 assertTrue(false, "an intersection type has no declared members");
    91             }
    93             for (Element e2 : elements.getAllMembers((TypeElement)types.asElement(t))) {
    94                 Member m = e2.getAnnotation(Member.class);
    95                 if (m != null) {
    96                     assertTrue(e2.getKind() == m.value(), "Expected " + m.value() + " - found " + e2.getKind());
    97                 }
    98             }
   100             assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount);
   101             return super.visitVariable(node, p);
   102         }
   103     }
   105     private void validateIntersectionTypeInfo(TypeMirror expectedIntersectionType, IntersectionTypeInfo it) {
   107         assertTrue(expectedIntersectionType.getKind() == TypeKind.INTERSECTION, "INTERSECTION kind expected");
   109         try {
   110             new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedIntersectionType);
   111             throw new RuntimeException("Expected UnknownTypeException not thrown.");
   112         } catch (UnknownTypeException ute) {
   113             ; // Expected
   114         }
   116         try {
   117             new SimpleTypeVisitor7<Void, Void>(){}.visit(expectedIntersectionType);
   118             throw new RuntimeException("Expected UnknownTypeException not thrown.");
   119         } catch (UnknownTypeException ute) {
   120             ; // Expected
   121         }
   123         IntersectionType intersectionType = new SimpleTypeVisitor<IntersectionType, Void>(){
   124             @Override
   125             protected IntersectionType defaultAction(TypeMirror e, Void p) {return null;}
   127             @Override
   128             public IntersectionType visitIntersection(IntersectionType t, Void p) {return t;}
   129         }.visit(expectedIntersectionType);
   130         assertTrue(intersectionType != null, "Must get a non-null intersection type.");
   132         assertTrue(it.value().length == intersectionType.getBounds().size(), "Cardinalities do not match");
   134         String[] typeNames = it.value();
   135         for(int i = 0; i < typeNames.length; i++) {
   136             TypeMirror typeFromAnnotation = nameToType(typeNames[i]);
   137             assertTrue(types.isSameType(typeFromAnnotation, intersectionType.getBounds().get(i)),
   138                        "Types were not equal.");
   139         }
   140     }
   142     private TypeMirror nameToType(String name) {
   143         return elements.getTypeElement(name).asType();
   144     }
   146     private static void assertTrue(boolean cond, String msg) {
   147         assertionCount++;
   148         if (!cond)
   149             throw new AssertionError(msg);
   150     }
   152     static int assertionCount = 0;
   153 }

mercurial