From a05064d3ebd06a041258cd7b7a197da8818a0146 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 12 Nov 2012 13:14:24 +0900 Subject: [PATCH] Fix more inspection warnings + compilation errors --- .../http/multipart/AbstractDiskHttpData.java | 2 +- .../multipart/AbstractMemoryHttpData.java | 7 ++-- .../multipart/HttpPostRequestDecoder.java | 33 ++++++++--------- .../multipart/HttpPostRequestEncoder.java | 4 +-- .../websocketx/WebSocket08FrameEncoder.java | 2 +- .../handler/codec/spdy/SpdyFrameEncoder.java | 2 +- .../spdy/SpdyHttpResponseStreamIdHandler.java | 2 +- .../handler/codec/spdy/SpdyOrHttpChooser.java | 5 ++- ...tractCompatibleMarshallingEncoderTest.java | 10 +++--- .../io/netty/util/internal/DetectionUtil.java | 2 +- .../io/netty/util/internal/jzlib/Deflate.java | 5 +-- .../netty/util/internal/jzlib/InfCodes.java | 8 ++--- .../io/netty/util/internal/jzlib/Tree.java | 2 +- .../http/upload/HttpUploadServerHandler.java | 6 ++-- .../netty/channel/DefaultChannelPipeline.java | 22 ++++++------ .../netty/channel/group/CombinedIterator.java | 35 ++++++++++--------- .../io/netty/channel/socket/SctpMessage.java | 12 ++----- 17 files changed, 76 insertions(+), 83 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java index e42f0251a3..76a22ad938 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractDiskHttpData.java @@ -72,7 +72,7 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData { * @return a new Temp File from getDiskFilename(), default prefix, postfix and baseDirectory */ private File tempFile() throws IOException { - String newpostfix = null; + String newpostfix; String diskFilename = getDiskFilename(); if (diskFilename != null) { newpostfix = '_' + diskFilename; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java index 671c0406a8..837da05559 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/AbstractMemoryHttpData.java @@ -15,9 +15,6 @@ */ package io.netty.handler.codec.http.multipart; -import static io.netty.buffer.Unpooled.EMPTY_BUFFER; -import static io.netty.buffer.Unpooled.buffer; -import static io.netty.buffer.Unpooled.wrappedBuffer; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.HttpConstants; @@ -30,6 +27,8 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; +import static io.netty.buffer.Unpooled.*; + /** * Abstract Memory HttpData implementation */ @@ -157,7 +156,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData { return ""; } if (encoding == null) { - return getString(HttpConstants.DEFAULT_CHARSET); + encoding = HttpConstants.DEFAULT_CHARSET; } return byteBuf.toString(encoding); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java index 3d86fa9451..30e0dc97b1 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java @@ -441,8 +441,8 @@ public class HttpPostRequestDecoder { private void parseBodyAttributesStandard() throws ErrorDataDecoderException { int firstpos = undecodedChunk.readerIndex(); int currentpos = firstpos; - int equalpos = firstpos; - int ampersandpos = firstpos; + int equalpos; + int ampersandpos; if (currentStatus == MultiPartStatus.NOTSTARTED) { currentStatus = MultiPartStatus.DISPOSITION; } @@ -492,7 +492,6 @@ public class HttpPostRequestDecoder { contRead = false; } else { // Error - contRead = false; throw new ErrorDataDecoderException("Bad end of line"); } } else { @@ -553,7 +552,7 @@ public class HttpPostRequestDecoder { * errors */ private void parseBodyAttributes() throws ErrorDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e1) { @@ -562,8 +561,8 @@ public class HttpPostRequestDecoder { } int firstpos = undecodedChunk.readerIndex(); int currentpos = firstpos; - int equalpos = firstpos; - int ampersandpos = firstpos; + int equalpos; + int ampersandpos; if (currentStatus == MultiPartStatus.NOTSTARTED) { currentStatus = MultiPartStatus.DISPOSITION; } @@ -616,7 +615,6 @@ public class HttpPostRequestDecoder { } else { // Error sao.setReadPosition(0); - contRead = false; throw new ErrorDataDecoderException("Bad end of line"); } } else { @@ -833,7 +831,7 @@ public class HttpPostRequestDecoder { * @throws NotEnoughDataDecoderException */ void skipControlCharacters() throws NotEnoughDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e) { @@ -937,7 +935,7 @@ public class HttpPostRequestDecoder { } String[] contents = splitMultipartHeader(newline); if (contents[0].equalsIgnoreCase(HttpPostBodyUtil.CONTENT_DISPOSITION)) { - boolean checkSecondArg = false; + boolean checkSecondArg; if (currentStatus == MultiPartStatus.DISPOSITION) { checkSecondArg = contents[1].equalsIgnoreCase(HttpPostBodyUtil.FORM_DATA); } else { @@ -1218,7 +1216,7 @@ public class HttpPostRequestDecoder { * value */ private String readLine() throws NotEnoughDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e1) { @@ -1364,7 +1362,7 @@ public class HttpPostRequestDecoder { * value */ private String readDelimiter(String delimiter) throws NotEnoughDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e1) { @@ -1573,7 +1571,7 @@ public class HttpPostRequestDecoder { */ private void readFileUploadByteMultipart(String delimiter) throws NotEnoughDataDecoderException, ErrorDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e1) { @@ -1585,7 +1583,7 @@ public class HttpPostRequestDecoder { boolean newLine = true; int index = 0; int lastrealpos = sao.pos; - int lastPosition = undecodedChunk.readerIndex(); + int lastPosition; boolean found = false; while (sao.pos < sao.limit) { @@ -1769,7 +1767,7 @@ public class HttpPostRequestDecoder { * @throws ErrorDataDecoderException */ private void loadFieldMultipart(String delimiter) throws NotEnoughDataDecoderException, ErrorDataDecoderException { - SeekAheadOptimize sao = null; + SeekAheadOptimize sao; try { sao = new SeekAheadOptimize(undecodedChunk); } catch (SeekAheadNoBackArrayException e1) { @@ -1781,7 +1779,7 @@ public class HttpPostRequestDecoder { // found the decoder limit boolean newLine = true; int index = 0; - int lastPosition = undecodedChunk.readerIndex(); + int lastPosition; int lastrealpos = sao.pos; boolean found = false; @@ -1871,8 +1869,7 @@ public class HttpPostRequestDecoder { */ private static String cleanString(String field) { StringBuilder sb = new StringBuilder(field.length()); - int i = 0; - for (i = 0; i < field.length(); i++) { + for (int i = 0; i < field.length(); i++) { char nextChar = field.charAt(i); if (nextChar == HttpConstants.COLON) { sb.append(HttpConstants.SP); @@ -1976,7 +1973,7 @@ public class HttpPostRequestDecoder { valueEnd = HttpPostBodyUtil.findEndOfString(sb); headers.add(sb.substring(nameStart, nameEnd)); String svalue = sb.substring(valueStart, valueEnd); - String[] values = null; + String[] values; if (svalue.indexOf(';') >= 0) { values = StringUtil.split(svalue, ';'); } else { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java index 5f5d20eb00..1a590969f7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/multipart/HttpPostRequestEncoder.java @@ -467,7 +467,7 @@ public class HttpPostRequestEncoder implements ChunkedMessageInput { // previously a data field so CRLF internal.addValue("\r\n"); } - boolean localMixed = false; + boolean localMixed; if (duringMixedMode) { if (currentFileUpload != null && currentFileUpload.getName().equals(fileUpload.getName())) { // continue a mixed mode @@ -918,7 +918,7 @@ public class HttpPostRequestEncoder implements ChunkedMessageInput { isLastChunkSent = true; return new DefaultHttpChunk(EMPTY_BUFFER); } - ByteBuf buffer = null; + ByteBuf buffer; int size = HttpPostBodyUtil.chunkSize; // first test if previous buffer is not empty if (currentBuffer != null) { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java index 937035b447..34ff75673d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java @@ -168,7 +168,7 @@ public class WebSocket08FrameEncoder extends MessageToByteEncoder { out.writeInt(numEntries); for (Integer ID: IDs) { int id = ID.intValue(); - byte ID_flags = (byte) 0; + byte ID_flags = 0; if (spdySettingsFrame.isPersistValue(id)) { ID_flags |= SPDY_SETTINGS_PERSIST_VALUE; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java index 321625c454..2aeea40cdb 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpResponseStreamIdHandler.java @@ -40,7 +40,7 @@ public class SpdyHttpResponseStreamIdHandler extends @Override public HttpMessage encode(ChannelHandlerContext ctx, HttpMessage msg) throws Exception { Integer id = ids.poll(); - if (id != null && id != NO_ID && !msg.containsHeader(SpdyHttpHeaders.Names.STREAM_ID)) { + if (id != null && id.intValue() != NO_ID && !msg.containsHeader(SpdyHttpHeaders.Names.STREAM_ID)) { SpdyHttpHeaders.setStreamId(msg, id); } return msg; diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java index 4d7002a02e..c5301a0cba 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java @@ -15,8 +15,6 @@ */ package io.netty.handler.codec.spdy; -import javax.net.ssl.SSLEngine; - import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandler; @@ -31,6 +29,8 @@ import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.ssl.SslHandler; +import javax.net.ssl.SSLEngine; + /** * {@link ChannelInboundByteHandler} which is responsible to setup the {@link ChannelPipeline} either for * HTTP or SPDY. This offers an easy way for users to support both at the same time while not care to @@ -89,7 +89,6 @@ public abstract class SpdyOrHttpChooser extends ChannelHandlerAdapter implements throw new IllegalStateException("SslHandler is needed for SPDY"); } - ChannelPipeline pipeline = ctx.pipeline(); SelectedProtocol protocol = getProtocol(handler.getEngine()); switch (protocol) { case None: diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java index 8484a5a319..dc520738e6 100644 --- a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java @@ -26,6 +26,8 @@ import org.junit.Test; import java.io.IOException; +import static org.junit.Assert.*; + public abstract class AbstractCompatibleMarshallingEncoderTest { @Test @@ -39,18 +41,18 @@ public abstract class AbstractCompatibleMarshallingEncoderTest { EmbeddedByteChannel ch = new EmbeddedByteChannel(createEncoder()); ch.writeOutbound(testObject); - Assert.assertTrue(ch.finish()); + assertTrue(ch.finish()); ByteBuf buffer = ch.readOutbound(); Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration); unmarshaller.start(Marshalling.createByteInput(truncate(buffer).nioBuffer())); String read = (String) unmarshaller.readObject(); - Assert.assertEquals(testObject, read); + assertEquals(testObject, read); - Assert.assertEquals(-1, unmarshaller.read()); + assertEquals(-1, unmarshaller.read()); - Assert.assertNull(ch.readOutbound()); + assertNull(ch.readOutbound()); unmarshaller.finish(); unmarshaller.close(); diff --git a/common/src/main/java/io/netty/util/internal/DetectionUtil.java b/common/src/main/java/io/netty/util/internal/DetectionUtil.java index 4021bebb17..4948fa5d54 100644 --- a/common/src/main/java/io/netty/util/internal/DetectionUtil.java +++ b/common/src/main/java/io/netty/util/internal/DetectionUtil.java @@ -114,7 +114,7 @@ public final class DetectionUtil { } // Legacy properties - boolean tryUnsafe = false; + boolean tryUnsafe; if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) { tryUnsafe = SystemPropertyUtil.getBoolean("io.netty.tryUnsafe", true); } else { diff --git a/common/src/main/java/io/netty/util/internal/jzlib/Deflate.java b/common/src/main/java/io/netty/util/internal/jzlib/Deflate.java index eea1ee5a8d..0c5e389cf3 100644 --- a/common/src/main/java/io/netty/util/internal/jzlib/Deflate.java +++ b/common/src/main/java/io/netty/util/internal/jzlib/Deflate.java @@ -349,7 +349,7 @@ final class Deflate { int n; // iterates over all tree elements int prevlen = -1; // last emitted length int curlen; // length of current code - int nextlen = tree[0 * 2 + 1]; // length of next code + int nextlen = tree[1]; // length of next code int count = 0; // repeat count of the current code int max_count = 7; // max repeat count int min_count = 4; // min repeat count @@ -445,7 +445,7 @@ final class Deflate { int n; // iterates over all tree elements int prevlen = -1; // last emitted length int curlen; // length of current code - int nextlen = tree[0 * 2 + 1]; // length of next code + int nextlen = tree[1]; // length of next code int count = 0; // repeat count of the current code int max_count = 7; // max repeat count int min_count = 4; // min repeat count @@ -797,6 +797,7 @@ final class Deflate { int stored_len, // length of input block boolean eof // true if this is the last block for a file ) { + //noinspection PointlessArithmeticExpression send_bits((STORED_BLOCK << 1) + (eof? 1 : 0), 3); // send block type copy_block(buf, stored_len, true); // with header } diff --git a/common/src/main/java/io/netty/util/internal/jzlib/InfCodes.java b/common/src/main/java/io/netty/util/internal/jzlib/InfCodes.java index 7fb57bd2c3..685896421c 100644 --- a/common/src/main/java/io/netty/util/internal/jzlib/InfCodes.java +++ b/common/src/main/java/io/netty/util/internal/jzlib/InfCodes.java @@ -100,9 +100,9 @@ final class InfCodes { int j; // temporary storage int tindex; // temporary pointer int e; // extra bits or operation - int b = 0; // bit buffer - int k = 0; // bits in bit buffer - int p = 0; // input data pointer + int b; // bit buffer + int k; // bits in bit buffer + int p; // input data pointer int n; // bytes available there int q; // output window write pointer int m; // bytes to end of window or read pointer @@ -587,7 +587,6 @@ final class InfCodes { q, e); q += e; r += e; - e = 0; } r = 0; // copy rest from start of window } @@ -603,7 +602,6 @@ final class InfCodes { System.arraycopy(s.window, r, s.window, q, c); q += c; r += c; - c = 0; } break; } else if ((e & 64) == 0) { diff --git a/common/src/main/java/io/netty/util/internal/jzlib/Tree.java b/common/src/main/java/io/netty/util/internal/jzlib/Tree.java index 9ac87fd982..4b32f6a5e2 100644 --- a/common/src/main/java/io/netty/util/internal/jzlib/Tree.java +++ b/common/src/main/java/io/netty/util/internal/jzlib/Tree.java @@ -213,7 +213,7 @@ final class Tree { continue; } if (tree[m * 2 + 1] != bits) { - s.opt_len += ((long) bits - (long) tree[m * 2 + 1]) * + s.opt_len += ((long) bits - tree[m * 2 + 1]) * tree[m * 2]; tree[m * 2 + 1] = (short) bits; } diff --git a/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java b/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java index 93f59d17fd..3865f6672f 100644 --- a/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java +++ b/example/src/main/java/io/netty/example/http/upload/HttpUploadServerHandler.java @@ -192,7 +192,7 @@ public class HttpUploadServerHandler extends ChannelInboundMessageHandlerAdapter responseContent.append('o'); // example of reading chunk by chunk (minimize memory usage due to // Factory) - readHttpDataChunkByChunk(ctx.channel()); + readHttpDataChunkByChunk(); // example of reading only if at the end if (chunk.isLast()) { readHttpDataAllReceive(ctx.channel()); @@ -206,7 +206,7 @@ public class HttpUploadServerHandler extends ChannelInboundMessageHandlerAdapter * Example of reading all InterfaceHttpData from finished transfer */ private void readHttpDataAllReceive(Channel channel) { - List datas = null; + List datas; try { datas = decoder.getBodyHttpDatas(); } catch (NotEnoughDataDecoderException e1) { @@ -226,7 +226,7 @@ public class HttpUploadServerHandler extends ChannelInboundMessageHandlerAdapter /** * Example of reading request by chunk and getting values from chunk to chunk */ - private void readHttpDataChunkByChunk(Channel channel) { + private void readHttpDataChunkByChunk() { try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index 110819c98b..eb2c980b5f 100755 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -1433,20 +1433,22 @@ public class DefaultChannelPipeline implements ChannelPipeline { } private static boolean inExceptionCaught(Throwable cause) { - if (cause == null) { - return false; - } + for (;;) { + if (cause == null) { + return false; + } - StackTraceElement[] trace = cause.getStackTrace(); - if (trace != null) { - for (StackTraceElement t: trace) { - if ("exceptionCaught".equals(t.getMethodName())) { - return true; + StackTraceElement[] trace = cause.getStackTrace(); + if (trace != null) { + for (StackTraceElement t : trace) { + if ("exceptionCaught".equals(t.getMethodName())) { + return true; + } } } - } - return inExceptionCaught(cause.getCause()); + cause = cause.getCause(); + } } private void checkDuplicateName(String name) { diff --git a/transport/src/main/java/io/netty/channel/group/CombinedIterator.java b/transport/src/main/java/io/netty/channel/group/CombinedIterator.java index 5aa478c04d..1c42eb03a3 100644 --- a/transport/src/main/java/io/netty/channel/group/CombinedIterator.java +++ b/transport/src/main/java/io/netty/channel/group/CombinedIterator.java @@ -40,29 +40,30 @@ final class CombinedIterator implements Iterator { @Override public boolean hasNext() { - boolean hasNext = currentIterator.hasNext(); - if (hasNext) { - return true; - } + for (;;) { + if (currentIterator.hasNext()) { + return true; + } - if (currentIterator == i1) { - currentIterator = i2; - return hasNext(); - } else { - return false; + if (currentIterator == i1) { + currentIterator = i2; + } else { + return false; + } } } @Override public E next() { - try { - return currentIterator.next(); - } catch (NoSuchElementException e) { - if (currentIterator == i1) { - currentIterator = i2; - return next(); - } else { - throw e; + for (;;) { + try { + return currentIterator.next(); + } catch (NoSuchElementException e) { + if (currentIterator == i1) { + currentIterator = i2; + } else { + throw e; + } } } } diff --git a/transport/src/main/java/io/netty/channel/socket/SctpMessage.java b/transport/src/main/java/io/netty/channel/socket/SctpMessage.java index 1a921d2346..3e675f9e1c 100644 --- a/transport/src/main/java/io/netty/channel/socket/SctpMessage.java +++ b/transport/src/main/java/io/netty/channel/socket/SctpMessage.java @@ -116,14 +116,8 @@ public final class SctpMessage { @Override public String toString() { - return new StringBuilder(). - append("SctpFrame{"). - append("streamIdentifier="). - append(streamIdentifier). - append(", protocolIdentifier="). - append(protocolIdentifier). - append(", payloadBuffer="). - append(ByteBufUtil.hexDump(getPayloadBuffer())). - append('}').toString(); + return "SctpFrame{" + + "streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier + + ", payloadBuffer=" + ByteBufUtil.hexDump(getPayloadBuffer()) + '}'; } }