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