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