langtools/test/tools/javac/TryWithResources/TwrAvoidNullCheck.java
changeset 37638 aac00923d48e
equal deleted inserted replaced
37637:b47af0433922 37638:aac00923d48e
       
     1 /*
       
     2  * Copyright (c) 2016, 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 7020499
       
    27  * @summary Verify that try-with-resources desugaring is not generating unnecessary null checks
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.code
       
    31  *          jdk.compiler/com.sun.tools.javac.comp
       
    32  *          jdk.compiler/com.sun.tools.javac.main
       
    33  *          jdk.compiler/com.sun.tools.javac.tree
       
    34  *          jdk.compiler/com.sun.tools.javac.util
       
    35  * @build toolbox.ToolBox toolbox.JavacTask TwrAvoidNullCheck
       
    36  * @run main TwrAvoidNullCheck
       
    37  */
       
    38 
       
    39 import java.io.IOException;
       
    40 import java.util.Arrays;
       
    41 
       
    42 import com.sun.source.util.JavacTask;
       
    43 import com.sun.tools.javac.api.JavacTool;
       
    44 import com.sun.tools.javac.comp.AttrContext;
       
    45 import com.sun.tools.javac.comp.Env;
       
    46 import com.sun.tools.javac.comp.Lower;
       
    47 import com.sun.tools.javac.tree.JCTree;
       
    48 import com.sun.tools.javac.tree.JCTree.JCBinary;
       
    49 import com.sun.tools.javac.tree.TreeInfo;
       
    50 import com.sun.tools.javac.tree.TreeMaker;
       
    51 import com.sun.tools.javac.tree.TreeScanner;
       
    52 import com.sun.tools.javac.util.Context;
       
    53 import com.sun.tools.javac.util.Context.Factory;
       
    54 import com.sun.tools.javac.util.List;
       
    55 
       
    56 import toolbox.ToolBox;
       
    57 
       
    58 public class TwrAvoidNullCheck {
       
    59     public static void main(String... args) throws IOException {
       
    60         new TwrAvoidNullCheck().run();
       
    61     }
       
    62     void run() throws IOException {
       
    63         run("new Test()", false);
       
    64         run("null", true);
       
    65         run("System.getProperty(\"test\") != null ? new Test() : null", true);
       
    66     }
       
    67     void run(String resourceSpecification, boolean expected) throws IOException {
       
    68         String template = "public class Test implements AutoCloseable {\n" +
       
    69                           "    void t() {\n" +
       
    70                           "        try (Test resource = RESOURCE) { }\n" +
       
    71                           "    }\n" +
       
    72                           "    public void close() { }\n" +
       
    73                           "}\n";
       
    74         String code = template.replace("RESOURCE", resourceSpecification);
       
    75         Context ctx = new Context();
       
    76         DumpLower.preRegister(ctx);
       
    77         Iterable<ToolBox.JavaSource> files = Arrays.asList(new ToolBox.JavaSource(code));
       
    78         JavacTask task = JavacTool.create().getTask(null, null, null, null, null, files, ctx);
       
    79         task.call();
       
    80 
       
    81         boolean hasNullCheck = ((DumpLower) DumpLower.instance(ctx)).hasNullCheck;
       
    82 
       
    83         if (hasNullCheck != expected) {
       
    84             throw new IllegalStateException("expected: " + expected +
       
    85                                             "; actual: " + hasNullCheck +
       
    86                                             "; code: " + code);
       
    87         }
       
    88     }
       
    89 
       
    90     static class DumpLower extends Lower {
       
    91 
       
    92         public static void preRegister(Context ctx) {
       
    93             ctx.put(lowerKey, new Factory<Lower>() {
       
    94                 @Override
       
    95                 public Lower make(Context c) {
       
    96                     return new DumpLower(c);
       
    97                 }
       
    98             });
       
    99         }
       
   100 
       
   101         public DumpLower(Context context) {
       
   102             super(context);
       
   103         }
       
   104 
       
   105         boolean hasNullCheck;
       
   106 
       
   107         @Override
       
   108         public List<JCTree> translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMaker make) {
       
   109             List<JCTree> result = super.translateTopLevelClass(env, cdef, make);
       
   110 
       
   111             new TreeScanner() {
       
   112                 @Override
       
   113                 public void visitBinary(JCBinary tree) {
       
   114                     hasNullCheck |= tree.operator.getSimpleName().contentEquals("!=") &&
       
   115                                     "resource".equals(String.valueOf(TreeInfo.name(tree.lhs))) &&
       
   116                                     TreeInfo.isNull(tree.rhs);
       
   117                     super.visitBinary(tree);
       
   118                 }
       
   119             }.scan(result);
       
   120 
       
   121             return result;
       
   122         }
       
   123 
       
   124     }
       
   125 }