darcy@609: /* katleman@1013: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. darcy@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. darcy@609: * darcy@609: * This code is free software; you can redistribute it and/or modify it darcy@609: * under the terms of the GNU General Public License version 2 only, as darcy@609: * published by the Free Software Foundation. darcy@609: * darcy@609: * This code is distributed in the hope that it will be useful, but WITHOUT darcy@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or darcy@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License darcy@609: * version 2 for more details (a copy is included in the LICENSE file that darcy@609: * accompanied this code). darcy@609: * darcy@609: * You should have received a copy of the GNU General Public License version darcy@609: * 2 along with this work; if not, write to the Free Software Foundation, darcy@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. darcy@609: * darcy@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA darcy@609: * or visit www.oracle.com if you need additional information or have any darcy@609: * questions. darcy@609: */ darcy@609: darcy@609: /* darcy@609: * @test darcy@840: * @bug 6911256 6964740 6965277 7013420 darcy@609: * @author Maurizio Cimadamore darcy@840: * @summary Check that lowered try-with-resources block does not end up creating resource twice darcy@609: */ darcy@609: darcy@609: import java.util.ArrayList; darcy@609: darcy@609: public class DuplicateResource { darcy@609: darcy@609: static class TestResource implements AutoCloseable { darcy@609: TestResource() { darcy@609: resources.add(this); darcy@609: } darcy@609: boolean isClosed = false; darcy@609: public void close() throws Exception { darcy@609: isClosed = true; darcy@609: } darcy@609: } darcy@609: darcy@609: static ArrayList resources = new ArrayList(); darcy@609: darcy@609: public static void main(String[] args) { darcy@840: try(TestResource tr = new TestResource()) { darcy@609: //do something darcy@609: } catch (Exception e) { darcy@609: throw new AssertionError("Shouldn't reach here", e); darcy@609: } darcy@609: check(); darcy@609: } darcy@609: darcy@609: public static void check() { darcy@609: if (resources.size() != 1) { darcy@609: throw new AssertionError("Expected one resource, found: " + resources.size()); darcy@609: } darcy@609: TestResource resource = resources.get(0); darcy@609: if (!resource.isClosed) { darcy@840: throw new AssertionError("Resource used in try-with-resources block has not been automatically closed"); darcy@609: } darcy@609: } darcy@609: }