@ -11,17 +11,19 @@ import java.io.StringReader;
public class CloseableAsResourceExample {
public class CloseableAsResourceExample {
native boolean star ( ) ;
class LocalException extends IOException {
class LocalException extends IOException {
}
}
class SomeResource implements Closeable {
class SomeResource implements Closeable {
native boolean isValid ( ) ;
void doSomething ( ) throws LocalException {
void doSomething ( ) throws LocalException {
if ( ! isValid ( ) ) {
if ( ! star ( ) ) {
throw new LocalException ( ) ;
throw new LocalException ( ) ;
}
}
}
}
public void close ( ) { }
public void close ( ) { }
}
}
@ -50,15 +52,15 @@ public class CloseableAsResourceExample {
res . close ( ) ;
res . close ( ) ;
} // should report a resource leak
} // should report a resource leak
class Res implements Closeable {
class Res ource implements Closeable {
public Res ( ) {
public Res ource ( ) {
}
}
public void close ( ) { }
public void close ( ) { }
}
}
class Wrapper implements Closeable {
class Wrapper implements Closeable {
Res mR ;
Res ource mR ;
public Wrapper ( Res r ) {
public Wrapper ( Res ource r ) {
mR = r ;
mR = r ;
}
}
public void close ( ) {
public void close ( ) {
@ -67,19 +69,19 @@ public class CloseableAsResourceExample {
}
}
class Sub extends Wrapper {
class Sub extends Wrapper {
public Sub ( Res r ) {
public Sub ( Res ource r ) {
super ( r ) ;
super ( r ) ;
}
}
}
}
void closingWrapper ( ) {
void closingWrapper ( ) {
Res r = new Res ( ) ;
Res ource r = new Res ource ( ) ;
Sub s = new Sub ( r ) ;
Sub s = new Sub ( r ) ;
s . close ( ) ;
s . close ( ) ;
}
}
void notClosingWrapper ( ) {
void notClosingWrapper ( ) {
Sub s = new Sub ( new Res ( ) ) ;
Sub s = new Sub ( new Res ource ( ) ) ;
s . mR . close ( ) ;
s . mR . close ( ) ;
} // should report a resource leak
} // should report a resource leak
@ -115,4 +117,23 @@ public class CloseableAsResourceExample {
}
}
}
}
class ResourceWithException implements Closeable {
public void close ( ) throws IOException {
if ( star ( ) ) {
throw new IOException ( ) ;
}
}
}
void noLeakwithExceptionOnClose ( ) throws IOException {
ResourceWithException res = new ResourceWithException ( ) ;
res . close ( ) ;
}
void noLeakWithCloseQuietlyAndExceptionOnClose ( ) {
ResourceWithException res = new ResourceWithException ( ) ;
Utils . closeQuietly ( res ) ;
}
}
}