|
1 /* |
|
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. |
|
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 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6993963 7025809 |
|
27 * @summary Project Coin: Use precise exception analysis for effectively final catch parameters |
|
28 * @library /tools/javac/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> { |
|
70 Trees trees; |
|
71 |
|
72 public MulticatchParamTester(Trees trees) { |
|
73 super(); |
|
74 this.trees = trees; |
|
75 } |
|
76 |
|
77 @Override |
|
78 public Void visitCatch(CatchTree node, Void p) { |
|
79 TreePath param = new TreePath(getCurrentPath(), node.getParameter()); |
|
80 Element ex = trees.getElement(param); |
|
81 validateUnionTypeInfo(ex); |
|
82 if (ex.getSimpleName().contentEquals("ex")) { |
|
83 assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); |
|
84 for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) { |
|
85 Member m = e.getAnnotation(Member.class); |
|
86 if (m != null) { |
|
87 assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); |
|
88 } |
|
89 } |
|
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 SimpleTypeVisitor<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(); |
|
131 } |
|
132 |
|
133 private static void assertTrue(boolean cond, String msg) { |
|
134 assertionCount++; |
|
135 if (!cond) |
|
136 throw new AssertionError(msg); |
|
137 } |
|
138 |
|
139 static int assertionCount = 0; |
|
140 } |