/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed 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.undertow.websockets.jsr.util;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.IdentityHashMap;
import java.util.Map;

import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.MessageHandler;

import io.undertow.websockets.jsr.JsrWebSocketMessages;

/**
 * @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
 */
public final class ClassUtils {
    private ClassUtils() {
    }

    /**
     * Returns a map of all supported message types by the given handler class.
     * The key of the map is the supported message type; the value indicates
     * whether it is a partial message handler or not.
     *
     * @return a map of all supported message types by the given handler class.
     */
    public static Map<Class<?>, Boolean> getHandlerTypes(Class<? extends MessageHandler> clazz) {
        Map<Class<?>, Boolean> types = new IdentityHashMap<Class<?>, Boolean>(2);
        for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
            for (Type type : c.getGenericInterfaces()) {
                if (type instanceof ParameterizedType) {
                    ParameterizedType pt = (ParameterizedType) type;
                    Type rawType = pt.getRawType();
                    if (rawType == MessageHandler.Whole.class) {
                        Type messageType = pt.getActualTypeArguments()[0];
                        types.put((Class<?>) messageType, Boolean.FALSE);
                    } else if (rawType == MessageHandler.Partial.class) {
                        Type messageType = pt.getActualTypeArguments()[0];
                        types.put((Class<?>) messageType, Boolean.TRUE);
                    }
                }
            }
        }
        if (types.isEmpty()) {
            throw JsrWebSocketMessages.MESSAGES.unknownHandlerType(clazz);
        }
        return types;
    }

    /**
     * Returns the Object type for which the {@link Encoder} can be used.
     */
    public static Class<?> getEncoderType(Class<? extends Encoder> clazz) {
        Method[] methods = clazz.getMethods();
        for (Method m : methods) {
            if ("encode".equals(m.getName()) && !m.isBridge()) {
                return m.getParameterTypes()[0];
            }
        }
        throw JsrWebSocketMessages.MESSAGES.unknownEncoderType(clazz);
    }

    /**
     * Returns the Object type for which the {@link Encoder} can be used.
     */
    public static Class<?> getDecoderType(Class<? extends Decoder> clazz) {
        Method[] methods = clazz.getMethods();
        for (Method m : methods) {
            if ("decode".equals(m.getName()) && !m.isBridge()) {
                return m.getReturnType();
            }
        }
        throw JsrWebSocketMessages.MESSAGES.couldNotDetermineDecoderTypeFor(clazz);
    }

}

@@ -1775,32 +1775,324 @@
 rclass()) %7B%0A    
+        exampleGenericInterfaces(types, c);%0A        %7D%0A        if (types.isEmpty()) %7B%0A            throw JsrWebSocketMessages.MESSAGES.unknownHandlerType(clazz);%0A        %7D%0A        return types;%0A    %7D%0A%0A    private static void exampleGenericInterfaces(Map%3CClass%3C?%3E, Boolean%3E types, Class%3C?%3E c) %7B%0A
         for (Typ
@@ -2136,28 +2136,24 @@
             
-  
-  
 if (type ins
@@ -2177,28 +2177,24 @@
 izedType) %7B%0A
-    
             
@@ -2258,28 +2258,24 @@
             
-    
 Type rawType
@@ -2306,28 +2306,24 @@
             
- 
-   
 if (rawType 
@@ -2347,36 +2347,32 @@
 .Whole.class) %7B%0A
-    
                 
@@ -2438,36 +2438,32 @@
                 
-   
- 
 types.put((Class
@@ -2492,28 +2492,24 @@
 ean.FALSE);%0A
-    
             
@@ -2578,36 +2578,32 @@
                 
-    
 Type messageType
@@ -2649,36 +2649,32 @@
                 
- 
-   
 types.put((Class
@@ -2714,37 +2714,69 @@
                 
-    %7D
+%7D else if(rawType instanceof Class) %7B
 %0A               
@@ -2780,23 +2780,45 @@
     
-%7D%0A
     
-        %7D
+Class rawClass = (Class) rawType;
 %0A   
@@ -2814,34 +2814,36 @@
 awType;%0A        
-%7D%0A
+    
         if (type
@@ -2840,103 +2840,205 @@
   if
- (types.isEmpty()) %7B%0A            throw JsrWebSocketMessages.MESSAGES.unknownHandlerType(clazz);
+(rawClass.getGenericInterfaces() != null) %7B%0A                        exampleGenericInterfaces(types, rawClass);%0A                    %7D%0A                %7D%0A            %7D else if(type instanceof Class) %7B
 %0A   
@@ -3038,34 +3038,32 @@
 ) %7B%0A        
-%7D%0A
         
 return types
@@ -3046,37 +3046,93 @@
             
-return types;
+exampleGenericInterfaces(types, (Class)type);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    

Improve websocket endpoint type detection