]> source.dussan.org Git - jgit.git/commitdiff
Suppress resource warnings with Java 7 90/7790/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Wed, 10 Oct 2012 06:02:30 +0000 (08:02 +0200)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 25 Oct 2012 22:17:23 +0000 (15:17 -0700)
For streams that should not be closed, i.e. don't own an underlying
stream, and in-memory streams that do not need to be closed we just
suppress the warning. This mostly apply to test cases. GC is enough.

For streams with external resources (i.e. files) we add the necessary
call to close().

Change-Id: I4d883ba2e7d07f199fe57ccb3459ece00441a570

13 files changed:
org.eclipse.jgit.http.server/src/org/eclipse/jgit/http/server/GitSmartHttpTools.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileRepositoryBuilderTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/SideBandOutputStreamTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/EolCanonicalizingInputStreamTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/UnionInputStreamTest.java
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/GC.java
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryInserter.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java
org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java

index ab4d0da345d8314c1edc68baf46c9523f4f87240..92d575c2d040e55508d969a6b98c196e04406e95 100644 (file)
@@ -297,6 +297,7 @@ public class GitSmartHttpTools {
 
        private static void writeSideBand(OutputStream out, String textForGit)
                        throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                OutputStream msg = new SideBandOutputStream(CH_ERROR, SMALL_BUF, out);
                msg.write(Constants.encode("error: " + textForGit));
                msg.flush();
index c852d41638f43af853b3076d71a14ebf5304e012..fa9fd6e68e4dbc5853ddbede3f0170cf7b922884 100644 (file)
@@ -119,6 +119,7 @@ class ShowPackDelta extends TextBuiltin {
                        ptr++;
                ptr++;
 
+               @SuppressWarnings("resource" /* java 7 */)
                TemporaryBuffer.Heap raw = new TemporaryBuffer.Heap(bufArray.length);
                InflaterInputStream inf = new InflaterInputStream(
                                new ByteArrayInputStream(bufArray, ptr, bufArray.length));
index b75d0bdc4ccae82584e38d878a88127531e2d2ac..3f0bc043ac979995fffa4eeca8e4ff24deb353b0 100644 (file)
@@ -162,8 +162,9 @@ public class CheckoutCommandTest extends RepositoryTestCase {
                } catch (IOException e) {
                        // the test makes only sense if deletion of
                        // a file with open stream fails
+               } finally {
+                       fis.close();
                }
-               fis.close();
                FileUtils.delete(testFile);
                CheckoutCommand co = git.checkout();
                // delete Test.txt in branch test
index b6377482dca60939d7d82e7086cbad5539e2ab1c..8f3f48829649000bf1f007a88b03d48fc96b0f83 100644 (file)
@@ -112,6 +112,7 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
                }
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void absoluteGitDirRef() throws Exception {
                FileRepository repo1 = createWorkRepository();
@@ -129,6 +130,7 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
                assertEquals(dir, repo2.getWorkTree());
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void relativeGitDirRef() throws Exception {
                FileRepository repo1 = createWorkRepository();
@@ -147,6 +149,7 @@ public class FileRepositoryBuilderTest extends LocalDiskRepositoryTestCase {
                assertEquals(dir, repo2.getWorkTree());
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void scanWithGitDirRef() throws Exception {
                FileRepository repo1 = createWorkRepository();
index cb3c7d88f0064a4bffa12beaf57456406d43cd3f..baa0d094d59a702e1fced0d4684794991feb7d9d 100644 (file)
@@ -128,6 +128,7 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
                assertFalse(gen.next());
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void repositoryWithRootLevelSubmoduleAbsoluteRef()
                        throws IOException, ConfigInvalidException {
@@ -176,6 +177,7 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
                assertFalse(gen.next());
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void repositoryWithRootLevelSubmoduleRelativeRef()
                        throws IOException, ConfigInvalidException {
index 2924297de76a397c0d97dbfc0a3279e08bdfa4b0..717a58bcbb157e663d22bac2c926b11e125b787c 100644 (file)
@@ -78,8 +78,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_CH_DATA() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
+                               SMALL_BUF, rawOut);
                out.write(new byte[] { 'a', 'b', 'c' });
                out.flush();
                assertBuffer("0008\001abc");
@@ -87,8 +88,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_CH_PROGRESS() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_PROGRESS, SMALL_BUF, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_PROGRESS,
+                               SMALL_BUF, rawOut);
                out.write(new byte[] { 'a', 'b', 'c' });
                out.flush();
                assertBuffer("0008\002abc");
@@ -96,8 +98,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_CH_ERROR() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_ERROR, SMALL_BUF, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_ERROR,
+                               SMALL_BUF, rawOut);
                out.write(new byte[] { 'a', 'b', 'c' });
                out.flush();
                assertBuffer("0008\003abc");
@@ -105,8 +108,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_Small() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, SMALL_BUF, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
+                               SMALL_BUF, rawOut);
                out.write('a');
                out.write('b');
                out.write('c');
@@ -116,8 +120,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_SmallBlocks1() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, 6, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
+                               rawOut);
                out.write('a');
                out.write('b');
                out.write('c');
@@ -127,8 +132,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_SmallBlocks2() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, 6, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 6,
+                               rawOut);
                out.write(new byte[] { 'a', 'b', 'c' });
                out.flush();
                assertBuffer("0006\001a0006\001b0006\001c");
@@ -136,8 +142,9 @@ public class SideBandOutputStreamTest {
 
        @Test
        public void testWrite_SmallBlocks3() throws IOException {
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, 7, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA, 7,
+                               rawOut);
                out.write('a');
                out.write(new byte[] { 'b', 'c' });
                out.flush();
@@ -152,8 +159,9 @@ public class SideBandOutputStreamTest {
                        buf[i] = (byte) i;
                }
 
-               final SideBandOutputStream out;
-               out = new SideBandOutputStream(CH_DATA, MAX_BUF, rawOut);
+               @SuppressWarnings("resource" /* java 7 */)
+               final SideBandOutputStream out = new SideBandOutputStream(CH_DATA,
+                               MAX_BUF, rawOut);
                out.write(buf);
                out.flush();
 
@@ -167,6 +175,7 @@ public class SideBandOutputStreamTest {
                }
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        @Test
        public void testFlush() throws IOException {
                final int[] flushCnt = new int[1];
index 8b67532a97351ab5ca421071f877c214c7421f72..5f5968dbcf05da152e774b015c46777e8ef040a3 100644 (file)
@@ -353,6 +353,7 @@ public class TemporaryBufferTest {
 
        @Test
        public void testDestroyWhileOpen() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final TemporaryBuffer b = new TemporaryBuffer.LocalFile();
                try {
                        b.write(new TestRng(getName())
@@ -411,6 +412,7 @@ public class TemporaryBufferTest {
 
        @Test
        public void testHeap() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final TemporaryBuffer b = new TemporaryBuffer.Heap(2 * 8 * 1024);
                final byte[] r = new byte[8 * 1024];
                b.write(r);
index 52ad0139c6ce59fdadd31ff30ca70bbb35721b35..bd1524563bbcae92bc32ebc1dc455076b4c2dac1 100644 (file)
@@ -121,7 +121,9 @@ public class EolCanonicalizingInputStreamTest {
                        }
 
                        assertEquals(expected.length, read);
+                       cis2.close();
                }
+               cis1.close();
        }
 
        private static byte[] asBytes(String in) {
index daebffbc1bacf4deedb678529633172cdbd45aba..c213157b35b4417c6519d55e42e56c8502b1e198 100644 (file)
@@ -69,6 +69,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testReadSingleBytes() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream();
 
                assertTrue(u.isEmpty());
@@ -101,6 +102,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testReadByteBlocks() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream();
                u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
                u.add(new ByteArrayInputStream(new byte[] { 3 }));
@@ -124,6 +126,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testArrayConstructor() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream(
                                new ByteArrayInputStream(new byte[] { 1, 0, 2 }),
                                new ByteArrayInputStream(new byte[] { 3 }),
@@ -141,6 +144,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testMarkSupported() {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream();
                assertFalse(u.markSupported());
                u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
@@ -149,6 +153,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testSkip() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream();
                u.add(new ByteArrayInputStream(new byte[] { 1, 0, 2 }));
                u.add(new ByteArrayInputStream(new byte[] { 3 }));
@@ -171,6 +176,7 @@ public class UnionInputStreamTest {
 
        @Test
        public void testAutoCloseDuringRead() throws IOException {
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream();
                final boolean closed[] = new boolean[2];
                u.add(new ByteArrayInputStream(new byte[] { 1 }) {
@@ -248,6 +254,7 @@ public class UnionInputStreamTest {
                                throw new IOException("Expected");
                        }
                };
+               @SuppressWarnings("resource" /* java 7 */)
                final UnionInputStream u = new UnionInputStream(
                                new ByteArrayInputStream(new byte[]{1,2,3}),
                                errorReadStream);
index bf3a8a2b0ad140e772e93f3c3d1ef9e8d4a43bc1..b0fab6f4b8189d3e14b27e06cd31ae39b2f2766e 100644 (file)
@@ -662,6 +662,7 @@ public class GC {
                                                JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));
 
                        // write the packfile
+                       @SuppressWarnings("resource" /* java 7 */)
                        FileChannel channel = new FileOutputStream(tmpPack).getChannel();
                        OutputStream channelStream = Channels.newOutputStream(channel);
                        try {
@@ -673,6 +674,7 @@ public class GC {
                        }
 
                        // write the packindex
+                       @SuppressWarnings("resource")
                        FileChannel idxChannel = new FileOutputStream(tmpIdx).getChannel();
                        OutputStream idxStream = Channels.newOutputStream(idxChannel);
                        try {
index ffd5c4149f6a1b10ef384fbfcb0792c46a881cc8..a9e403a16852e21382966e5d4eec696473b22433 100644 (file)
@@ -146,6 +146,7 @@ class ObjectDirectoryInserter extends ObjectInserter {
                }
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        private File toTemp(final MessageDigest md, final int type, long len,
                        final InputStream is) throws IOException, FileNotFoundException,
                        Error {
@@ -185,6 +186,7 @@ class ObjectDirectoryInserter extends ObjectInserter {
                }
        }
 
+       @SuppressWarnings("resource" /* java 7 */)
        private File toTemp(final int type, final byte[] buf, final int pos,
                        final int len) throws IOException, FileNotFoundException {
                boolean delete = true;
index c7a94b29e31aab4aadc0652b6315f96737f37071..c2cda5498131d32e219e5e7510d9825495f9efa1 100644 (file)
@@ -1079,7 +1079,7 @@ public class UploadPack {
 
        private boolean reportInternalServerErrorOverSideband() {
                try {
-                       @SuppressWarnings("resource")
+                       @SuppressWarnings("resource" /* java 7 */)
                        SideBandOutputStream err = new SideBandOutputStream(
                                        SideBandOutputStream.CH_ERROR,
                                        SideBandOutputStream.SMALL_BUF,
@@ -1121,7 +1121,7 @@ public class UploadPack {
                } catch (ServiceMayNotContinueException noPack) {
                        if (sideband && noPack.getMessage() != null) {
                                noPack.setOutput();
-                               @SuppressWarnings("resource")
+                               @SuppressWarnings("resource" /* java 7 */)
                                SideBandOutputStream err = new SideBandOutputStream(
                                                SideBandOutputStream.CH_ERROR,
                                                SideBandOutputStream.SMALL_BUF, rawOut);
index 4abe4a875080a80bf8f463f9f0e99b3d965b5780..0975cd3b41d0a861aa8bf938bdb780f6329a7602 100644 (file)
@@ -216,6 +216,7 @@ public class IO {
                if (last < 0)
                        return ByteBuffer.wrap(out, 0, pos);
 
+               @SuppressWarnings("resource" /* java 7 */)
                TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
                tmp.write(out);
                tmp.write(last);