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

changeset 988
7ae6c0fd479b
parent 735
f2048d9c666e
child 1013
8eb952f43b11
equal deleted inserted replaced
987:c7841bbe1227 988:7ae6c0fd479b
1 /* 1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 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. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
28 * @library ../../lib 28 * @library ../../lib
29 * @build JavacTestingAbstractProcessor ModelChecker 29 * @build JavacTestingAbstractProcessor ModelChecker
30 * @compile -processor ModelChecker Model01.java 30 * @compile -processor ModelChecker Model01.java
31 */ 31 */
32 32
33 import com.sun.source.tree.VariableTree; 33 import com.sun.source.tree.CatchTree;
34 import com.sun.source.util.TreePathScanner; 34 import com.sun.source.util.TreePathScanner;
35 import com.sun.source.util.Trees; 35 import com.sun.source.util.Trees;
36 import com.sun.source.util.TreePath; 36 import com.sun.source.util.TreePath;
37 37
38 import java.util.Set; 38 import java.util.Set;
39 import javax.annotation.processing.RoundEnvironment; 39 import javax.annotation.processing.RoundEnvironment;
40 import javax.annotation.processing.SupportedAnnotationTypes; 40 import javax.annotation.processing.SupportedAnnotationTypes;
41 import javax.lang.model.element.Element; 41 import javax.lang.model.element.Element;
42 import javax.lang.model.element.ElementKind; 42 import javax.lang.model.element.ElementKind;
43 import javax.lang.model.element.TypeElement; 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;
44 50
45 @SupportedAnnotationTypes("Check") 51 @SupportedAnnotationTypes("Check")
46 public class ModelChecker extends JavacTestingAbstractProcessor { 52 public class ModelChecker extends JavacTestingAbstractProcessor {
47 53
48 @Override 54 @Override
67 super(); 73 super();
68 this.trees = trees; 74 this.trees = trees;
69 } 75 }
70 76
71 @Override 77 @Override
72 public Void visitVariable(VariableTree node, Void p) { 78 public Void visitCatch(CatchTree node, Void p) {
73 Element ex = trees.getElement(getCurrentPath()); 79 TreePath param = new TreePath(getCurrentPath(), node.getParameter());
80 Element ex = trees.getElement(param);
81 validateUnionTypeInfo(ex);
74 if (ex.getSimpleName().contentEquals("ex")) { 82 if (ex.getSimpleName().contentEquals("ex")) {
75 assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); 83 assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind());
76 for (Element e : types.asElement(ex.asType()).getEnclosedElements()) { 84 for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) {
77 Member m = e.getAnnotation(Member.class); 85 Member m = e.getAnnotation(Member.class);
78 if (m != null) { 86 if (m != null) {
79 assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); 87 assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind());
80 } 88 }
81 } 89 }
82 assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount); 90 assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount);
83 } 91 }
84 return super.visitVariable(node, p); 92 return super.visitCatch(node, p);
85 } 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();
86 } 131 }
87 132
88 private static void assertTrue(boolean cond, String msg) { 133 private static void assertTrue(boolean cond, String msg) {
89 assertionCount++; 134 assertionCount++;
90 if (!cond) 135 if (!cond)

mercurial