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