Tue, 01 Feb 2011 10:11:05 -0800
6961571: Update visitors to support ARM's ElementKind.RESOURCE_VARIABLE
Reviewed-by: jjg
darcy@609 | 1 | /* |
darcy@851 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
darcy@609 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
darcy@609 | 4 | * |
darcy@609 | 5 | * This code is free software; you can redistribute it and/or modify it |
darcy@609 | 6 | * under the terms of the GNU General Public License version 2 only, as |
darcy@609 | 7 | * published by the Free Software Foundation. |
darcy@609 | 8 | * |
darcy@609 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
darcy@609 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
darcy@609 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
darcy@609 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
darcy@609 | 13 | * accompanied this code). |
darcy@609 | 14 | * |
darcy@609 | 15 | * You should have received a copy of the GNU General Public License version |
darcy@609 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
darcy@609 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
darcy@609 | 18 | * |
darcy@609 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
darcy@609 | 20 | * or visit www.oracle.com if you need additional information or have any |
darcy@609 | 21 | * questions. |
darcy@609 | 22 | */ |
darcy@609 | 23 | |
darcy@609 | 24 | /* |
darcy@609 | 25 | * @test |
darcy@851 | 26 | * @bug 6911256 6964740 6967842 6961571 |
darcy@609 | 27 | * @summary Test that the resource variable kind is appropriately set |
darcy@609 | 28 | * @author Joseph D. Darcy |
darcy@699 | 29 | * @library ../../../lib |
darcy@699 | 30 | * @build JavacTestingAbstractProcessor TestResourceVariable |
sundar@697 | 31 | * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java |
darcy@609 | 32 | */ |
darcy@609 | 33 | |
darcy@609 | 34 | import java.io.*; |
darcy@609 | 35 | import javax.annotation.processing.*; |
darcy@609 | 36 | import javax.lang.model.*; |
darcy@609 | 37 | import javax.lang.model.element.*; |
darcy@609 | 38 | import javax.lang.model.type.*; |
darcy@609 | 39 | import javax.lang.model.util.*; |
darcy@609 | 40 | import java.util.*; |
darcy@609 | 41 | import com.sun.source.tree.*; |
darcy@609 | 42 | import com.sun.source.util.*; |
darcy@609 | 43 | import static javax.tools.Diagnostic.Kind.*; |
darcy@609 | 44 | |
darcy@609 | 45 | /** |
darcy@609 | 46 | * Using the tree API, retrieve element representations of the |
darcy@609 | 47 | * resource of an ARM block and verify their kind tags are set |
darcy@609 | 48 | * appropriately. |
darcy@609 | 49 | */ |
darcy@699 | 50 | public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable { |
darcy@609 | 51 | int resourceVariableCount = 0; |
darcy@609 | 52 | |
darcy@609 | 53 | public boolean process(Set<? extends TypeElement> annotations, |
darcy@609 | 54 | RoundEnvironment roundEnv) { |
darcy@609 | 55 | if (!roundEnv.processingOver()) { |
darcy@609 | 56 | Trees trees = Trees.instance(processingEnv); |
darcy@609 | 57 | |
darcy@609 | 58 | for(Element rootElement : roundEnv.getRootElements()) { |
darcy@609 | 59 | TreePath treePath = trees.getPath(rootElement); |
darcy@609 | 60 | |
darcy@609 | 61 | (new ResourceVariableScanner(trees)). |
darcy@609 | 62 | scan(trees.getTree(rootElement), |
darcy@609 | 63 | treePath.getCompilationUnit()); |
darcy@609 | 64 | } |
darcy@609 | 65 | if (resourceVariableCount != 3) |
darcy@609 | 66 | throw new RuntimeException("Bad resource variable count " + |
darcy@609 | 67 | resourceVariableCount); |
darcy@609 | 68 | } |
darcy@609 | 69 | return true; |
darcy@609 | 70 | } |
darcy@609 | 71 | |
darcy@609 | 72 | @Override |
darcy@609 | 73 | public void close() {} |
darcy@609 | 74 | |
darcy@609 | 75 | private void test1() { |
darcy@609 | 76 | try(TestResourceVariable trv = this) {} |
darcy@609 | 77 | } |
darcy@609 | 78 | |
darcy@609 | 79 | private void test2() { |
darcy@609 | 80 | try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {} |
darcy@609 | 81 | } |
darcy@609 | 82 | |
darcy@851 | 83 | /** |
darcy@851 | 84 | * Verify that a resource variable modeled as an element behaves |
darcy@851 | 85 | * as expected under 6 and 7 specific visitors. |
darcy@851 | 86 | */ |
darcy@851 | 87 | private static void testResourceVariable(Element element) { |
darcy@851 | 88 | ElementVisitor visitor6 = new ElementKindVisitor6<Void, Void>() {}; |
darcy@851 | 89 | |
darcy@851 | 90 | try { |
darcy@851 | 91 | visitor6.visit(element); |
darcy@851 | 92 | throw new RuntimeException("Expected UnknownElementException not thrown."); |
darcy@851 | 93 | } catch (UnknownElementException uee) { |
darcy@851 | 94 | ; // Expected. |
darcy@851 | 95 | } |
darcy@851 | 96 | |
darcy@851 | 97 | ElementKindVisitor7 visitor7 = new ElementKindVisitor7<Object, Void>() { |
darcy@851 | 98 | @Override |
darcy@851 | 99 | public Object visitVariableAsResourceVariable(VariableElement e, |
darcy@851 | 100 | Void p) { |
darcy@851 | 101 | return e; // a non-null value |
darcy@851 | 102 | } |
darcy@851 | 103 | }; |
darcy@851 | 104 | |
darcy@851 | 105 | if (visitor7.visit(element) == null) { |
darcy@851 | 106 | throw new RuntimeException("Null result of resource variable visitation."); |
darcy@851 | 107 | } |
darcy@851 | 108 | } |
darcy@851 | 109 | |
darcy@609 | 110 | class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> { |
darcy@609 | 111 | private Trees trees; |
darcy@609 | 112 | |
darcy@609 | 113 | public ResourceVariableScanner(Trees trees) { |
darcy@609 | 114 | super(); |
darcy@609 | 115 | this.trees = trees; |
darcy@609 | 116 | } |
darcy@609 | 117 | @Override |
darcy@609 | 118 | public Void visitVariable(VariableTree node, CompilationUnitTree cu) { |
darcy@609 | 119 | Element element = trees.getElement(trees.getPath(cu, node)); |
darcy@851 | 120 | |
darcy@851 | 121 | System.out.println("Name: " + element.getSimpleName() + |
darcy@851 | 122 | "\tKind: " + element.getKind()); |
darcy@851 | 123 | if (element.getKind() == ElementKind.RESOURCE_VARIABLE) { |
darcy@851 | 124 | testResourceVariable(element); |
darcy@609 | 125 | resourceVariableCount++; |
darcy@609 | 126 | } |
darcy@609 | 127 | return super.visitVariable(node, cu); |
darcy@609 | 128 | } |
darcy@851 | 129 | } |
darcy@609 | 130 | } |