Sat, 17 Nov 2012 19:01:03 +0000
8003280: Add lambda tests
Summary: Turn on lambda expression, method reference and default method support
Reviewed-by: jjg
mcimadamore@735 | 1 | /* |
katleman@1013 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@735 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@735 | 4 | * |
mcimadamore@735 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@735 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@735 | 7 | * published by the Free Software Foundation. |
mcimadamore@735 | 8 | * |
mcimadamore@735 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@735 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@735 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@735 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@735 | 13 | * accompanied this code). |
mcimadamore@735 | 14 | * |
mcimadamore@735 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@735 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@735 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@735 | 18 | * |
mcimadamore@735 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@735 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@735 | 21 | * questions. |
mcimadamore@735 | 22 | */ |
mcimadamore@735 | 23 | |
mcimadamore@735 | 24 | /* |
mcimadamore@735 | 25 | * @test |
darcy@1054 | 26 | * @bug 6993963 7025809 |
mcimadamore@735 | 27 | * @summary Project Coin: Use precise exception analysis for effectively final catch parameters |
mcimadamore@735 | 28 | * @library ../../lib |
mcimadamore@735 | 29 | * @build JavacTestingAbstractProcessor ModelChecker |
mcimadamore@735 | 30 | * @compile -processor ModelChecker Model01.java |
mcimadamore@735 | 31 | */ |
mcimadamore@735 | 32 | |
jjg@988 | 33 | import com.sun.source.tree.CatchTree; |
mcimadamore@735 | 34 | import com.sun.source.util.TreePathScanner; |
mcimadamore@735 | 35 | import com.sun.source.util.Trees; |
mcimadamore@735 | 36 | import com.sun.source.util.TreePath; |
mcimadamore@735 | 37 | |
mcimadamore@735 | 38 | import java.util.Set; |
mcimadamore@735 | 39 | import javax.annotation.processing.RoundEnvironment; |
mcimadamore@735 | 40 | import javax.annotation.processing.SupportedAnnotationTypes; |
mcimadamore@735 | 41 | import javax.lang.model.element.Element; |
mcimadamore@735 | 42 | import javax.lang.model.element.ElementKind; |
mcimadamore@735 | 43 | import javax.lang.model.element.TypeElement; |
jjg@988 | 44 | import javax.lang.model.type.TypeMirror; |
jjg@988 | 45 | import javax.lang.model.type.TypeKind; |
jjg@988 | 46 | import javax.lang.model.type.UnionType; |
jjg@988 | 47 | import javax.lang.model.type.UnknownTypeException; |
jjg@988 | 48 | import javax.lang.model.util.SimpleTypeVisitor6; |
jjg@988 | 49 | import javax.lang.model.util.SimpleTypeVisitor7; |
mcimadamore@735 | 50 | |
mcimadamore@735 | 51 | @SupportedAnnotationTypes("Check") |
mcimadamore@735 | 52 | public class ModelChecker extends JavacTestingAbstractProcessor { |
mcimadamore@735 | 53 | |
mcimadamore@735 | 54 | @Override |
mcimadamore@735 | 55 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
mcimadamore@735 | 56 | if (roundEnv.processingOver()) |
mcimadamore@735 | 57 | return true; |
mcimadamore@735 | 58 | |
mcimadamore@735 | 59 | Trees trees = Trees.instance(processingEnv); |
mcimadamore@735 | 60 | |
mcimadamore@735 | 61 | TypeElement testAnno = elements.getTypeElement("Check"); |
mcimadamore@735 | 62 | for (Element elem: roundEnv.getElementsAnnotatedWith(testAnno)) { |
mcimadamore@735 | 63 | TreePath p = trees.getPath(elem); |
mcimadamore@735 | 64 | new MulticatchParamTester(trees).scan(p, null); |
mcimadamore@735 | 65 | } |
mcimadamore@735 | 66 | return true; |
mcimadamore@735 | 67 | } |
mcimadamore@735 | 68 | |
mcimadamore@735 | 69 | class MulticatchParamTester extends TreePathScanner<Void, Void> { |
mcimadamore@735 | 70 | Trees trees; |
mcimadamore@735 | 71 | |
mcimadamore@735 | 72 | public MulticatchParamTester(Trees trees) { |
mcimadamore@735 | 73 | super(); |
mcimadamore@735 | 74 | this.trees = trees; |
mcimadamore@735 | 75 | } |
mcimadamore@735 | 76 | |
mcimadamore@735 | 77 | @Override |
jjg@988 | 78 | public Void visitCatch(CatchTree node, Void p) { |
jjg@988 | 79 | TreePath param = new TreePath(getCurrentPath(), node.getParameter()); |
jjg@988 | 80 | Element ex = trees.getElement(param); |
jjg@988 | 81 | validateUnionTypeInfo(ex); |
mcimadamore@735 | 82 | if (ex.getSimpleName().contentEquals("ex")) { |
mcimadamore@735 | 83 | assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); |
jjg@988 | 84 | for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) { |
mcimadamore@735 | 85 | Member m = e.getAnnotation(Member.class); |
mcimadamore@735 | 86 | if (m != null) { |
mcimadamore@735 | 87 | assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); |
mcimadamore@735 | 88 | } |
mcimadamore@735 | 89 | } |
jjg@988 | 90 | assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount); |
mcimadamore@735 | 91 | } |
jjg@988 | 92 | return super.visitCatch(node, p); |
mcimadamore@735 | 93 | } |
mcimadamore@735 | 94 | } |
mcimadamore@735 | 95 | |
jjg@988 | 96 | private void validateUnionTypeInfo(Element ex) { |
jjg@988 | 97 | UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class); |
jjg@988 | 98 | assertTrue(ut != null, "UnionType annotation must be present"); |
jjg@988 | 99 | |
jjg@988 | 100 | TypeMirror expectedUnionType = ex.asType(); |
jjg@988 | 101 | assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected"); |
jjg@988 | 102 | |
jjg@988 | 103 | try { |
jjg@988 | 104 | new SimpleTypeVisitor6<Void, Void>(){}.visit(expectedUnionType); |
jjg@988 | 105 | throw new RuntimeException("Expected UnknownTypeException not thrown."); |
jjg@988 | 106 | } catch (UnknownTypeException ute) { |
jjg@988 | 107 | ; // Expected |
jjg@988 | 108 | } |
jjg@988 | 109 | |
darcy@1054 | 110 | UnionType unionType = new SimpleTypeVisitor<UnionType, Void>(){ |
jjg@988 | 111 | @Override |
jjg@988 | 112 | protected UnionType defaultAction(TypeMirror e, Void p) {return null;} |
jjg@988 | 113 | |
jjg@988 | 114 | @Override |
jjg@988 | 115 | public UnionType visitUnion(UnionType t, Void p) {return t;} |
jjg@988 | 116 | }.visit(expectedUnionType); |
jjg@988 | 117 | assertTrue(unionType != null, "Must get a non-null union type."); |
jjg@988 | 118 | |
jjg@988 | 119 | assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match"); |
jjg@988 | 120 | |
jjg@988 | 121 | String[] typeNames = ut.value(); |
jjg@988 | 122 | for(int i = 0; i < typeNames.length; i++) { |
jjg@988 | 123 | TypeMirror typeFromAnnotation = nameToType(typeNames[i]); |
jjg@988 | 124 | assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), |
jjg@988 | 125 | "Types were not equal."); |
jjg@988 | 126 | } |
jjg@988 | 127 | } |
jjg@988 | 128 | |
jjg@988 | 129 | private TypeMirror nameToType(String name) { |
jjg@988 | 130 | return elements.getTypeElement(name).asType(); |
jjg@988 | 131 | } |
jjg@988 | 132 | |
mcimadamore@735 | 133 | private static void assertTrue(boolean cond, String msg) { |
mcimadamore@735 | 134 | assertionCount++; |
mcimadamore@735 | 135 | if (!cond) |
mcimadamore@735 | 136 | throw new AssertionError(msg); |
mcimadamore@735 | 137 | } |
mcimadamore@735 | 138 | |
mcimadamore@735 | 139 | static int assertionCount = 0; |
mcimadamore@735 | 140 | } |