[#5239] Allow adding handlers to pipeline with null name.

Motivation:

While doing 8fe3c83e4c I made a change which disallowed using null as name for handlers in the pipeline (this generated a new name before).

Modifications:

Revert to old behaviour and adding test case.

Result:

Allow null name again
This commit is contained in:
Norman Maurer 2016-05-23 19:01:15 +02:00
parent 26a175cd94
commit 2618c2a649
3 changed files with 40 additions and 31 deletions

View File

@ -225,7 +225,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified name or handler is {@code null} * if the specified handler is {@code null}
*/ */
ChannelPipeline addFirst(String name, ChannelHandler handler); ChannelPipeline addFirst(String name, ChannelHandler handler);
@ -240,7 +240,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified name or handler is {@code null} * if the specified handler is {@code null}
*/ */
ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler); ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler);
@ -253,7 +253,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified name or handler is {@code null} * if the specified handler is {@code null}
*/ */
ChannelPipeline addLast(String name, ChannelHandler handler); ChannelPipeline addLast(String name, ChannelHandler handler);
@ -268,7 +268,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified name or handler is {@code null} * if the specified handler is {@code null}
*/ */
ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler); ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler);
@ -285,7 +285,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified baseName, name, or handler is {@code null} * if the specified baseName or handler is {@code null}
*/ */
ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler); ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler);
@ -304,7 +304,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified baseName, name, or handler is {@code null} * if the specified baseName or handler is {@code null}
*/ */
ChannelPipeline addBefore(EventExecutorGroup group, String baseName, String name, ChannelHandler handler); ChannelPipeline addBefore(EventExecutorGroup group, String baseName, String name, ChannelHandler handler);
@ -321,7 +321,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified baseName, name, or handler is {@code null} * if the specified baseName or handler is {@code null}
*/ */
ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler); ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);
@ -340,7 +340,7 @@ public interface ChannelPipeline
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if there's an entry with the same name already in the pipeline * if there's an entry with the same name already in the pipeline
* @throws NullPointerException * @throws NullPointerException
* if the specified baseName, name, or handler is {@code null} * if the specified baseName or handler is {@code null}
*/ */
ChannelPipeline addAfter(EventExecutorGroup group, String baseName, String name, ChannelHandler handler); ChannelPipeline addAfter(EventExecutorGroup group, String baseName, String name, ChannelHandler handler);
@ -456,7 +456,7 @@ public interface ChannelPipeline
* if a handler with the specified new name already exists in this * if a handler with the specified new name already exists in this
* pipeline, except for the handler to be replaced * pipeline, except for the handler to be replaced
* @throws NullPointerException * @throws NullPointerException
* if the specified old handler, new name, or new handler is * if the specified old handler or new handler is
* {@code null} * {@code null}
*/ */
ChannelPipeline replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler); ChannelPipeline replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler);
@ -476,7 +476,7 @@ public interface ChannelPipeline
* if a handler with the specified new name already exists in this * if a handler with the specified new name already exists in this
* pipeline, except for the handler to be replaced * pipeline, except for the handler to be replaced
* @throws NullPointerException * @throws NullPointerException
* if the specified old handler, new name, or new handler is * if the specified old handler or new handler is
* {@code null} * {@code null}
*/ */
ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler); ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler);
@ -497,7 +497,7 @@ public interface ChannelPipeline
* if a handler with the specified new name already exists in this * if a handler with the specified new name already exists in this
* pipeline, except for the handler to be replaced * pipeline, except for the handler to be replaced
* @throws NullPointerException * @throws NullPointerException
* if the specified old handler, new name, or new handler is * if the specified old handler or new handler is
* {@code null} * {@code null}
*/ */
<T extends ChannelHandler> T replace(Class<T> oldHandlerType, String newName, <T extends ChannelHandler> T replace(Class<T> oldHandlerType, String newName,

View File

@ -145,12 +145,8 @@ public class DefaultChannelPipeline implements ChannelPipeline {
final AbstractChannelHandlerContext newCtx; final AbstractChannelHandlerContext newCtx;
final EventExecutor executor; final EventExecutor executor;
synchronized (this) { synchronized (this) {
if (name == null) {
name = generateName(handler);
} else {
checkDuplicateName(name);
}
checkMultiplicity(handler); checkMultiplicity(handler);
name = filterName(name, handler);
newCtx = newContext(group, name, handler); newCtx = newContext(group, name, handler);
executor = executorSafe(newCtx.executor); executor = executorSafe(newCtx.executor);
@ -197,14 +193,9 @@ public class DefaultChannelPipeline implements ChannelPipeline {
final EventExecutor executor; final EventExecutor executor;
final AbstractChannelHandlerContext newCtx; final AbstractChannelHandlerContext newCtx;
synchronized (this) { synchronized (this) {
if (name == null) {
name = generateName(handler);
} else {
checkDuplicateName(name);
}
checkMultiplicity(handler); checkMultiplicity(handler);
newCtx = newContext(group, name, handler); newCtx = newContext(group, filterName(name, handler), handler);
executor = executorSafe(newCtx.executor); executor = executorSafe(newCtx.executor);
addLast0(newCtx); addLast0(newCtx);
@ -251,12 +242,8 @@ public class DefaultChannelPipeline implements ChannelPipeline {
final AbstractChannelHandlerContext ctx; final AbstractChannelHandlerContext ctx;
synchronized (this) { synchronized (this) {
checkMultiplicity(handler); checkMultiplicity(handler);
name = filterName(name, handler);
ctx = getContextOrDie(baseName); ctx = getContextOrDie(baseName);
if (name == null) {
name = generateName(handler);
} else {
checkDuplicateName(name);
}
newCtx = newContext(group, name, handler); newCtx = newContext(group, name, handler);
executor = executorSafe(newCtx.executor); executor = executorSafe(newCtx.executor);
@ -292,6 +279,14 @@ public class DefaultChannelPipeline implements ChannelPipeline {
ctx.prev = newCtx; ctx.prev = newCtx;
} }
private String filterName(String name, ChannelHandler handler) {
if (name == null) {
return generateName(handler);
}
checkDuplicateName(name);
return name;
}
@Override @Override
public final ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) { public final ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) {
return addAfter(null, baseName, name, handler); return addAfter(null, baseName, name, handler);
@ -299,15 +294,15 @@ public class DefaultChannelPipeline implements ChannelPipeline {
@Override @Override
public final ChannelPipeline addAfter( public final ChannelPipeline addAfter(
EventExecutorGroup group, String baseName, final String name, ChannelHandler handler) { EventExecutorGroup group, String baseName, String name, ChannelHandler handler) {
final EventExecutor executor; final EventExecutor executor;
final AbstractChannelHandlerContext newCtx; final AbstractChannelHandlerContext newCtx;
final AbstractChannelHandlerContext ctx; final AbstractChannelHandlerContext ctx;
synchronized (this) { synchronized (this) {
checkMultiplicity(handler); checkMultiplicity(handler);
name = filterName(name, handler);
ctx = getContextOrDie(baseName); ctx = getContextOrDie(baseName);
checkDuplicateName(name);
newCtx = newContext(group, name, handler); newCtx = newContext(group, name, handler);
executor = executorSafe(newCtx.executor); executor = executorSafe(newCtx.executor);
@ -365,7 +360,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
for (int i = size - 1; i >= 0; i --) { for (int i = size - 1; i >= 0; i --) {
ChannelHandler h = handlers[i]; ChannelHandler h = handlers[i];
addFirst(executor, generateName(h), h); addFirst(executor, null, h);
} }
return this; return this;
@ -386,7 +381,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
if (h == null) { if (h == null) {
break; break;
} }
addLast(executor, generateName(h), h); addLast(executor, null, h);
} }
return this; return this;

View File

@ -879,6 +879,20 @@ public class DefaultChannelPipelineTest {
} }
} }
@Test
public void testNullName() {
ChannelPipeline pipeline = new LocalChannel().pipeline();
pipeline.addLast(newHandler());
pipeline.addLast(null, newHandler());
pipeline.addFirst(newHandler());
pipeline.addFirst(null, newHandler());
pipeline.addLast("test", newHandler());
pipeline.addAfter("test", null, newHandler());
pipeline.addBefore("test", null, newHandler());
}
private static final class TestTask implements Runnable { private static final class TestTask implements Runnable {
private final ChannelPipeline pipeline; private final ChannelPipeline pipeline;