1 /*
   2  * @test  /nodynamiccopyright/
   3  * @bug 6911256 6964740 6965277
   4  * @author Maurizio Cimadamore
   5  * @summary Check that lowered arm block does not end up creating resource twice
   6  */
   7 
   8 import java.util.ArrayList;
   9 
  10 public class DuplicateResource {
  11 
  12     static class TestResource implements AutoCloseable {
  13         TestResource() {
  14             resources.add(this);
  15         }
  16         boolean isClosed = false;
  17         public void close() throws Exception {
  18             isClosed = true;
  19         }
  20     }
  21 
  22     static ArrayList<TestResource> resources = new ArrayList<TestResource>();
  23 
  24     public static void main(String[] args) {
  25         try(new TestResource()) {
  26            //do something
  27         } catch (Exception e) {
  28             throw new AssertionError("Shouldn't reach here", e);
  29         }
  30         check();
  31     }
  32 
  33     public static void check() {
  34        if (resources.size() != 1) {
  35            throw new AssertionError("Expected one resource, found: " + resources.size());
  36        }
  37        TestResource resource = resources.get(0);
  38        if (!resource.isClosed) {
  39            throw new AssertionError("Resource used in ARM block has not been automatically closed");
  40        }
  41     }
  42 }