1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6967842
  27  * @summary Element not returned from tree API for ARM resource variables.
  28  * @author A. Sundararajan
  29  * @library ../../../lib
  30  * @build   JavacTestingAbstractProcessor TestResourceElement
  31  * @compile -processor TestResourceElement -proc:only TestResourceElement.java
  32  */
  33 
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.*;
  36 import javax.lang.model.element.*;
  37 import java.util.*;
  38 import com.sun.source.tree.*;
  39 import com.sun.source.util.*;
  40 
  41 public class TestResourceElement extends JavacTestingAbstractProcessor implements AutoCloseable {
  42     public boolean process(Set<? extends TypeElement> annotations,
  43                           RoundEnvironment roundEnv) {
  44        if (!roundEnv.processingOver()) {
  45            Trees trees = Trees.instance(processingEnv);
  46 
  47            for(Element rootElement : roundEnv.getRootElements()) {
  48                TreePath treePath = trees.getPath(rootElement);
  49 
  50                VariableScanner varScanner =  new VariableScanner(trees);
  51                varScanner.scan(trees.getTree(rootElement),
  52                         treePath.getCompilationUnit());
  53                if (varScanner.getTrvElement() == null) {
  54                    throw new AssertionError("Element is null for 'trv'");
  55                }
  56            }
  57        }
  58        return true;
  59     }
  60 
  61     @Override
  62     public void close() {}
  63 
  64     private void test1() {
  65         // The resource variable "trv"'s Element is checked.
  66         // Do not change the name of the variable.
  67         try(TestResourceElement trv = this) {}
  68     }
  69 
  70     class VariableScanner extends TreeScanner<Void, CompilationUnitTree> {
  71        private Trees trees;
  72        private Element trvElement;
  73 
  74        public VariableScanner(Trees trees) {
  75            super();
  76            this.trees = trees;
  77        }
  78        @Override
  79        public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
  80            // if this is "trv", get it's element.
  81            if (node.getName().contentEquals("trv")) {
  82                trvElement = trees.getElement(trees.getPath(cu, node));
  83            }
  84            return super.visitVariable(node, cu);
  85        }
  86 
  87        Element getTrvElement() {
  88            return trvElement;
  89        }
  90    }
  91 }