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