diff -r c7841bbe1227 -r 7ae6c0fd479b test/tools/javac/multicatch/model/ModelChecker.java --- a/test/tools/javac/multicatch/model/ModelChecker.java Thu Apr 28 08:46:06 2011 -0700 +++ b/test/tools/javac/multicatch/model/ModelChecker.java Thu Apr 28 15:05:36 2011 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2011 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,7 +30,7 @@ * @compile -processor ModelChecker Model01.java */ -import com.sun.source.tree.VariableTree; +import com.sun.source.tree.CatchTree; import com.sun.source.util.TreePathScanner; import com.sun.source.util.Trees; import com.sun.source.util.TreePath; @@ -41,6 +41,12 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.UnionType; +import javax.lang.model.type.UnknownTypeException; +import javax.lang.model.util.SimpleTypeVisitor6; +import javax.lang.model.util.SimpleTypeVisitor7; @SupportedAnnotationTypes("Check") public class ModelChecker extends JavacTestingAbstractProcessor { @@ -69,22 +75,61 @@ } @Override - public Void visitVariable(VariableTree node, Void p) { - Element ex = trees.getElement(getCurrentPath()); + public Void visitCatch(CatchTree node, Void p) { + TreePath param = new TreePath(getCurrentPath(), node.getParameter()); + Element ex = trees.getElement(param); + validateUnionTypeInfo(ex); if (ex.getSimpleName().contentEquals("ex")) { assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind()); - for (Element e : types.asElement(ex.asType()).getEnclosedElements()) { + for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) { Member m = e.getAnnotation(Member.class); if (m != null) { assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind()); } } - assertTrue(assertionCount == 3, "Expected 3 assertions - found " + assertionCount); + assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount); } - return super.visitVariable(node, p); + return super.visitCatch(node, p); } } + private void validateUnionTypeInfo(Element ex) { + UnionTypeInfo ut = ex.getAnnotation(UnionTypeInfo.class); + assertTrue(ut != null, "UnionType annotation must be present"); + + TypeMirror expectedUnionType = ex.asType(); + assertTrue(expectedUnionType.getKind() == TypeKind.UNION, "UNION kind expected"); + + try { + new SimpleTypeVisitor6(){}.visit(expectedUnionType); + throw new RuntimeException("Expected UnknownTypeException not thrown."); + } catch (UnknownTypeException ute) { + ; // Expected + } + + UnionType unionType = new SimpleTypeVisitor7(){ + @Override + protected UnionType defaultAction(TypeMirror e, Void p) {return null;} + + @Override + public UnionType visitUnion(UnionType t, Void p) {return t;} + }.visit(expectedUnionType); + assertTrue(unionType != null, "Must get a non-null union type."); + + assertTrue(ut.value().length == unionType.getAlternatives().size(), "Cardinalities do not match"); + + String[] typeNames = ut.value(); + for(int i = 0; i < typeNames.length; i++) { + TypeMirror typeFromAnnotation = nameToType(typeNames[i]); + assertTrue(types.isSameType(typeFromAnnotation, unionType.getAlternatives().get(i)), + "Types were not equal."); + } + } + + private TypeMirror nameToType(String name) { + return elements.getTypeElement(name).asType(); + } + private static void assertTrue(boolean cond, String msg) { assertionCount++; if (!cond)