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