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