Remove unnecessary parameters and fields / Clean-up

This commit is contained in:
Trustin Lee 2012-06-04 11:56:00 -07:00
parent 6b637ab22f
commit b322e98712
2 changed files with 60 additions and 67 deletions

View File

@ -19,8 +19,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
EventExecutor executor; // not thread-safe but OK because it never changes once set. EventExecutor executor; // not thread-safe but OK because it never changes once set.
private final String name; private final String name;
private final ChannelHandler handler; private final ChannelHandler handler;
private final boolean canHandleInbound;
private final boolean canHandleOutbound;
final ChannelBufferHolder<Object> in; final ChannelBufferHolder<Object> in;
final ChannelBufferHolder<Object> out; final ChannelBufferHolder<Object> out;
@ -41,7 +39,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
public void run() { public void run() {
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this; DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
try { try {
((ChannelInboundHandler<Object>) ctx.handler()).channelRegistered(ctx); ((ChannelInboundHandler<Object>) ctx.handler).channelRegistered(ctx);
} catch (Throwable t) { } catch (Throwable t) {
pipeline.notifyHandlerException(t); pipeline.notifyHandlerException(t);
} }
@ -53,7 +51,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
public void run() { public void run() {
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this; DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
try { try {
((ChannelInboundHandler<Object>) ctx.handler()).channelUnregistered(ctx); ((ChannelInboundHandler<Object>) ctx.handler).channelUnregistered(ctx);
} catch (Throwable t) { } catch (Throwable t) {
pipeline.notifyHandlerException(t); pipeline.notifyHandlerException(t);
} }
@ -65,7 +63,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
public void run() { public void run() {
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this; DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
try { try {
((ChannelInboundHandler<Object>) ctx.handler()).channelActive(ctx); ((ChannelInboundHandler<Object>) ctx.handler).channelActive(ctx);
} catch (Throwable t) { } catch (Throwable t) {
pipeline.notifyHandlerException(t); pipeline.notifyHandlerException(t);
} }
@ -77,7 +75,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
public void run() { public void run() {
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this; DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
try { try {
((ChannelInboundHandler<Object>) ctx.handler()).channelInactive(ctx); ((ChannelInboundHandler<Object>) ctx.handler).channelInactive(ctx);
} catch (Throwable t) { } catch (Throwable t) {
pipeline.notifyHandlerException(t); pipeline.notifyHandlerException(t);
} }
@ -90,13 +88,15 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this; DefaultChannelHandlerContext ctx = DefaultChannelHandlerContext.this;
flushBridge(); flushBridge();
try { try {
((ChannelInboundHandler<Object>) ctx.handler()).inboundBufferUpdated(ctx); ((ChannelInboundHandler<Object>) ctx.handler).inboundBufferUpdated(ctx);
} catch (Throwable t) { } catch (Throwable t) {
pipeline.notifyHandlerException(t); pipeline.notifyHandlerException(t);
} finally { } finally {
ChannelBufferHolder<Object> inbound = ctx.inbound(); if (inByteBridge != null) {
if (!inbound.isBypass() && inbound.isEmpty() && inbound.hasByteBuffer()) { ChannelBuffer buf = ctx.in.byteBuffer();
inbound.byteBuffer().discardReadBytes(); if (!buf.readable()) {
buf.discardReadBytes();
}
} }
} }
} }
@ -125,9 +125,9 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
if (handler == null) { if (handler == null) {
throw new NullPointerException("handler"); throw new NullPointerException("handler");
} }
canHandleInbound = handler instanceof ChannelInboundHandler;
canHandleOutbound = handler instanceof ChannelOutboundHandler;
boolean canHandleInbound = handler instanceof ChannelInboundHandler;
boolean canHandleOutbound = handler instanceof ChannelOutboundHandler;
if (!canHandleInbound && !canHandleOutbound) { if (!canHandleInbound && !canHandleOutbound) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"handler must be either " + "handler must be either " +
@ -293,12 +293,12 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
@Override @Override
public boolean canHandleInbound() { public boolean canHandleInbound() {
return canHandleInbound; return in != null;
} }
@Override @Override
public boolean canHandleOutbound() { public boolean canHandleOutbound() {
return canHandleOutbound; return out != null;
} }
@Override @Override

View File

@ -87,16 +87,14 @@ public class DefaultChannelPipeline implements ChannelPipeline {
new DefaultChannelHandlerContext(this, executor, head, nextCtx, name, handler); new DefaultChannelHandlerContext(this, executor, head, nextCtx, name, handler);
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) { if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
addFirst0(name, handler, nextCtx, newCtx); addFirst0(name, nextCtx, newCtx);
} else { } else {
try { try {
newCtx.executor().submit(new Runnable() { newCtx.executor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
checkDuplicateName(name); checkDuplicateName(name);
addFirst0(name, nextCtx, newCtx);
addFirst0(name, handler, nextCtx, newCtx);
} }
}).get(); }).get();
} catch (Throwable t) { } catch (Throwable t) {
@ -107,7 +105,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return this; return this;
} }
private void addFirst0(final String name, ChannelHandler handler, DefaultChannelHandlerContext nextCtx, DefaultChannelHandlerContext newCtx) { private void addFirst0(final String name, DefaultChannelHandlerContext nextCtx, DefaultChannelHandlerContext newCtx) {
callBeforeAdd(newCtx); callBeforeAdd(newCtx);
if (nextCtx != null) { if (nextCtx != null) {
@ -133,16 +131,14 @@ public class DefaultChannelPipeline implements ChannelPipeline {
if (!newTail.channel().isRegistered() || newTail.executor().inEventLoop()) { if (!newTail.channel().isRegistered() || newTail.executor().inEventLoop()) {
addLast0(name, handler, oldTail, newTail); addLast0(name, oldTail, newTail);
} else { } else {
try { try {
newTail.executor().submit(new Runnable() { newTail.executor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
checkDuplicateName(name); checkDuplicateName(name);
addLast0(name, oldTail, newTail);
addLast0(name, handler, oldTail, newTail);
} }
}).get(); }).get();
} catch (Throwable t) { } catch (Throwable t) {
@ -153,7 +149,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return this; return this;
} }
private void addLast0(final String name, ChannelHandler handler, DefaultChannelHandlerContext oldTail, DefaultChannelHandlerContext newTail) { private void addLast0(final String name, DefaultChannelHandlerContext oldTail, DefaultChannelHandlerContext newTail) {
callBeforeAdd(newTail); callBeforeAdd(newTail);
oldTail.next = newTail; oldTail.next = newTail;
@ -176,16 +172,14 @@ public class DefaultChannelPipeline implements ChannelPipeline {
new DefaultChannelHandlerContext(this, executor, ctx.prev, ctx, name, handler); new DefaultChannelHandlerContext(this, executor, ctx.prev, ctx, name, handler);
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) { if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
addBefore0(name, handler, ctx, newCtx); addBefore0(name, ctx, newCtx);
} else { } else {
try { try {
newCtx.executor().submit(new Runnable() { newCtx.executor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
checkDuplicateName(name); checkDuplicateName(name);
addBefore0(name, ctx, newCtx);
addBefore0(name, handler, ctx, newCtx);
} }
}).get(); }).get();
} catch (Throwable t) { } catch (Throwable t) {
@ -195,7 +189,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return this; return this;
} }
private void addBefore0(final String name, ChannelHandler handler, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) { private void addBefore0(final String name, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) {
callBeforeAdd(newCtx); callBeforeAdd(newCtx);
ctx.prev.next = newCtx; ctx.prev.next = newCtx;
@ -204,6 +198,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
callAfterAdd(newCtx); callAfterAdd(newCtx);
} }
@Override @Override
public ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) { public ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) {
return addAfter(null, baseName, name, handler); return addAfter(null, baseName, name, handler);
@ -220,16 +215,14 @@ public class DefaultChannelPipeline implements ChannelPipeline {
new DefaultChannelHandlerContext(this, executor, ctx, ctx.next, name, handler); new DefaultChannelHandlerContext(this, executor, ctx, ctx.next, name, handler);
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) { if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
addAfter0(name, handler, ctx, newCtx); addAfter0(name, ctx, newCtx);
} else { } else {
try { try {
newCtx.executor().submit(new Runnable() { newCtx.executor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
checkDuplicateName(name); checkDuplicateName(name);
addAfter0(name, ctx, newCtx);
addAfter0(name, handler, ctx, newCtx);
} }
}).get(); }).get();
} catch (Throwable t) { } catch (Throwable t) {
@ -242,7 +235,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return this; return this;
} }
private void addAfter0(final String name, ChannelHandler handler, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) { private void addAfter0(final String name, DefaultChannelHandlerContext ctx, DefaultChannelHandlerContext newCtx) {
checkDuplicateName(name); checkDuplicateName(name);
callBeforeAdd(newCtx); callBeforeAdd(newCtx);
@ -451,14 +444,13 @@ public class DefaultChannelPipeline implements ChannelPipeline {
new DefaultChannelHandlerContext(this, ctx.executor, prev, next, newName, newHandler); new DefaultChannelHandlerContext(this, ctx.executor, prev, next, newName, newHandler);
if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) { if (!newCtx.channel().isRegistered() || newCtx.executor().inEventLoop()) {
replace0(ctx, newName, newHandler, newCtx); replace0(ctx, newName, newCtx);
} else { } else {
try { try {
newCtx.executor().submit(new Runnable() { newCtx.executor().submit(new Runnable() {
@Override @Override
public void run() { public void run() {
replace0(ctx, newName, newHandler, newCtx); replace0(ctx, newName, newCtx);
} }
}).get(); }).get();
@ -466,13 +458,12 @@ public class DefaultChannelPipeline implements ChannelPipeline {
throw new ChannelException(t); throw new ChannelException(t);
} }
} }
} }
return ctx.handler(); return ctx.handler();
} }
private void replace0(DefaultChannelHandlerContext ctx, String newName, ChannelHandler newHandler, DefaultChannelHandlerContext newCtx) { private void replace0(DefaultChannelHandlerContext ctx, String newName, DefaultChannelHandlerContext newCtx) {
boolean sameName = ctx.name().equals(newName); boolean sameName = ctx.name().equals(newName);
DefaultChannelHandlerContext prev = ctx.prev; DefaultChannelHandlerContext prev = ctx.prev;
@ -520,6 +511,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
throw addException; throw addException;
} }
} }
private static void callBeforeAdd(ChannelHandlerContext ctx) { private static void callBeforeAdd(ChannelHandlerContext ctx) {
ChannelHandler handler = ctx.handler(); ChannelHandler handler = ctx.handler();
if (handler instanceof AbstractChannelHandler) { if (handler instanceof AbstractChannelHandler) {
@ -809,9 +801,8 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return bridge.byteBuf; return bridge.byteBuf;
} }
} }
ChannelBufferHolder<Object> in = ctx.in; if (ctx.inByteBridge != null) {
if (in != null && !in.isBypass() && in.hasByteBuffer()) { return ctx.in.byteBuffer();
return in.byteBuffer();
} }
ctx = ctx.next; ctx = ctx.next;
} }
@ -1298,9 +1289,11 @@ public class DefaultChannelPipeline implements ChannelPipeline {
} catch (Throwable t) { } catch (Throwable t) {
notifyHandlerException(t); notifyHandlerException(t);
} finally { } finally {
ChannelBufferHolder<Object> outbound = ctx.outbound(); if (ctx.outByteBridge != null) {
if (!outbound.isBypass() && outbound.isEmpty() && outbound.hasByteBuffer()) { ChannelBuffer buf = ctx.out.byteBuffer();
outbound.byteBuffer().discardReadBytes(); if (!buf.readable()) {
buf.discardReadBytes();
}
} }
} }
} }