1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/model/element/TestResourceVariable.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,131 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2011, 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 6967842 6961571 7025809 1.30 + * @summary Test that the resource variable kind is appropriately set 1.31 + * @author Joseph D. Darcy 1.32 + * @library /tools/javac/lib 1.33 + * @build JavacTestingAbstractProcessor TestResourceVariable 1.34 + * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java 1.35 + */ 1.36 + 1.37 +import java.io.*; 1.38 +import javax.annotation.processing.*; 1.39 +import javax.lang.model.*; 1.40 +import javax.lang.model.element.*; 1.41 +import javax.lang.model.type.*; 1.42 +import javax.lang.model.util.*; 1.43 +import java.util.*; 1.44 +import com.sun.source.tree.*; 1.45 +import com.sun.source.util.*; 1.46 +import static javax.tools.Diagnostic.Kind.*; 1.47 + 1.48 +/** 1.49 + * Using the tree API, retrieve element representations of the 1.50 + * resource of a try-with-resources statement and verify their kind 1.51 + * tags are set appropriately. 1.52 + */ 1.53 +public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable { 1.54 + int resourceVariableCount = 0; 1.55 + 1.56 + public boolean process(Set<? extends TypeElement> annotations, 1.57 + RoundEnvironment roundEnv) { 1.58 + if (!roundEnv.processingOver()) { 1.59 + Trees trees = Trees.instance(processingEnv); 1.60 + 1.61 + for(Element rootElement : roundEnv.getRootElements()) { 1.62 + TreePath treePath = trees.getPath(rootElement); 1.63 + 1.64 + (new ResourceVariableScanner(trees)). 1.65 + scan(trees.getTree(rootElement), 1.66 + treePath.getCompilationUnit()); 1.67 + } 1.68 + if (resourceVariableCount != 3) 1.69 + throw new RuntimeException("Bad resource variable count " + 1.70 + resourceVariableCount); 1.71 + } 1.72 + return true; 1.73 + } 1.74 + 1.75 + @Override 1.76 + public void close() {} 1.77 + 1.78 + private void test1() { 1.79 + try(TestResourceVariable trv = this) {} 1.80 + } 1.81 + 1.82 + private void test2() { 1.83 + try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {} 1.84 + } 1.85 + 1.86 + /** 1.87 + * Verify that a resource variable modeled as an element behaves 1.88 + * as expected under 6 and latest specific visitors. 1.89 + */ 1.90 + private static void testResourceVariable(Element element) { 1.91 + ElementVisitor visitor6 = new ElementKindVisitor6<Void, Void>() {}; 1.92 + 1.93 + try { 1.94 + visitor6.visit(element); 1.95 + throw new RuntimeException("Expected UnknownElementException not thrown."); 1.96 + } catch (UnknownElementException uee) { 1.97 + ; // Expected. 1.98 + } 1.99 + 1.100 + ElementKindVisitor visitorLatest = 1.101 + new ElementKindVisitor<Object, Void>() { 1.102 + @Override 1.103 + public Object visitVariableAsResourceVariable(VariableElement e, 1.104 + Void p) { 1.105 + return e; // a non-null value 1.106 + } 1.107 + }; 1.108 + 1.109 + if (visitorLatest.visit(element) == null) { 1.110 + throw new RuntimeException("Null result of resource variable visitation."); 1.111 + } 1.112 + } 1.113 + 1.114 + class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> { 1.115 + private Trees trees; 1.116 + 1.117 + public ResourceVariableScanner(Trees trees) { 1.118 + super(); 1.119 + this.trees = trees; 1.120 + } 1.121 + @Override 1.122 + public Void visitVariable(VariableTree node, CompilationUnitTree cu) { 1.123 + Element element = trees.getElement(trees.getPath(cu, node)); 1.124 + 1.125 + System.out.println("Name: " + element.getSimpleName() + 1.126 + "\tKind: " + element.getKind()); 1.127 + if (element.getKind() == ElementKind.RESOURCE_VARIABLE) { 1.128 + testResourceVariable(element); 1.129 + resourceVariableCount++; 1.130 + } 1.131 + return super.visitVariable(node, cu); 1.132 + } 1.133 + } 1.134 +}