696N/A/*
696N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
696N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
696N/A *
696N/A * This code is free software; you can redistribute it and/or modify it
696N/A * under the terms of the GNU General Public License version 2 only, as
696N/A * published by the Free Software Foundation.
696N/A *
696N/A * This code is distributed in the hope that it will be useful, but WITHOUT
696N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
696N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
696N/A * version 2 for more details (a copy is included in the LICENSE file that
696N/A * accompanied this code).
696N/A *
696N/A * You should have received a copy of the GNU General Public License version
696N/A * 2 along with this work; if not, write to the Free Software Foundation,
696N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
696N/A *
696N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
696N/A * or visit www.oracle.com if you need additional information or have any
696N/A * questions.
696N/A */
696N/A
696N/A/*
696N/A * @test
696N/A * @bug 6967842
696N/A * @summary Element not returned from tree API for ARM resource variables.
696N/A * @author A. Sundararajan
698N/A * @library ../../../lib
698N/A * @build JavacTestingAbstractProcessor TestResourceElement
696N/A * @compile -processor TestResourceElement -proc:only TestResourceElement.java
696N/A */
696N/A
696N/Aimport javax.annotation.processing.*;
696N/Aimport javax.lang.model.*;
696N/Aimport javax.lang.model.element.*;
696N/Aimport java.util.*;
696N/Aimport com.sun.source.tree.*;
696N/Aimport com.sun.source.util.*;
696N/A
698N/Apublic class TestResourceElement extends JavacTestingAbstractProcessor implements AutoCloseable {
696N/A public boolean process(Set<? extends TypeElement> annotations,
696N/A RoundEnvironment roundEnv) {
696N/A if (!roundEnv.processingOver()) {
696N/A Trees trees = Trees.instance(processingEnv);
696N/A
696N/A for(Element rootElement : roundEnv.getRootElements()) {
696N/A TreePath treePath = trees.getPath(rootElement);
696N/A
696N/A VariableScanner varScanner = new VariableScanner(trees);
696N/A varScanner.scan(trees.getTree(rootElement),
696N/A treePath.getCompilationUnit());
696N/A if (varScanner.getTrvElement() == null) {
696N/A throw new AssertionError("Element is null for 'trv'");
696N/A }
696N/A }
696N/A }
696N/A return true;
696N/A }
696N/A
696N/A @Override
696N/A public void close() {}
696N/A
696N/A private void test1() {
696N/A // The resource variable "trv"'s Element is checked.
696N/A // Do not change the name of the variable.
696N/A try(TestResourceElement trv = this) {}
696N/A }
696N/A
696N/A class VariableScanner extends TreeScanner<Void, CompilationUnitTree> {
696N/A private Trees trees;
696N/A private Element trvElement;
696N/A
696N/A public VariableScanner(Trees trees) {
696N/A super();
696N/A this.trees = trees;
696N/A }
696N/A @Override
696N/A public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
696N/A // if this is "trv", get it's element.
696N/A if (node.getName().contentEquals("trv")) {
696N/A trvElement = trees.getElement(trees.getPath(cu, node));
696N/A }
696N/A return super.visitVariable(node, cu);
696N/A }
696N/A
696N/A Element getTrvElement() {
696N/A return trvElement;
696N/A }
696N/A }
696N/A}