/*
 * 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() { }
}

@@ -754,37 +754,24 @@
 va.util.
-concurrent.Concurrent
+Identity
 HashMap;
@@ -792,29 +792,8 @@
 til.
-concurrent.Concurrent
 Map;
@@ -840,40 +840,8 @@
  %7B%0A%0A
-    // TODO: Use a weak key map%0A
     
@@ -861,26 +861,28 @@
 c final 
-Concurrent
+ThreadLocal%3C
 Map%3CClas
@@ -896,16 +896,17 @@
 lass%3C?%3E%3E
+%3E
  typeMap
@@ -916,18 +916,157 @@
 new 
-Concurrent
+ThreadLocal%3CMap%3CClass%3C?%3E, Class%3C?%3E%3E%3E() %7B%0A        @Override%0A        protected Map%3CClass%3C?%3E, Class%3C?%3E%3E initialValue() %7B%0A            return new Identity
 Hash
@@ -1091,16 +1091,33 @@
 s%3C?%3E%3E();
+%0A        %7D%0A    %7D;
 %0A%0A    pu
@@ -1259,24 +1259,107 @@
 ramIndex) %7B%0A
+        final Map%3CClass%3C?%3E, Class%3C?%3E%3E typeMap = TypeParameterFinder.typeMap.get();%0A
         fina

Use thread-local map instead of ConcurrentHashMap for faster instantiation of handler adapters