test/tools/javac/TryWithResources/ResourceShadow.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/TryWithResources/ResourceShadow.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, 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     8025113
    1.30 + * @author  sogoel
    1.31 + * @summary Test shadowing of resource variable
    1.32 + */
    1.33 +
    1.34 +/*
    1.35 + * "...a variable declared in a resource specification
    1.36 + * may be shadowed (6.3.1) anywhere inside a class declaration nested
    1.37 + * within the Block of the try."
    1.38 + */
    1.39 +public class ResourceShadow {
    1.40 +
    1.41 +    static final String str = "asdf";  //this is okay
    1.42 +
    1.43 +    /**
    1.44 +     * Resource variable shadows switch and case variables
    1.45 +     */
    1.46 +    String test1() {
    1.47 +        String ret = null;
    1.48 +        switch (str) {
    1.49 +            case str: //this is okay
    1.50 +                try (SilentCloseable str = new SilentCloseable()) {
    1.51 +                    SilentCloseable tr = new SilentCloseable(str);
    1.52 +                    ret = str.getClass().getSimpleName();
    1.53 +                }
    1.54 +                break;
    1.55 +            default:
    1.56 +                ret = "";
    1.57 +        }
    1.58 +        return ret;
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Resource variable may be shadowed (6.3.1) anywhere inside a class
    1.63 +     * declaration nested within the Block of the try
    1.64 +     */
    1.65 +    String test2() {
    1.66 +        String ret = null;
    1.67 +        try (SilentCloseable str = new SilentCloseable()) {
    1.68 +            class temp {
    1.69 +
    1.70 +                String str = "I am not a SilentCloseable";
    1.71 +
    1.72 +                public void printSTR() {
    1.73 +                    System.out.println(str);
    1.74 +                }
    1.75 +
    1.76 +                public String getSTR() {
    1.77 +                    return str;
    1.78 +                }
    1.79 +            }
    1.80 +            temp tmp = new temp();
    1.81 +            SilentCloseable tr = new SilentCloseable(tmp.getSTR());
    1.82 +            ret = tr.getMsg();
    1.83 +        }
    1.84 +        return ret;
    1.85 +    }
    1.86 +
    1.87 +    public static void main(String... args) {
    1.88 +        ResourceShadow t = new ResourceShadow();
    1.89 +        if (t.test1().compareTo("SilentCloseable") != 0) {
    1.90 +            throw new RuntimeException("FAIL-test1");
    1.91 +        }
    1.92 +        if (t.test2().compareTo("I am not a SilentCloseable") != 0) {
    1.93 +            throw new RuntimeException("FAIL-test2");
    1.94 +        }
    1.95 +    }
    1.96 +}
    1.97 +
    1.98 +class SilentCloseable implements AutoCloseable {
    1.99 +
   1.100 +    SilentCloseable testres = null;
   1.101 +    String msg = "default";
   1.102 +
   1.103 +    @Override
   1.104 +    public void close() {
   1.105 +    }
   1.106 +
   1.107 +    public SilentCloseable() {
   1.108 +    }
   1.109 +
   1.110 +    public SilentCloseable(String s) {
   1.111 +        msg = s;
   1.112 +    }
   1.113 +
   1.114 +    public SilentCloseable(SilentCloseable tr) {
   1.115 +        testres = tr;
   1.116 +    }
   1.117 +
   1.118 +    public String getMsg() {
   1.119 +        return msg;
   1.120 +    }
   1.121 +}
   1.122 +

mercurial