1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/model/element/TestResourceVariable.java Fri Jul 16 19:35:24 2010 -0700 1.3 @@ -0,0 +1,113 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6911256 6964740 1.30 + * @summary Test that the resource variable kind is appropriately set 1.31 + * @author Joseph D. Darcy 1.32 + * @build TestResourceVariable 1.33 + * @compile/fail -processor TestResourceVariable -proc:only TestResourceVariable.java 1.34 + */ 1.35 + 1.36 +// Bug should be filed for this misbehavior 1.37 + 1.38 +import java.io.*; 1.39 +import javax.annotation.processing.*; 1.40 +import javax.lang.model.*; 1.41 +import javax.lang.model.element.*; 1.42 +import javax.lang.model.type.*; 1.43 +import javax.lang.model.util.*; 1.44 +import java.util.*; 1.45 +import com.sun.source.tree.*; 1.46 +import com.sun.source.util.*; 1.47 +import static javax.tools.Diagnostic.Kind.*; 1.48 + 1.49 +/** 1.50 + * Using the tree API, retrieve element representations of the 1.51 + * resource of an ARM block and verify their kind tags are set 1.52 + * appropriately. 1.53 + */ 1.54 +@SupportedAnnotationTypes("*") 1.55 +public class TestResourceVariable extends AbstractProcessor implements AutoCloseable { 1.56 + int resourceVariableCount = 0; 1.57 + 1.58 + public boolean process(Set<? extends TypeElement> annotations, 1.59 + RoundEnvironment roundEnv) { 1.60 + if (!roundEnv.processingOver()) { 1.61 + Trees trees = Trees.instance(processingEnv); 1.62 + 1.63 + for(Element rootElement : roundEnv.getRootElements()) { 1.64 + TreePath treePath = trees.getPath(rootElement); 1.65 + 1.66 + (new ResourceVariableScanner(trees)). 1.67 + scan(trees.getTree(rootElement), 1.68 + treePath.getCompilationUnit()); 1.69 + } 1.70 + if (resourceVariableCount != 3) 1.71 + throw new RuntimeException("Bad resource variable count " + 1.72 + resourceVariableCount); 1.73 + } 1.74 + return true; 1.75 + } 1.76 + 1.77 + @Override 1.78 + public void close() {} 1.79 + 1.80 + private void test1() { 1.81 + try(TestResourceVariable trv = this) {} 1.82 + } 1.83 + 1.84 + private void test2() { 1.85 + try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {} 1.86 + } 1.87 + 1.88 + class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> { 1.89 + private Trees trees; 1.90 + 1.91 + public ResourceVariableScanner(Trees trees) { 1.92 + super(); 1.93 + this.trees = trees; 1.94 + } 1.95 + @Override 1.96 + public Void visitVariable(VariableTree node, CompilationUnitTree cu) { 1.97 + Element element = trees.getElement(trees.getPath(cu, node)); 1.98 + if (element == null) { 1.99 + System.out.println("Null variable element: " + node); 1.100 + } else { 1.101 + System.out.println("Name: " + element.getSimpleName() + 1.102 + "\tKind: " + element.getKind()); 1.103 + } 1.104 + if (element != null && 1.105 + element.getKind() == ElementKind.RESOURCE_VARIABLE) { 1.106 + resourceVariableCount++; 1.107 + } 1.108 + return super.visitVariable(node, cu); 1.109 + } 1.110 + } 1.111 + 1.112 + @Override 1.113 + public SourceVersion getSupportedSourceVersion() { 1.114 + return SourceVersion.latest(); 1.115 + } 1.116 +}