001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.util;
021    
022    import java.lang.reflect.ParameterizedType;
023    import java.lang.reflect.Type;
024    import java.lang.reflect.TypeVariable;
025    
026    public class TypeResolver {
027    
028      public static Class<?> resolveToClass(Type implementation, Class<?> type, int parameterIndex) {
029        if (implementation == null) {
030          throw new NullPointerException("No null type accepted");
031        }
032    
033        // First resolve to type
034        Type resolvedType = resolve(implementation, type, parameterIndex);
035    
036        //
037        if (resolvedType != null) {
038          return resolveToClass(resolvedType);
039        } else {
040          return null;
041        }
042      }
043    
044      public static Class resolveToClass(Type type) {
045        if (type == null) {
046          throw new NullPointerException("No null type accepted");
047        }
048        if (type instanceof Class<?>) {
049          return (Class<?>)type;
050        } else if (type instanceof TypeVariable) {
051          TypeVariable resolvedTypeVariable = (TypeVariable)type;
052          return resolveToClass(resolvedTypeVariable.getBounds()[0]);
053        } else {
054          throw new UnsupportedOperationException("Type resolution of " + type + " not yet implemented");
055        }
056      }
057    
058      /**
059       * A simplistic implementation, it may not handle all cases but it should handle enough.
060       *
061       * @param implementation the type for which the parameter requires a resolution
062       * @param type the type that owns the parameter
063       * @param parameterIndex the parameter index
064       * @return the resolved type
065       */
066      public static Type resolve(Type implementation, Class<?> type, int parameterIndex) {
067        if (implementation == null) {
068          throw new NullPointerException();
069        }
070    
071        //
072        if (implementation == type) {
073          TypeVariable<? extends Class<?>>[] tp = type.getTypeParameters();
074          if (parameterIndex < tp.length) {
075            return tp[parameterIndex];
076          } else {
077            throw new IllegalArgumentException();
078          }
079        } else if (implementation instanceof Class<?>) {
080          Class<?> c = (Class<?>) implementation;
081          Type gsc = c.getGenericSuperclass();
082          Type resolved = null;
083          if (gsc != null) {
084            resolved = resolve(gsc, type, parameterIndex);
085            if (resolved == null) {
086              // Try with interface
087            }
088          }
089          return resolved;
090        } else if (implementation instanceof ParameterizedType) {
091          ParameterizedType pt = (ParameterizedType) implementation;
092          Type[] typeArgs = pt.getActualTypeArguments();
093          Type rawType = pt.getRawType();
094          if (rawType == type) {
095            return typeArgs[parameterIndex];
096          } else if (rawType instanceof Class<?>) {
097            Class<?> classRawType = (Class<?>)rawType;
098            Type resolved = resolve(classRawType, type, parameterIndex);
099            if (resolved == null) {
100              return null;
101            } else if (resolved instanceof TypeVariable) {
102              TypeVariable resolvedTV = (TypeVariable)resolved;
103              TypeVariable[] a = classRawType.getTypeParameters();
104              for (int i = 0;i < a.length;i++) {
105                if (a[i].equals(resolvedTV)) {
106                  return resolve(implementation, classRawType, i);
107                }
108              }
109              throw new AssertionError();
110            } else {
111              throw new UnsupportedOperationException("Cannot support resolution of " + resolved);
112            }
113          } else {
114            throw new UnsupportedOperationException();
115          }
116        } else {
117          throw new UnsupportedOperationException("todo " + implementation + " " + implementation.getClass());
118        }
119      }
120    }