Tue, 24 Dec 2013 09:17:37 -0800
8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com
mcimadamore@1436 | 1 | /* |
ksrini@2227 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1436 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1436 | 4 | * |
mcimadamore@1436 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1436 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1436 | 7 | * published by the Free Software Foundation. |
mcimadamore@1436 | 8 | * |
mcimadamore@1436 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1436 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1436 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1436 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1436 | 13 | * accompanied this code). |
mcimadamore@1436 | 14 | * |
mcimadamore@1436 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1436 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1436 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1436 | 18 | * |
mcimadamore@1436 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1436 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1436 | 21 | * questions. |
mcimadamore@1436 | 22 | */ |
mcimadamore@1436 | 23 | |
mcimadamore@1436 | 24 | import com.sun.source.tree.ExpressionTree; |
mcimadamore@1436 | 25 | import com.sun.source.tree.Tree; |
mcimadamore@1436 | 26 | import com.sun.source.tree.TypeCastTree; |
mcimadamore@1436 | 27 | import com.sun.source.tree.VariableTree; |
mcimadamore@1436 | 28 | import com.sun.source.util.TreePathScanner; |
mcimadamore@1436 | 29 | import com.sun.source.util.Trees; |
mcimadamore@1436 | 30 | import com.sun.source.util.TreePath; |
mcimadamore@1436 | 31 | import com.sun.tools.javac.tree.JCTree.JCExpression; |
mcimadamore@1436 | 32 | |
mcimadamore@1436 | 33 | import java.util.Set; |
mcimadamore@1436 | 34 | |
mcimadamore@1436 | 35 | import javax.annotation.processing.RoundEnvironment; |
mcimadamore@1436 | 36 | import javax.annotation.processing.SupportedAnnotationTypes; |
mcimadamore@1436 | 37 | import javax.lang.model.element.Element; |
mcimadamore@1436 | 38 | import javax.lang.model.element.TypeElement; |
mcimadamore@1436 | 39 | import javax.lang.model.type.TypeMirror; |
mcimadamore@1436 | 40 | import javax.lang.model.type.TypeKind; |
mcimadamore@1436 | 41 | import javax.lang.model.type.IntersectionType; |
mcimadamore@1436 | 42 | import javax.lang.model.type.UnknownTypeException; |
mcimadamore@1436 | 43 | import javax.lang.model.util.SimpleTypeVisitor6; |
mcimadamore@1436 | 44 | import javax.lang.model.util.SimpleTypeVisitor7; |
mcimadamore@1436 | 45 | |
mcimadamore@1436 | 46 | @SupportedAnnotationTypes("Check") |
mcimadamore@1436 | 47 | public class ModelChecker extends JavacTestingAbstractProcessor { |
mcimadamore@1436 | 48 | |
mcimadamore@1436 | 49 | @Override |
mcimadamore@1436 | 50 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
mcimadamore@1436 | 51 | if (roundEnv.processingOver()) |
mcimadamore@1436 | 52 | return true; |
mcimadamore@1436 | 53 | |
mcimadamore@1436 | 54 | Trees trees = Trees.instance(processingEnv); |
mcimadamore@1436 | 55 | |
mcimadamore@1436 | 56 | TypeElement testAnno = elements.getTypeElement("Check"); |
mcimadamore@1436 | 57 | for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { |
mcimadamore@1436 | 58 | TreePath p = trees.getPath(elem); |
mcimadamore@1436 | 59 | new IntersectionCastTester(trees).scan(p, null); |
mcimadamore@1436 | 60 | } |
mcimadamore@1436 | 61 | return true; |
mcimadamore@1436 | 62 | } |
mcimadamore@1436 | 63 | |
mcimadamore@1436 | 64 | class IntersectionCastTester extends TreePathScanner<Void, Void> { |
mcimadamore@1436 | 65 | Trees trees; |
mcimadamore@1436 | 66 | |
mcimadamore@1436 | 67 | public IntersectionCastTester(Trees trees) { |
mcimadamore@1436 | 68 | super(); |
mcimadamore@1436 | 69 | this.trees = trees; |
mcimadamore@1436 | 70 | } |
mcimadamore@1436 | 71 | |
mcimadamore@1436 | 72 | @Override |
mcimadamore@1436 | 73 | public Void visitVariable(VariableTree node, Void p) { |
mcimadamore@1436 | 74 | |
mcimadamore@1436 | 75 | TreePath varPath = new TreePath(getCurrentPath(), node); |
mcimadamore@1436 | 76 | Element v = trees.getElement(varPath); |
mcimadamore@1436 | 77 | |
mcimadamore@1436 | 78 | IntersectionTypeInfo it = v.getAnnotation(IntersectionTypeInfo.class); |
mcimadamore@1436 | 79 | assertTrue(it != null, "IntersectionType annotation must be present"); |
mcimadamore@1436 | 80 | |
mcimadamore@1436 | 81 | ExpressionTree varInit = node.getInitializer(); |
mcimadamore@1436 | 82 | assertTrue(varInit != null && varInit.getKind() == Tree.Kind.TYPE_CAST, |
mcimadamore@1436 | 83 | "variable must have be initialized to an expression containing an intersection type cast"); |
mcimadamore@1436 | 84 | |
mcimadamore@1436 | 85 | TypeMirror t = ((JCExpression)((TypeCastTree)varInit).getType()).type; |
mcimadamore@1436 | 86 | |
mcimadamore@1436 | 87 | validateIntersectionTypeInfo(t, it); |
mcimadamore@1436 | 88 | |
mcimadamore@1436 | 89 | for (Element e2 : types.asElement(t).getEnclosedElements()) { |
mcimadamore@1436 | 90 | assertTrue(false, "an intersection type has no declared members"); |
mcimadamore@1436 | 91 | } |
mcimadamore@1436 | 92 | |
mcimadamore@1436 | 93 | for (Element e2 : elements.getAllMembers((TypeElement)types.asElement(t))) { |
mcimadamore@1436 | 94 | Member m = e2.getAnnotation(Member.class); |
mcimadamore@1436 | 95 | if (m != null) { |
mcimadamore@1436 | 96 | assertTrue(e2.getKind() == m.value(), "Expected " + m.value() + " - found " + e2.getKind()); |
mcimadamore@1436 | 97 | } |
mcimadamore@1436 | 98 | } |
mcimadamore@1436 | 99 | |
emc@2050 | 100 | assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount); |
mcimadamore@1436 | 101 | return super.visitVariable(node, p); |
mcimadamore@1436 | 102 | } |
mcimadamore@1436 | 103 | } |
mcimadamore@1436 | 104 | |
mcimadamore@1436 | 105 | private void validateIntersectionTypeInfo(TypeMirror expectedIntersectionType, IntersectionTypeInfo it) { |
mcimadamore@1436 | 106 | |
mcimadamore@1436 | 107 | assertTrue(expectedIntersectionType.getKind() == TypeKind.INTERSECTION, "INTERSECTION kind expected"); |
mcimadamore@1436 | 108 | |
mcimadamore@1436 | 109 | try { |
mcimadamore@1436 | 110 | new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedIntersectionType); |
mcimadamore@1436 | 111 | throw new RuntimeException("Expected UnknownTypeException not thrown."); |
mcimadamore@1436 | 112 | } catch (UnknownTypeException ute) { |
mcimadamore@1436 | 113 | ; // Expected |
mcimadamore@1436 | 114 | } |
mcimadamore@1436 | 115 | |
mcimadamore@1436 | 116 | try { |
mcimadamore@1436 | 117 | new SimpleTypeVisitor7<Void, Void>(){}.visit(expectedIntersectionType); |
mcimadamore@1436 | 118 | throw new RuntimeException("Expected UnknownTypeException not thrown."); |
mcimadamore@1436 | 119 | } catch (UnknownTypeException ute) { |
mcimadamore@1436 | 120 | ; // Expected |
mcimadamore@1436 | 121 | } |
mcimadamore@1436 | 122 | |
mcimadamore@1436 | 123 | IntersectionType intersectionType = new SimpleTypeVisitor<IntersectionType, Void>(){ |
mcimadamore@1436 | 124 | @Override |
mcimadamore@1436 | 125 | protected IntersectionType defaultAction(TypeMirror e, Void p) {return null;} |
mcimadamore@1436 | 126 | |
mcimadamore@1436 | 127 | @Override |
mcimadamore@1436 | 128 | public IntersectionType visitIntersection(IntersectionType t, Void p) {return t;} |
mcimadamore@1436 | 129 | }.visit(expectedIntersectionType); |
mcimadamore@1436 | 130 | assertTrue(intersectionType != null, "Must get a non-null intersection type."); |
mcimadamore@1436 | 131 | |
mcimadamore@1436 | 132 | assertTrue(it.value().length == intersectionType.getBounds().size(), "Cardinalities do not match"); |
mcimadamore@1436 | 133 | |
mcimadamore@1436 | 134 | String[] typeNames = it.value(); |
mcimadamore@1436 | 135 | for(int i = 0; i < typeNames.length; i++) { |
mcimadamore@1436 | 136 | TypeMirror typeFromAnnotation = nameToType(typeNames[i]); |
mcimadamore@1436 | 137 | assertTrue(types.isSameType(typeFromAnnotation, intersectionType.getBounds().get(i)), |
mcimadamore@1436 | 138 | "Types were not equal."); |
mcimadamore@1436 | 139 | } |
mcimadamore@1436 | 140 | } |
mcimadamore@1436 | 141 | |
mcimadamore@1436 | 142 | private TypeMirror nameToType(String name) { |
mcimadamore@1436 | 143 | return elements.getTypeElement(name).asType(); |
mcimadamore@1436 | 144 | } |
mcimadamore@1436 | 145 | |
mcimadamore@1436 | 146 | private static void assertTrue(boolean cond, String msg) { |
mcimadamore@1436 | 147 | assertionCount++; |
mcimadamore@1436 | 148 | if (!cond) |
mcimadamore@1436 | 149 | throw new AssertionError(msg); |
mcimadamore@1436 | 150 | } |
mcimadamore@1436 | 151 | |
mcimadamore@1436 | 152 | static int assertionCount = 0; |
mcimadamore@1436 | 153 | } |