test/tools/javac/processing/model/element/TestResourceElement.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 699
d2aaaec153e8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

sundar@697 1 /*
sundar@697 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
sundar@697 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
sundar@697 4 *
sundar@697 5 * This code is free software; you can redistribute it and/or modify it
sundar@697 6 * under the terms of the GNU General Public License version 2 only, as
sundar@697 7 * published by the Free Software Foundation.
sundar@697 8 *
sundar@697 9 * This code is distributed in the hope that it will be useful, but WITHOUT
sundar@697 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
sundar@697 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
sundar@697 12 * version 2 for more details (a copy is included in the LICENSE file that
sundar@697 13 * accompanied this code).
sundar@697 14 *
sundar@697 15 * You should have received a copy of the GNU General Public License version
sundar@697 16 * 2 along with this work; if not, write to the Free Software Foundation,
sundar@697 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
sundar@697 18 *
sundar@697 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
sundar@697 20 * or visit www.oracle.com if you need additional information or have any
sundar@697 21 * questions.
sundar@697 22 */
sundar@697 23
sundar@697 24 /*
sundar@697 25 * @test
sundar@697 26 * @bug 6967842
sundar@697 27 * @summary Element not returned from tree API for ARM resource variables.
sundar@697 28 * @author A. Sundararajan
darcy@1466 29 * @library /tools/javac/lib
darcy@699 30 * @build JavacTestingAbstractProcessor TestResourceElement
sundar@697 31 * @compile -processor TestResourceElement -proc:only TestResourceElement.java
sundar@697 32 */
sundar@697 33
sundar@697 34 import javax.annotation.processing.*;
sundar@697 35 import javax.lang.model.*;
sundar@697 36 import javax.lang.model.element.*;
sundar@697 37 import java.util.*;
sundar@697 38 import com.sun.source.tree.*;
sundar@697 39 import com.sun.source.util.*;
sundar@697 40
darcy@699 41 public class TestResourceElement extends JavacTestingAbstractProcessor implements AutoCloseable {
sundar@697 42 public boolean process(Set<? extends TypeElement> annotations,
sundar@697 43 RoundEnvironment roundEnv) {
sundar@697 44 if (!roundEnv.processingOver()) {
sundar@697 45 Trees trees = Trees.instance(processingEnv);
sundar@697 46
sundar@697 47 for(Element rootElement : roundEnv.getRootElements()) {
sundar@697 48 TreePath treePath = trees.getPath(rootElement);
sundar@697 49
sundar@697 50 VariableScanner varScanner = new VariableScanner(trees);
sundar@697 51 varScanner.scan(trees.getTree(rootElement),
sundar@697 52 treePath.getCompilationUnit());
sundar@697 53 if (varScanner.getTrvElement() == null) {
sundar@697 54 throw new AssertionError("Element is null for 'trv'");
sundar@697 55 }
sundar@697 56 }
sundar@697 57 }
sundar@697 58 return true;
sundar@697 59 }
sundar@697 60
sundar@697 61 @Override
sundar@697 62 public void close() {}
sundar@697 63
sundar@697 64 private void test1() {
sundar@697 65 // The resource variable "trv"'s Element is checked.
sundar@697 66 // Do not change the name of the variable.
sundar@697 67 try(TestResourceElement trv = this) {}
sundar@697 68 }
sundar@697 69
sundar@697 70 class VariableScanner extends TreeScanner<Void, CompilationUnitTree> {
sundar@697 71 private Trees trees;
sundar@697 72 private Element trvElement;
sundar@697 73
sundar@697 74 public VariableScanner(Trees trees) {
sundar@697 75 super();
sundar@697 76 this.trees = trees;
sundar@697 77 }
sundar@697 78 @Override
sundar@697 79 public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
sundar@697 80 // if this is "trv", get it's element.
sundar@697 81 if (node.getName().contentEquals("trv")) {
sundar@697 82 trvElement = trees.getElement(trees.getPath(cu, node));
sundar@697 83 }
sundar@697 84 return super.visitVariable(node, cu);
sundar@697 85 }
sundar@697 86
sundar@697 87 Element getTrvElement() {
sundar@697 88 return trvElement;
sundar@697 89 }
sundar@697 90 }
sundar@697 91 }

mercurial