test/tools/javac/processing/model/element/TestResourceVariable.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 1054
111bbf1ad913
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
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@1054 26 * @bug 6911256 6964740 6967842 6961571 7025809
darcy@609 27 * @summary Test that the resource variable kind is appropriately set
darcy@609 28 * @author Joseph D. Darcy
darcy@1466 29 * @library /tools/javac/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@1054 47 * resource of a try-with-resources statement and verify their kind
darcy@1054 48 * tags are set 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@1054 85 * as expected under 6 and latest 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@1054 97 ElementKindVisitor visitorLatest =
darcy@1054 98 new ElementKindVisitor<Object, Void>() {
darcy@851 99 @Override
darcy@851 100 public Object visitVariableAsResourceVariable(VariableElement e,
darcy@851 101 Void p) {
darcy@851 102 return e; // a non-null value
darcy@851 103 }
darcy@851 104 };
darcy@851 105
darcy@1054 106 if (visitorLatest.visit(element) == null) {
darcy@851 107 throw new RuntimeException("Null result of resource variable visitation.");
darcy@851 108 }
darcy@851 109 }
darcy@851 110
darcy@609 111 class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
darcy@609 112 private Trees trees;
darcy@609 113
darcy@609 114 public ResourceVariableScanner(Trees trees) {
darcy@609 115 super();
darcy@609 116 this.trees = trees;
darcy@609 117 }
darcy@609 118 @Override
darcy@609 119 public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
darcy@609 120 Element element = trees.getElement(trees.getPath(cu, node));
darcy@851 121
darcy@851 122 System.out.println("Name: " + element.getSimpleName() +
darcy@851 123 "\tKind: " + element.getKind());
darcy@851 124 if (element.getKind() == ElementKind.RESOURCE_VARIABLE) {
darcy@851 125 testResourceVariable(element);
darcy@609 126 resourceVariableCount++;
darcy@609 127 }
darcy@609 128 return super.visitVariable(node, cu);
darcy@609 129 }
darcy@851 130 }
darcy@609 131 }

mercurial