langtools/test/tools/javac/TryWithResources/TwrAvoidNullCheck.java
author jlahoda
Tue, 19 Apr 2016 14:16:39 +0200
changeset 37638 aac00923d48e
permissions -rw-r--r--
7020499: Project Coin: improvements to try-with-resources desugaring Summary: Avoid unnecessary check for resource nullness if the resource is known to be non-null; put resource closing code in a method that is shared by multiple try-with-resources. Reviewed-by: darcy, mcimadamore, vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37638
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     1
/*
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     4
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     8
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    14
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    18
 *
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    21
 * questions.
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    22
 */
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    23
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    24
/*
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    25
 * @test
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    26
 * @bug 7020499
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    27
 * @summary Verify that try-with-resources desugaring is not generating unnecessary null checks
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    28
 * @library /tools/lib
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.code
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.comp
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.main
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.tree
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    34
 *          jdk.compiler/com.sun.tools.javac.util
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    35
 * @build toolbox.ToolBox toolbox.JavacTask TwrAvoidNullCheck
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    36
 * @run main TwrAvoidNullCheck
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    37
 */
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    38
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    39
import java.io.IOException;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    40
import java.util.Arrays;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    41
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    42
import com.sun.source.util.JavacTask;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.api.JavacTool;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    44
import com.sun.tools.javac.comp.AttrContext;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    45
import com.sun.tools.javac.comp.Env;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    46
import com.sun.tools.javac.comp.Lower;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    47
import com.sun.tools.javac.tree.JCTree;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.JCBinary;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    49
import com.sun.tools.javac.tree.TreeInfo;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    50
import com.sun.tools.javac.tree.TreeMaker;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    51
import com.sun.tools.javac.tree.TreeScanner;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    52
import com.sun.tools.javac.util.Context;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    53
import com.sun.tools.javac.util.Context.Factory;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    54
import com.sun.tools.javac.util.List;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    55
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    56
import toolbox.ToolBox;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    57
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    58
public class TwrAvoidNullCheck {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    59
    public static void main(String... args) throws IOException {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    60
        new TwrAvoidNullCheck().run();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    61
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    62
    void run() throws IOException {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    63
        run("new Test()", false);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    64
        run("null", true);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    65
        run("System.getProperty(\"test\") != null ? new Test() : null", true);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    66
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    67
    void run(String resourceSpecification, boolean expected) throws IOException {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    68
        String template = "public class Test implements AutoCloseable {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    69
                          "    void t() {\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    70
                          "        try (Test resource = RESOURCE) { }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    71
                          "    }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    72
                          "    public void close() { }\n" +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    73
                          "}\n";
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    74
        String code = template.replace("RESOURCE", resourceSpecification);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    75
        Context ctx = new Context();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    76
        DumpLower.preRegister(ctx);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    77
        Iterable<ToolBox.JavaSource> files = Arrays.asList(new ToolBox.JavaSource(code));
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    78
        JavacTask task = JavacTool.create().getTask(null, null, null, null, null, files, ctx);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    79
        task.call();
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    80
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    81
        boolean hasNullCheck = ((DumpLower) DumpLower.instance(ctx)).hasNullCheck;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    82
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    83
        if (hasNullCheck != expected) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    84
            throw new IllegalStateException("expected: " + expected +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    85
                                            "; actual: " + hasNullCheck +
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    86
                                            "; code: " + code);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    87
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    88
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    89
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    90
    static class DumpLower extends Lower {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    91
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    92
        public static void preRegister(Context ctx) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    93
            ctx.put(lowerKey, new Factory<Lower>() {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    94
                @Override
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    95
                public Lower make(Context c) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    96
                    return new DumpLower(c);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    97
                }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    98
            });
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
    99
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   100
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   101
        public DumpLower(Context context) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   102
            super(context);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   103
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   104
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   105
        boolean hasNullCheck;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   106
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   107
        @Override
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   108
        public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   109
            List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   110
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   111
            new TreeScanner() {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   112
                @Override
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   113
                public void visitBinary(JCBinary tree) {
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   114
                    hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") &&
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   115
                                    "resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) &&
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   116
                                    TreeInfo.isNull(tree.rhs);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   117
                    super.visitBinary(tree);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   118
                }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   119
            }.scan(result);
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   120
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   121
            return result;
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   122
        }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   123
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   124
    }
aac00923d48e 7020499: Project Coin: improvements to try-with-resources desugaring
jlahoda
parents:
diff changeset
   125
}