Extract type parameter finder code to a utility class
This commit is contained in:
parent
1033bec4cd
commit
71136390f1
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.netty.util.internal;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
public final class TypeParameterFinder {
|
||||||
|
|
||||||
|
// TODO: Use a weak key map
|
||||||
|
private static final ConcurrentMap<Class<?>, Class<?>> typeMap = new ConcurrentHashMap<Class<?>, Class<?>>();
|
||||||
|
|
||||||
|
public static Class<?> findActualTypeParameter(
|
||||||
|
final Object object, final Class<?> parameterizedSuperClass, final int typeParamIndex) {
|
||||||
|
final Class<?> thisClass = object.getClass();
|
||||||
|
Class<?> messageType = typeMap.get(thisClass);
|
||||||
|
if (messageType == null) {
|
||||||
|
Class<?> currentClass = thisClass;
|
||||||
|
for (;;) {
|
||||||
|
if (currentClass.getSuperclass() == parameterizedSuperClass) {
|
||||||
|
Type[] types = ((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
|
||||||
|
if (types.length - 1 < typeParamIndex || !(types[0] instanceof Class)) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"cannot determine the inbound message type of " + thisClass.getSimpleName());
|
||||||
|
}
|
||||||
|
|
||||||
|
messageType = (Class<?>) types[0];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
currentClass = currentClass.getSuperclass();
|
||||||
|
}
|
||||||
|
|
||||||
|
typeMap.put(thisClass, messageType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return messageType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private TypeParameterFinder() { }
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
io.netty.channel.DefaultChannelPromise.setFailure/*
|
/*
|
||||||
* Copyright 2012 The Netty Project
|
* Copyright 2012 The Netty Project
|
||||||
*
|
*
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
@ -17,11 +17,7 @@ package io.netty.channel;
|
|||||||
|
|
||||||
import io.netty.buffer.MessageBuf;
|
import io.netty.buffer.MessageBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
|
import io.netty.util.internal.TypeParameterFinder;
|
||||||
import java.lang.reflect.ParameterizedType;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ChannelHandler} which handles inbound messages of a specific type.
|
* {@link ChannelHandler} which handles inbound messages of a specific type.
|
||||||
@ -48,9 +44,6 @@ import java.util.concurrent.ConcurrentMap;
|
|||||||
public abstract class ChannelInboundMessageHandlerAdapter<I>
|
public abstract class ChannelInboundMessageHandlerAdapter<I>
|
||||||
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
|
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
|
||||||
|
|
||||||
private static final ConcurrentMap<Class<?>, Class<?>> messageTypeMap =
|
|
||||||
new ConcurrentHashMap<Class<?>, Class<?>>();
|
|
||||||
|
|
||||||
private final Class<?> acceptedMsgType;
|
private final Class<?> acceptedMsgType;
|
||||||
|
|
||||||
protected ChannelInboundMessageHandlerAdapter() {
|
protected ChannelInboundMessageHandlerAdapter() {
|
||||||
@ -61,32 +54,8 @@ public abstract class ChannelInboundMessageHandlerAdapter<I>
|
|||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
Class<? extends ChannelInboundMessageHandlerAdapter> parameterizedHandlerType,
|
Class<? extends ChannelInboundMessageHandlerAdapter> parameterizedHandlerType,
|
||||||
int messageTypeParamIndex) {
|
int messageTypeParamIndex) {
|
||||||
acceptedMsgType = findMessageType(parameterizedHandlerType, messageTypeParamIndex);
|
acceptedMsgType = TypeParameterFinder.findActualTypeParameter(
|
||||||
}
|
this, parameterizedHandlerType, messageTypeParamIndex);
|
||||||
|
|
||||||
private Class<?> findMessageType(Class<?> parameterizedHandlerType, int messageTypeParamIndex) {
|
|
||||||
final Class<?> thisClass = getClass();
|
|
||||||
Class<?> messageType = messageTypeMap.get(thisClass);
|
|
||||||
if (messageType == null) {
|
|
||||||
Class<?> currentClass = thisClass;
|
|
||||||
for (;;) {
|
|
||||||
if (currentClass.getSuperclass() == parameterizedHandlerType) {
|
|
||||||
Type[] types = ((ParameterizedType) currentClass.getGenericSuperclass()).getActualTypeArguments();
|
|
||||||
if (types.length - 1 < messageTypeParamIndex || !(types[0] instanceof Class)) {
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"cannot determine the inbound message type of " + thisClass.getSimpleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
messageType = (Class<?>) types[0];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
currentClass = currentClass.getSuperclass();
|
|
||||||
}
|
|
||||||
|
|
||||||
messageTypeMap.put(thisClass, messageType);
|
|
||||||
}
|
|
||||||
|
|
||||||
return messageType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user