7029150: Project Coin: present union types from the tree API through to javax.lang.model

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2011-04-28 15:05:36 -07:00
parent 2999ef704d
commit e29746adb0
9 changed files with 371 additions and 18 deletions

View file

@ -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.annotation.processing.SupportedAnnotationTypes;
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 @@ public class ModelChecker extends JavacTestingAbstractProcessor {
}
@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<Void, Void>(){}.visit(expectedUnionType);
throw new RuntimeException("Expected UnknownTypeException not thrown.");
} catch (UnknownTypeException ute) {
; // Expected
}
UnionType unionType = new SimpleTypeVisitor7<UnionType, Void>(){
@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)