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

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
parent 697
28b021bb889f
child 851
cad51b6eb7a6
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

darcy@609 1 /*
darcy@609 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
darcy@609 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@609 4 *
darcy@609 5 * This code is free software; you can redistribute it and/or modify it
darcy@609 6 * under the terms of the GNU General Public License version 2 only, as
darcy@609 7 * published by the Free Software Foundation.
darcy@609 8 *
darcy@609 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@609 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@609 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@609 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@609 13 * accompanied this code).
darcy@609 14 *
darcy@609 15 * You should have received a copy of the GNU General Public License version
darcy@609 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@609 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@609 18 *
darcy@609 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@609 20 * or visit www.oracle.com if you need additional information or have any
darcy@609 21 * questions.
darcy@609 22 */
darcy@609 23
darcy@609 24 /*
darcy@609 25 * @test
sundar@697 26 * @bug 6911256 6964740 6967842
darcy@609 27 * @summary Test that the resource variable kind is appropriately set
darcy@609 28 * @author Joseph D. Darcy
darcy@699 29 * @library ../../../lib
darcy@699 30 * @build JavacTestingAbstractProcessor TestResourceVariable
sundar@697 31 * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java
darcy@609 32 */
darcy@609 33
darcy@609 34 // Bug should be filed for this misbehavior
darcy@609 35
darcy@609 36 import java.io.*;
darcy@609 37 import javax.annotation.processing.*;
darcy@609 38 import javax.lang.model.*;
darcy@609 39 import javax.lang.model.element.*;
darcy@609 40 import javax.lang.model.type.*;
darcy@609 41 import javax.lang.model.util.*;
darcy@609 42 import java.util.*;
darcy@609 43 import com.sun.source.tree.*;
darcy@609 44 import com.sun.source.util.*;
darcy@609 45 import static javax.tools.Diagnostic.Kind.*;
darcy@609 46
darcy@609 47 /**
darcy@609 48 * Using the tree API, retrieve element representations of the
darcy@609 49 * resource of an ARM block and verify their kind tags are set
darcy@609 50 * appropriately.
darcy@609 51 */
darcy@699 52 public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable {
darcy@609 53 int resourceVariableCount = 0;
darcy@609 54
darcy@609 55 public boolean process(Set<? extends TypeElement> annotations,
darcy@609 56 RoundEnvironment roundEnv) {
darcy@609 57 if (!roundEnv.processingOver()) {
darcy@609 58 Trees trees = Trees.instance(processingEnv);
darcy@609 59
darcy@609 60 for(Element rootElement : roundEnv.getRootElements()) {
darcy@609 61 TreePath treePath = trees.getPath(rootElement);
darcy@609 62
darcy@609 63 (new ResourceVariableScanner(trees)).
darcy@609 64 scan(trees.getTree(rootElement),
darcy@609 65 treePath.getCompilationUnit());
darcy@609 66 }
darcy@609 67 if (resourceVariableCount != 3)
darcy@609 68 throw new RuntimeException("Bad resource variable count " +
darcy@609 69 resourceVariableCount);
darcy@609 70 }
darcy@609 71 return true;
darcy@609 72 }
darcy@609 73
darcy@609 74 @Override
darcy@609 75 public void close() {}
darcy@609 76
darcy@609 77 private void test1() {
darcy@609 78 try(TestResourceVariable trv = this) {}
darcy@609 79 }
darcy@609 80
darcy@609 81 private void test2() {
darcy@609 82 try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {}
darcy@609 83 }
darcy@609 84
darcy@609 85 class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
darcy@609 86 private Trees trees;
darcy@609 87
darcy@609 88 public ResourceVariableScanner(Trees trees) {
darcy@609 89 super();
darcy@609 90 this.trees = trees;
darcy@609 91 }
darcy@609 92 @Override
darcy@609 93 public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
darcy@609 94 Element element = trees.getElement(trees.getPath(cu, node));
darcy@609 95 if (element == null) {
darcy@609 96 System.out.println("Null variable element: " + node);
darcy@609 97 } else {
darcy@609 98 System.out.println("Name: " + element.getSimpleName() +
darcy@609 99 "\tKind: " + element.getKind());
darcy@609 100 }
darcy@609 101 if (element != null &&
darcy@609 102 element.getKind() == ElementKind.RESOURCE_VARIABLE) {
darcy@609 103 resourceVariableCount++;
darcy@609 104 }
darcy@609 105 return super.visitVariable(node, cu);
darcy@609 106 }
darcy@609 107 }
darcy@609 108 }

mercurial