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