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  6911256 6964740
  27  * @summary Test that the resource variable kind is appropriately set
  28  * @author  Joseph D. Darcy
  29  * @library ../../../lib
  30  * @build   JavacTestingAbstractProcessor
  31  * @build TestResourceVariable
  32  * @compile/fail -processor TestResourceVariable -proc:only TestResourceVariable.java
  33  */
  34 
  35 // Bug should be filed for this misbehavior
  36 
  37 import java.io.*;
  38 import javax.annotation.processing.*;
  39 import javax.lang.model.*;
  40 import javax.lang.model.element.*;
  41 import javax.lang.model.type.*;
  42 import javax.lang.model.util.*;
  43 import java.util.*;
  44 import com.sun.source.tree.*;
  45 import com.sun.source.util.*;
  46 import static javax.tools.Diagnostic.Kind.*;
  47 
  48 /**
  49  * Using the tree API, retrieve element representations of the
  50  * resource of an ARM block and verify their kind tags are set
  51  * appropriately.
  52  */
  53 public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable {
  54     int resourceVariableCount = 0;
  55 
  56     public boolean process(Set<? extends TypeElement> annotations,
  57                           RoundEnvironment roundEnv) {
  58        if (!roundEnv.processingOver()) {
  59            Trees trees = Trees.instance(processingEnv);
  60 
  61            for(Element rootElement : roundEnv.getRootElements()) {
  62                TreePath treePath = trees.getPath(rootElement);
  63 
  64                (new ResourceVariableScanner(trees)).
  65                    scan(trees.getTree(rootElement),
  66                         treePath.getCompilationUnit());
  67            }
  68            if (resourceVariableCount != 3)
  69                throw new RuntimeException("Bad resource variable count " +
  70                                           resourceVariableCount);
  71        }
  72        return true;
  73     }
  74 
  75     @Override
  76     public void close() {}
  77 
  78     private void test1() {
  79         try(TestResourceVariable trv = this) {}
  80     }
  81 
  82     private void test2() {
  83         try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {}
  84     }
  85 
  86     class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
  87        private Trees trees;
  88 
  89        public ResourceVariableScanner(Trees trees) {
  90            super();
  91            this.trees = trees;
  92        }
  93        @Override
  94        public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
  95            Element element = trees.getElement(trees.getPath(cu, node));
  96            if (element == null) {
  97                System.out.println("Null variable element: " + node);
  98            } else {
  99                System.out.println("Name: " + element.getSimpleName() +
 100                                   "\tKind: " + element.getKind());
 101            }
 102            if (element != null &&
 103                element.getKind() == ElementKind.RESOURCE_VARIABLE) {
 104                resourceVariableCount++;
 105            }
 106            return super.visitVariable(node, cu);
 107        }
 108    }
 109 }