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