darcy@609: /* darcy@851: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. darcy@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. darcy@609: * darcy@609: * This code is free software; you can redistribute it and/or modify it darcy@609: * under the terms of the GNU General Public License version 2 only, as darcy@609: * published by the Free Software Foundation. darcy@609: * darcy@609: * This code is distributed in the hope that it will be useful, but WITHOUT darcy@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or darcy@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License darcy@609: * version 2 for more details (a copy is included in the LICENSE file that darcy@609: * accompanied this code). darcy@609: * darcy@609: * You should have received a copy of the GNU General Public License version darcy@609: * 2 along with this work; if not, write to the Free Software Foundation, darcy@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. darcy@609: * darcy@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA darcy@609: * or visit www.oracle.com if you need additional information or have any darcy@609: * questions. darcy@609: */ darcy@609: darcy@609: /* darcy@609: * @test darcy@1054: * @bug 6911256 6964740 6967842 6961571 7025809 darcy@609: * @summary Test that the resource variable kind is appropriately set darcy@609: * @author Joseph D. Darcy darcy@1466: * @library /tools/javac/lib darcy@699: * @build JavacTestingAbstractProcessor TestResourceVariable sundar@697: * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java darcy@609: */ darcy@609: darcy@609: import java.io.*; darcy@609: import javax.annotation.processing.*; darcy@609: import javax.lang.model.*; darcy@609: import javax.lang.model.element.*; darcy@609: import javax.lang.model.type.*; darcy@609: import javax.lang.model.util.*; darcy@609: import java.util.*; darcy@609: import com.sun.source.tree.*; darcy@609: import com.sun.source.util.*; darcy@609: import static javax.tools.Diagnostic.Kind.*; darcy@609: darcy@609: /** darcy@609: * Using the tree API, retrieve element representations of the darcy@1054: * resource of a try-with-resources statement and verify their kind darcy@1054: * tags are set appropriately. darcy@609: */ darcy@699: public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable { darcy@609: int resourceVariableCount = 0; darcy@609: darcy@609: public boolean process(Set annotations, darcy@609: RoundEnvironment roundEnv) { darcy@609: if (!roundEnv.processingOver()) { darcy@609: Trees trees = Trees.instance(processingEnv); darcy@609: darcy@609: for(Element rootElement : roundEnv.getRootElements()) { darcy@609: TreePath treePath = trees.getPath(rootElement); darcy@609: darcy@609: (new ResourceVariableScanner(trees)). darcy@609: scan(trees.getTree(rootElement), darcy@609: treePath.getCompilationUnit()); darcy@609: } darcy@609: if (resourceVariableCount != 3) darcy@609: throw new RuntimeException("Bad resource variable count " + darcy@609: resourceVariableCount); darcy@609: } darcy@609: return true; darcy@609: } darcy@609: darcy@609: @Override darcy@609: public void close() {} darcy@609: darcy@609: private void test1() { darcy@609: try(TestResourceVariable trv = this) {} darcy@609: } darcy@609: darcy@609: private void test2() { darcy@609: try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {} darcy@609: } darcy@609: darcy@851: /** darcy@851: * Verify that a resource variable modeled as an element behaves darcy@1054: * as expected under 6 and latest specific visitors. darcy@851: */ darcy@851: private static void testResourceVariable(Element element) { darcy@851: ElementVisitor visitor6 = new ElementKindVisitor6() {}; darcy@851: darcy@851: try { darcy@851: visitor6.visit(element); darcy@851: throw new RuntimeException("Expected UnknownElementException not thrown."); darcy@851: } catch (UnknownElementException uee) { darcy@851: ; // Expected. darcy@851: } darcy@851: darcy@1054: ElementKindVisitor visitorLatest = darcy@1054: new ElementKindVisitor() { darcy@851: @Override darcy@851: public Object visitVariableAsResourceVariable(VariableElement e, darcy@851: Void p) { darcy@851: return e; // a non-null value darcy@851: } darcy@851: }; darcy@851: darcy@1054: if (visitorLatest.visit(element) == null) { darcy@851: throw new RuntimeException("Null result of resource variable visitation."); darcy@851: } darcy@851: } darcy@851: darcy@609: class ResourceVariableScanner extends TreeScanner { darcy@609: private Trees trees; darcy@609: darcy@609: public ResourceVariableScanner(Trees trees) { darcy@609: super(); darcy@609: this.trees = trees; darcy@609: } darcy@609: @Override darcy@609: public Void visitVariable(VariableTree node, CompilationUnitTree cu) { darcy@609: Element element = trees.getElement(trees.getPath(cu, node)); darcy@851: darcy@851: System.out.println("Name: " + element.getSimpleName() + darcy@851: "\tKind: " + element.getKind()); darcy@851: if (element.getKind() == ElementKind.RESOURCE_VARIABLE) { darcy@851: testResourceVariable(element); darcy@609: resourceVariableCount++; darcy@609: } darcy@609: return super.visitVariable(node, cu); darcy@609: } darcy@851: } darcy@609: }