darcy@609: /* darcy@609: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. darcy@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. darcy@609: * darcy@609: * This code is free software; you can redistribute it and/or modify it darcy@609: * under the terms of the GNU General Public License version 2 only, as darcy@609: * published by the Free Software Foundation. darcy@609: * darcy@609: * This code is distributed in the hope that it will be useful, but WITHOUT darcy@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or darcy@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License darcy@609: * version 2 for more details (a copy is included in the LICENSE file that darcy@609: * accompanied this code). darcy@609: * darcy@609: * You should have received a copy of the GNU General Public License version darcy@609: * 2 along with this work; if not, write to the Free Software Foundation, darcy@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. darcy@609: * darcy@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA darcy@609: * or visit www.oracle.com if you need additional information or have any darcy@609: * questions. darcy@609: */ darcy@609: darcy@609: /* darcy@609: * @test darcy@609: * @bug 6911256 6964740 darcy@609: * @summary Test that the resource variable kind is appropriately set darcy@609: * @author Joseph D. Darcy darcy@609: * @build TestResourceVariable darcy@609: * @compile/fail -processor TestResourceVariable -proc:only TestResourceVariable.java darcy@609: */ darcy@609: darcy@609: // Bug should be filed for this misbehavior darcy@609: darcy@609: import java.io.*; darcy@609: import javax.annotation.processing.*; darcy@609: import javax.lang.model.*; darcy@609: import javax.lang.model.element.*; darcy@609: import javax.lang.model.type.*; darcy@609: import javax.lang.model.util.*; darcy@609: import java.util.*; darcy@609: import com.sun.source.tree.*; darcy@609: import com.sun.source.util.*; darcy@609: import static javax.tools.Diagnostic.Kind.*; darcy@609: darcy@609: /** darcy@609: * Using the tree API, retrieve element representations of the darcy@609: * resource of an ARM block and verify their kind tags are set darcy@609: * appropriately. darcy@609: */ darcy@609: @SupportedAnnotationTypes("*") darcy@609: public class TestResourceVariable extends AbstractProcessor implements AutoCloseable { darcy@609: int resourceVariableCount = 0; darcy@609: darcy@609: public boolean process(Set annotations, darcy@609: RoundEnvironment roundEnv) { darcy@609: if (!roundEnv.processingOver()) { darcy@609: Trees trees = Trees.instance(processingEnv); darcy@609: darcy@609: for(Element rootElement : roundEnv.getRootElements()) { darcy@609: TreePath treePath = trees.getPath(rootElement); darcy@609: darcy@609: (new ResourceVariableScanner(trees)). darcy@609: scan(trees.getTree(rootElement), darcy@609: treePath.getCompilationUnit()); darcy@609: } darcy@609: if (resourceVariableCount != 3) darcy@609: throw new RuntimeException("Bad resource variable count " + darcy@609: resourceVariableCount); darcy@609: } darcy@609: return true; darcy@609: } darcy@609: darcy@609: @Override darcy@609: public void close() {} darcy@609: darcy@609: private void test1() { darcy@609: try(TestResourceVariable trv = this) {} darcy@609: } darcy@609: darcy@609: private void test2() { darcy@609: try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {} darcy@609: } darcy@609: darcy@609: class ResourceVariableScanner extends TreeScanner { darcy@609: private Trees trees; darcy@609: darcy@609: public ResourceVariableScanner(Trees trees) { darcy@609: super(); darcy@609: this.trees = trees; darcy@609: } darcy@609: @Override darcy@609: public Void visitVariable(VariableTree node, CompilationUnitTree cu) { darcy@609: Element element = trees.getElement(trees.getPath(cu, node)); darcy@609: if (element == null) { darcy@609: System.out.println("Null variable element: " + node); darcy@609: } else { darcy@609: System.out.println("Name: " + element.getSimpleName() + darcy@609: "\tKind: " + element.getKind()); darcy@609: } darcy@609: if (element != null && darcy@609: element.getKind() == ElementKind.RESOURCE_VARIABLE) { darcy@609: resourceVariableCount++; darcy@609: } darcy@609: return super.visitVariable(node, cu); darcy@609: } darcy@609: } darcy@609: darcy@609: @Override darcy@609: public SourceVersion getSupportedSourceVersion() { darcy@609: return SourceVersion.latest(); darcy@609: } darcy@609: }