Fix inspector warnings

- Move the methods used by an inner class to the inner class
- Removal of various redundant things (throws, parens, ..)
This commit is contained in:
Trustin Lee 2014-07-02 16:46:54 +09:00
parent 750f023ced
commit 66d8d8219a
3 changed files with 162 additions and 191 deletions

View File

@ -15,22 +15,6 @@
package io.netty.handler.codec.http2;
import static io.netty.handler.codec.http2.Http2CodecUtil.CONNECTION_STREAM_ID;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
import static io.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_STREAM_ID;
import static io.netty.handler.codec.http2.Http2CodecUtil.connectionPrefaceBuf;
import static io.netty.handler.codec.http2.Http2CodecUtil.toByteBuf;
import static io.netty.handler.codec.http2.Http2CodecUtil.toHttp2Exception;
import static io.netty.handler.codec.http2.Http2Error.NO_ERROR;
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED;
import static io.netty.handler.codec.http2.Http2Exception.protocolError;
import static io.netty.handler.codec.http2.Http2Stream.State.CLOSED;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_REMOTE;
import static io.netty.handler.codec.http2.Http2Stream.State.OPEN;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_REMOTE;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
@ -40,9 +24,14 @@ import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static io.netty.handler.codec.http2.Http2CodecUtil.*;
import static io.netty.handler.codec.http2.Http2Error.*;
import static io.netty.handler.codec.http2.Http2Exception.*;
import static io.netty.handler.codec.http2.Http2Stream.State.*;
/**
* Abstract base class for a handler of HTTP/2 frames. Handles reading and writing of HTTP/2 frames
* as well as management of connection state and flow control for both inbound and outbound data
@ -127,8 +116,8 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
}
// Create a local stream used for the HTTP cleartext upgrade.
createLocalStream(HTTP_UPGRADE_STREAM_ID, true, CONNECTION_STREAM_ID,
DEFAULT_PRIORITY_WEIGHT, false);
createLocalStream(HTTP_UPGRADE_STREAM_ID, true
);
}
/**
@ -149,8 +138,8 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
applyRemoteSettings(settings);
// Create a stream in the half-closed state.
createRemoteStream(HTTP_UPGRADE_STREAM_ID, true, CONNECTION_STREAM_ID,
DEFAULT_PRIORITY_WEIGHT, false);
createRemoteStream(HTTP_UPGRADE_STREAM_ID, true
);
}
@Override
@ -189,8 +178,9 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
ChannelFuture future = ctx.newSucceededFuture();
for (Http2Stream stream : connection.activeStreams().toArray(new Http2Stream[0])) {
close(stream, future);
final Collection<Http2Stream> streams = connection.activeStreams();
for (Http2Stream s : streams.toArray(new Http2Stream[streams.size()])) {
close(s, future);
}
super.channelInactive(ctx);
}
@ -239,7 +229,7 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
*/
@Override
public final void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
int padding, boolean endStream, boolean endSegment) throws Http2Exception {
int padding, boolean endStream, boolean endSegment) {
}
/**
@ -392,7 +382,7 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
Http2Stream stream = connection.stream(streamId);
if (stream == null) {
// Create a new locally-initiated stream.
stream = createLocalStream(streamId, endStream, streamDependency, weight, exclusive);
createLocalStream(streamId, endStream);
} else {
// An existing stream...
if (stream.state() == RESERVED_LOCAL) {
@ -533,7 +523,8 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
/**
* Processes the given exception. Depending on the type of exception, delegates to either
* {@link #processConnectionError} or {@link #processStreamError}.
* {@link #onConnectionError(ChannelHandlerContext, Http2Exception)} or
* {@link #onStreamError(ChannelHandlerContext, Http2StreamException)}.
*/
protected final void onHttp2Exception(ChannelHandlerContext ctx, Http2Exception e) {
if (e instanceof Http2StreamException) {
@ -693,26 +684,6 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
}
}
/**
* Closes the remote side of the given stream. If this causes the stream to be closed, adds a
* hook to close the channel after the given future completes.
*
* @param stream the stream to be half closed.
* @param future If closing, the future after which to close the channel. If {@code null},
* ignored.
*/
private void closeRemoteSide(Http2Stream stream, ChannelFuture future) {
switch (stream.state()) {
case HALF_CLOSED_REMOTE:
case OPEN:
stream.closeRemoteSide();
break;
default:
close(stream, future);
break;
}
}
/**
* Closes the given stream and adds a hook to close the channel after the given future completes.
*
@ -729,19 +700,10 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
}
}
/**
* Verifies that the HTTP/2 connection preface has been received from the remote endpoint.
*/
private void verifyPrefaceReceived() throws Http2Exception {
if (!prefaceReceived) {
throw protocolError("Received non-SETTINGS as first frame.");
}
}
/**
* Sends the HTTP/2 connection preface upon establishment of the connection, if not already sent.
*/
private void sendPreface(final ChannelHandlerContext ctx) throws Http2Exception {
private void sendPreface(final ChannelHandlerContext ctx) {
if (prefaceSent || !ctx.channel().isActive()) {
return;
}
@ -760,34 +722,6 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
ChannelFutureListener.CLOSE_ON_FAILURE);
}
/**
* Applies settings sent from the local endpoint.
*/
private void applyLocalSettings(Http2Settings settings) throws Http2Exception {
if (settings.hasPushEnabled()) {
if (connection.isServer()) {
throw protocolError("Server sending SETTINGS frame with ENABLE_PUSH specified");
}
connection.local().allowPushTo(settings.pushEnabled());
}
if (settings.hasAllowCompressedData()) {
connection.local().allowCompressedData(settings.allowCompressedData());
}
if (settings.hasMaxConcurrentStreams()) {
connection.remote().maxStreams(settings.maxConcurrentStreams());
}
if (settings.hasMaxHeaderTableSize()) {
frameReader.maxHeaderTableSize(settings.maxHeaderTableSize());
}
if (settings.hasInitialWindowSize()) {
inboundFlow.initialInboundWindowSize(settings.initialWindowSize());
}
}
/**
* Applies settings received from the remote endpoint.
*/
@ -819,16 +753,14 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
/**
* Creates a new stream initiated by the local endpoint.
*/
private Http2Stream createLocalStream(int streamId, boolean halfClosed, int streamDependency,
short weight, boolean exclusive) throws Http2Exception {
private Http2Stream createLocalStream(int streamId, boolean halfClosed) throws Http2Exception {
return connection.local().createStream(streamId, halfClosed);
}
/**
* Creates a new stream initiated by the remote endpoint.
*/
private Http2Stream createRemoteStream(int streamId, boolean halfClosed, int streamDependency,
short weight, boolean exclusive) throws Http2Exception {
private Http2Stream createRemoteStream(int streamId, boolean halfClosed) throws Http2Exception {
return connection.remote().createStream(streamId, halfClosed);
}
@ -855,8 +787,7 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
compressed, new Http2InboundFlowController.FrameWriter() {
@Override
public void writeFrame(int streamId, int windowSizeIncrement)
throws Http2Exception {
public void writeFrame(int streamId, int windowSizeIncrement) {
frameWriter.writeWindowUpdate(ctx, ctx.newPromise(), streamId,
windowSizeIncrement);
}
@ -877,6 +808,35 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
endOfSegment, compressed);
}
/**
* Verifies that the HTTP/2 connection preface has been received from the remote endpoint.
*/
private void verifyPrefaceReceived() throws Http2Exception {
if (!prefaceReceived) {
throw protocolError("Received non-SETTINGS as first frame.");
}
}
/**
* Closes the remote side of the given stream. If this causes the stream to be closed, adds a
* hook to close the channel after the given future completes.
*
* @param stream the stream to be half closed.
* @param future If closing, the future after which to close the channel. If {@code null},
* ignored.
*/
private void closeRemoteSide(Http2Stream stream, ChannelFuture future) {
switch (stream.state()) {
case HALF_CLOSED_REMOTE:
case OPEN:
stream.closeRemoteSide();
break;
default:
close(stream, future);
break;
}
}
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
int padding, boolean endStream, boolean endSegment) throws Http2Exception {
@ -893,13 +853,13 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
Http2Stream stream = connection.stream(streamId);
verifyGoAwayNotReceived();
verifyRstStreamNotReceived(stream);
if (connection.remote().isGoAwayReceived() || (stream != null && shouldIgnoreFrame(stream))) {
if (connection.remote().isGoAwayReceived() || stream != null && shouldIgnoreFrame(stream)) {
// Ignore this frame.
return;
}
if (stream == null) {
createRemoteStream(streamId, endStream, streamDependency, weight, exclusive);
createRemoteStream(streamId, endStream);
} else {
if (stream.state() == RESERVED_REMOTE) {
// Received headers for a reserved push stream ... open it for push to the
@ -979,6 +939,34 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
AbstractHttp2ConnectionHandler.this.onSettingsAckRead(ctx);
}
/**
* Applies settings sent from the local endpoint.
*/
private void applyLocalSettings(Http2Settings settings) throws Http2Exception {
if (settings.hasPushEnabled()) {
if (connection.isServer()) {
throw protocolError("Server sending SETTINGS frame with ENABLE_PUSH specified");
}
connection.local().allowPushTo(settings.pushEnabled());
}
if (settings.hasAllowCompressedData()) {
connection.local().allowCompressedData(settings.allowCompressedData());
}
if (settings.hasMaxConcurrentStreams()) {
connection.remote().maxStreams(settings.maxConcurrentStreams());
}
if (settings.hasMaxHeaderTableSize()) {
frameReader.maxHeaderTableSize(settings.maxHeaderTableSize());
}
if (settings.hasInitialWindowSize()) {
inboundFlow.initialInboundWindowSize(settings.initialWindowSize());
}
}
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings)
throws Http2Exception {
@ -1138,8 +1126,8 @@ public abstract class AbstractHttp2ConnectionHandler extends ByteToMessageDecode
FlowControlWriter(ChannelHandlerContext ctx, ByteBuf data, ChannelPromise promise) {
this.ctx = ctx;
this.promise = promise;
promises = new ArrayList<ChannelPromise>(
Arrays.asList(promise));
promises = new ArrayList<ChannelPromise>(4);
promises.add(promise);
remaining = data.readableBytes();
}

View File

@ -15,31 +15,22 @@
package io.netty.handler.codec.http2;
import static io.netty.handler.codec.http2.Http2CodecUtil.CONNECTION_STREAM_ID;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
import static io.netty.handler.codec.http2.Http2CodecUtil.MAX_WEIGHT;
import static io.netty.handler.codec.http2.Http2CodecUtil.MIN_WEIGHT;
import static io.netty.handler.codec.http2.Http2CodecUtil.immediateRemovalPolicy;
import static io.netty.handler.codec.http2.Http2Exception.format;
import static io.netty.handler.codec.http2.Http2Exception.protocolError;
import static io.netty.handler.codec.http2.Http2Stream.State.CLOSED;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_REMOTE;
import static io.netty.handler.codec.http2.Http2Stream.State.IDLE;
import static io.netty.handler.codec.http2.Http2Stream.State.OPEN;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_REMOTE;
import io.netty.handler.codec.http2.Http2StreamRemovalPolicy.Action;
import io.netty.util.collection.PrimitiveCollections;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
import io.netty.util.collection.PrimitiveCollections;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import static io.netty.handler.codec.http2.Http2CodecUtil.*;
import static io.netty.handler.codec.http2.Http2Exception.*;
import static io.netty.handler.codec.http2.Http2Stream.State.*;
/**
* Simple implementation of {@link Http2Connection}.
*/
@ -141,7 +132,7 @@ public class DefaultHttp2Connection implements Http2Connection {
@Override
public Set<Http2Stream> activeStreams() {
return java.util.Collections.unmodifiableSet(activeStreams);
return Collections.unmodifiableSet(activeStreams);
}
@Override
@ -159,17 +150,6 @@ public class DefaultHttp2Connection implements Http2Connection {
return localEndpoint.isGoAwayReceived() || remoteEndpoint.isGoAwayReceived();
}
private void addStream(DefaultStream stream) {
// Add the stream to the map and priority tree.
streamMap.put(stream.id(), stream);
connectionStream.addChild(stream, false);
// Notify the observers of the event.
for (Listener listener : listeners) {
listener.streamAdded(stream);
}
}
private void removeStream(DefaultStream stream) {
// Notify the observers of the event first.
for (Listener listener : listeners) {
@ -193,42 +173,6 @@ public class DefaultHttp2Connection implements Http2Connection {
}
}
private void deactivate(DefaultStream stream) {
activeStreams.remove(stream);
// Update the number of active streams initiated by the endpoint.
stream.createdBy().numActiveStreams--;
// Notify the listeners.
for (Listener listener : listeners) {
listener.streamInactive(stream);
}
}
private void notifyGoingAway() {
for (Listener listener : listeners) {
listener.goingAway();
}
}
private void notifyHalfClosed(Http2Stream stream) {
for (Listener listener : listeners) {
listener.streamHalfClosed(stream);
}
}
private void notifyPriorityChanged(Http2Stream stream, Http2Stream previousParent) {
for (Listener listener : listeners) {
listener.streamPriorityChanged(stream, previousParent);
}
}
private void notifyPrioritySubtreeChanged(Http2Stream stream, Http2Stream subtreeRoot) {
for (Listener listener : listeners) {
listener.streamPrioritySubtreeChanged(stream, subtreeRoot);
}
}
/**
* Simple stream implementation. Streams can be compared to each other by priority.
*/
@ -422,6 +366,18 @@ public class DefaultHttp2Connection implements Http2Connection {
}
}
private void notifyPriorityChanged(Http2Stream stream, Http2Stream previousParent) {
for (Listener listener : listeners) {
listener.streamPriorityChanged(stream, previousParent);
}
}
private void notifyPrioritySubtreeChanged(Http2Stream stream, Http2Stream subtreeRoot) {
for (Listener listener : listeners) {
listener.streamPrioritySubtreeChanged(stream, subtreeRoot);
}
}
@Override
public Http2Stream verifyState(Http2Error error, State... allowedStates)
throws Http2Exception {
@ -463,6 +419,18 @@ public class DefaultHttp2Connection implements Http2Connection {
return this;
}
private void deactivate(DefaultStream stream) {
activeStreams.remove(stream);
// Update the number of active streams initiated by the endpoint.
stream.createdBy().numActiveStreams--;
// Notify the listeners.
for (Listener listener : listeners) {
listener.streamInactive(stream);
}
}
@Override
public Http2Stream closeLocalSide() {
switch (state) {
@ -495,6 +463,12 @@ public class DefaultHttp2Connection implements Http2Connection {
return this;
}
private void notifyHalfClosed(Http2Stream stream) {
for (Listener listener : listeners) {
listener.streamHalfClosed(stream);
}
}
@Override
public final boolean remoteSideOpen() {
return state == HALF_CLOSED_LOCAL || state == OPEN || state == RESERVED_REMOTE;
@ -575,7 +549,7 @@ public class DefaultHttp2Connection implements Http2Connection {
}
}
private static <T> IntObjectMap<DefaultStream> newChildMap() {
private static IntObjectMap<DefaultStream> newChildMap() {
return new IntObjectHashMap<DefaultStream>(4);
}
@ -583,24 +557,22 @@ public class DefaultHttp2Connection implements Http2Connection {
* Stream class representing the connection, itself.
*/
private final class ConnectionStream extends DefaultStream {
public ConnectionStream() {
private ConnectionStream() {
super(CONNECTION_STREAM_ID);
}
@Override
public Http2Stream setPriority(int parentStreamId, short weight, boolean exclusive)
throws Http2Exception {
public Http2Stream setPriority(int parentStreamId, short weight, boolean exclusive) {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream verifyState(Http2Error error, State... allowedStates)
throws Http2Exception {
public Http2Stream verifyState(Http2Error error, State... allowedStates) {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream openForPush() throws Http2Exception {
public Http2Stream openForPush() {
throw new UnsupportedOperationException();
}
@ -727,6 +699,17 @@ public class DefaultHttp2Connection implements Http2Connection {
return stream;
}
private void addStream(DefaultStream stream) {
// Add the stream to the map and priority tree.
streamMap.put(stream.id(), stream);
connectionStream.addChild(stream, false);
// Notify the observers of the event.
for (Listener listener : listeners) {
listener.streamAdded(stream);
}
}
@Override
public void allowPushTo(boolean allow) {
if (allow && server) {
@ -789,6 +772,12 @@ public class DefaultHttp2Connection implements Http2Connection {
}
}
private void notifyGoingAway() {
for (Listener listener : listeners) {
listener.goingAway();
}
}
@Override
public Endpoint opposite() {
return isLocal() ? remoteEndpoint : localEndpoint;

View File

@ -15,14 +15,6 @@
package io.netty.handler.codec.http2;
import static io.netty.handler.codec.http2.Http2CodecUtil.CONNECTION_STREAM_ID;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_FLOW_CONTROL_WINDOW_SIZE;
import static io.netty.handler.codec.http2.Http2Error.FLOW_CONTROL_ERROR;
import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED;
import static io.netty.handler.codec.http2.Http2Exception.format;
import static io.netty.handler.codec.http2.Http2Exception.protocolError;
import static java.lang.Math.max;
import static java.lang.Math.min;
import io.netty.buffer.ByteBuf;
import java.util.ArrayDeque;
@ -32,6 +24,11 @@ import java.util.Comparator;
import java.util.List;
import java.util.Queue;
import static io.netty.handler.codec.http2.Http2CodecUtil.*;
import static io.netty.handler.codec.http2.Http2Error.*;
import static io.netty.handler.codec.http2.Http2Exception.*;
import static java.lang.Math.*;
/**
* Basic implementation of {@link Http2OutboundFlowController}.
*/
@ -55,7 +52,7 @@ public class DefaultHttp2OutboundFlowController implements Http2OutboundFlowCont
}
// Scale the data by the weight.
return (o1Data * o1.weight()) - (o2Data * o2.weight());
return o1Data * o1.weight() - o2Data * o2.weight();
}
};
@ -247,10 +244,7 @@ public class DefaultHttp2OutboundFlowController implements Http2OutboundFlowCont
* Recursively traverses the priority tree rooted at the given node. Attempts to write the
* allowed bytes for the streams in this sub tree based on their weighted priorities.
*
* @param priority the sub tree to write to. On the first invocation, this will be the root of
* the priority tree (i.e. the connection node).
* @param allowance an allowed number of bytes that may be written to the streams in this sub
* tree.
* @param allowance an allowed number of bytes that may be written to the streams in this subtree
*/
private void writeAllowedBytes(Http2Stream stream, int allowance) throws Http2Exception {
// Write the allowed bytes for this node. If not all of the allowance was used,
@ -388,7 +382,7 @@ public class DefaultHttp2OutboundFlowController implements Http2OutboundFlowCont
* value.
*/
int incrementStreamWindow(int delta) throws Http2Exception {
if (delta > 0 && (Integer.MAX_VALUE - delta) < window) {
if (delta > 0 && Integer.MAX_VALUE - delta < window) {
throw new Http2StreamException(stream.id(), FLOW_CONTROL_ERROR,
"Window size overflow for stream: " + stream.id());
}
@ -520,19 +514,6 @@ public class DefaultHttp2OutboundFlowController implements Http2OutboundFlowCont
return bytesWritten;
}
/**
* Increments the number of pending bytes for this node. If there was any change to the
* number of bytes that fit into the stream window, then {@link #incrementPriorityBytes} to
* recursively update this branch of the priority tree.
*/
private void incrementPendingBytes(int numBytes) {
int previouslyStreamable = streamableBytes();
pendingBytes += numBytes;
int delta = streamableBytes() - previouslyStreamable;
incrementPriorityBytes(delta);
}
/**
* Recursively increments the priority bytes for this branch in the priority tree starting
* at the current node.
@ -582,6 +563,19 @@ public class DefaultHttp2OutboundFlowController implements Http2OutboundFlowCont
}
}
/**
* Increments the number of pending bytes for this node. If there was any change to the
* number of bytes that fit into the stream window, then {@link #incrementPriorityBytes} to
* recursively update this branch of the priority tree.
*/
private void incrementPendingBytes(int numBytes) {
int previouslyStreamable = streamableBytes();
pendingBytes += numBytes;
int delta = streamableBytes() - previouslyStreamable;
incrementPriorityBytes(delta);
}
/**
* Writes the frame and decrements the stream and connection window sizes. If the frame
* is in the pending queue, the written bytes are removed from this branch of the