Mon, 31 Aug 2015 14:49:47 -0700
Merge
1 /*
2 * Copyright (c) 2010, 2013, 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 */
24 /*
25 * @test
26 * @bug 8025113
27 * @author sogoel
28 * @summary Test shadowing of resource variable
29 */
31 /*
32 * "...a variable declared in a resource specification
33 * may be shadowed (6.3.1) anywhere inside a class declaration nested
34 * within the Block of the try."
35 */
36 public class ResourceShadow {
38 static final String str = "asdf"; //this is okay
40 /**
41 * Resource variable shadows switch and case variables
42 */
43 String test1() {
44 String ret = null;
45 switch (str) {
46 case str: //this is okay
47 try (SilentCloseable str = new SilentCloseable()) {
48 SilentCloseable tr = new SilentCloseable(str);
49 ret = str.getClass().getSimpleName();
50 }
51 break;
52 default:
53 ret = "";
54 }
55 return ret;
56 }
58 /**
59 * Resource variable may be shadowed (6.3.1) anywhere inside a class
60 * declaration nested within the Block of the try
61 */
62 String test2() {
63 String ret = null;
64 try (SilentCloseable str = new SilentCloseable()) {
65 class temp {
67 String str = "I am not a SilentCloseable";
69 public void printSTR() {
70 System.out.println(str);
71 }
73 public String getSTR() {
74 return str;
75 }
76 }
77 temp tmp = new temp();
78 SilentCloseable tr = new SilentCloseable(tmp.getSTR());
79 ret = tr.getMsg();
80 }
81 return ret;
82 }
84 public static void main(String... args) {
85 ResourceShadow t = new ResourceShadow();
86 if (t.test1().compareTo("SilentCloseable") != 0) {
87 throw new RuntimeException("FAIL-test1");
88 }
89 if (t.test2().compareTo("I am not a SilentCloseable") != 0) {
90 throw new RuntimeException("FAIL-test2");
91 }
92 }
93 }
95 class SilentCloseable implements AutoCloseable {
97 SilentCloseable testres = null;
98 String msg = "default";
100 @Override
101 public void close() {
102 }
104 public SilentCloseable() {
105 }
107 public SilentCloseable(String s) {
108 msg = s;
109 }
111 public SilentCloseable(SilentCloseable tr) {
112 testres = tr;
113 }
115 public String getMsg() {
116 return msg;
117 }
118 }