class Owner< T extends AutoCloseable> implements AutoCloseable {
private T t;
this .t = t;
}
@Override
if ( t != null ) {
t.close ( ) ;
t = null ;
}
}
public T release( ) {
T result = t;
t = null ;
return result;
}
}
class Bender implements AutoCloseable {
private final boolean bad;
throw new Exception ( name
+ ": bite my shiny metal ass" ) ; }
public Bender
( String name,
boolean bad
) { this .name = name;
this .bad = bad;
}
@Override
if ( bad) {
throw new Exception ( name
+ ": kill all humans" ) ; } else {
System .
out .
println ( name
+ ": all hail to hypnotoad" ) ; }
}
}
public class Main implements AutoCloseable {
private Bender first;
private Bender second;
try ( Owner< Bender> first = new Owner< Bender> ( new Bender( "first" , false ) ) ) {
second = new Bender( "second" ) ;
this .first = first.release ( ) ;
}
}
try ( Owner< Bender> first = new Owner< Bender> ( new Bender( "first" , false ) ) ) {
second = new Bender( "second" , bad) ;
this .first = first.release ( ) ;
}
}
@Override
try ( Bender first = this .first ; Bender second = this .second ) {
}
}
public static void main
( String [ ] args
) { try ( Main twr = new Main( ) ) {
System .
out .
println ( "first test" ) ; e.
printStackTrace ( System .
out ) ; }
try ( Main twr = new Main( true ) ) {
System .
out .
println ( "second test" ) ; e.
printStackTrace ( System .
out ) ; }
try ( Main twr = new Main( false ) ) {
System .
out .
println ( "third test" ) ; e.
printStackTrace ( System .
out ) ; }
}
}
Y2xhc3MgT3duZXI8VCBleHRlbmRzIEF1dG9DbG9zZWFibGU+IGltcGxlbWVudHMgQXV0b0Nsb3NlYWJsZSB7CiAgICBwcml2YXRlIFQgdDsKCiAgICBwdWJsaWMgT3duZXIoVCB0KSB7CiAgICAgICAgdGhpcy50ID0gdDsKICAgIH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyB2b2lkIGNsb3NlKCkgdGhyb3dzIEV4Y2VwdGlvbiB7CiAgICAgICAgaWYgKHQgIT0gbnVsbCkgewogICAgICAgICAgICB0LmNsb3NlKCk7CiAgICAgICAgICAgIHQgPSBudWxsOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJ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GVyPihuZXcgQmVuZGVyKCJmaXJzdCIsIGZhbHNlKSkpIHsKICAgICAgICAgICAgc2Vjb25kID0gbmV3IEJlbmRlcigic2Vjb25kIik7CiAgICAgICAgICAgIHRoaXMuZmlyc3QgPSBmaXJzdC5yZWxlYXNlKCk7CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyBNYWluKGJvb2xlYW4gYmFkKSB0aHJvd3MgRXhjZXB0aW9uIHsKICAgICAgICB0cnkgKE93bmVyPEJlbmRlcj4gZmlyc3QgPSBuZXcgT3duZXI8QmVuZGVyPihuZXcgQmVuZGVyKCJmaXJzdCIsIGZhbHNlKSkpIHsKICAgICAgICAgICAgc2Vjb25kID0gbmV3IEJlbmRlcigic2Vjb25kIiwgYmFkKTsKICAgICAgICAgICAgdGhpcy5maXJzdCA9IGZpcnN0LnJlbGVhc2UoKTsKICAgICAgICB9CiAgICB9CgogICAgQE92ZXJyaWRlCiAgICBwdWJsaWMgdm9pZCBjbG9zZSgpIHRocm93cyBFeGNlcHRpb24gewogICAgICAgIHRyeSAoQmVuZGVyIGZpcnN0ID0gdGhpcy5maXJzdDsgQmVuZGVyIHNlY29uZCA9IHRoaXMuc2Vjb25kKSB7CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICB0cnkgKE1haW4gdHdyID0gbmV3IE1haW4oKSkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oImZpcnN0IHRlc3QiKTsKICAgICAgICB9IGNhdGNoIChFeGNlcHRpb24gZSkgewogICAgICAgICAgICBlLnByaW50U3RhY2tUcmFjZShTeXN0ZW0ub3V0KTsKICAgICAgICB9CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCk7CgogICAgICAgIHRyeSAoTWFpbiB0d3IgPSBuZXcgTWFpbih0cnVlKSkgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oInNlY29uZCB0ZXN0Iik7CiAgICAgICAgfSBjYXRjaCAoRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgZS5wcmludFN0YWNrVHJhY2UoU3lzdGVtLm91dCk7CiAgICAgICAgfQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigpOwoKICAgICAgICB0cnkgKE1haW4gdHdyID0gbmV3IE1haW4oZmFsc2UpKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigidGhpcmQgdGVzdCIpOwogICAgICAgIH0gY2F0Y2ggKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIGUucHJpbnRTdGFja1RyYWNlKFN5c3RlbS5vdXQpOwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgIH0KfQo=