001    /*
002     * Copyright (C) 2010 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    /**
027     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
028     * @version $Revision$
029     */
030    public class TypeResolver {
031    
032      /**
033       * A simplistic implementation, it may not handle all cases but it should handle enough.
034       *
035       * @param implementation the type for which the parameter requires a resolution
036       * @param type the type that owns the parameter
037       * @param parameterIndex the parameter index
038       * @return the resolved type
039       */
040      public static Type resolve(Type implementation, Class<?> type, int parameterIndex) {
041        if (implementation == null) {
042          throw new NullPointerException();
043        }
044    
045        //
046        if (implementation == type) {
047          TypeVariable<? extends Class<?>>[] tp = type.getTypeParameters();
048          if (parameterIndex < tp.length) {
049            return tp[parameterIndex];
050          } else {
051            throw new IllegalArgumentException();
052          }
053        } else if (implementation instanceof Class<?>) {
054          Class<?> c = (Class<?>) implementation;
055          Type gsc = c.getGenericSuperclass();
056          Type resolved = null;
057          if (gsc != null) {
058            resolved = resolve(gsc, type, parameterIndex);
059            if (resolved == null) {
060              // Try with interface
061            }
062          }
063          return resolved;
064        } else if (implementation instanceof ParameterizedType) {
065          ParameterizedType pt = (ParameterizedType) implementation;
066          Type[] typeArgs = pt.getActualTypeArguments();
067          Type rawType = pt.getRawType();
068          if (rawType == type) {
069            return typeArgs[parameterIndex];
070          } else if (rawType instanceof Class<?>) {
071            Class<?> classRawType = (Class<?>)rawType;
072            Type resolved = resolve(classRawType, type, parameterIndex);
073            if (resolved == null) {
074              return null;
075            } else if (resolved instanceof TypeVariable) {
076              TypeVariable resolvedTV = (TypeVariable)resolved;
077              TypeVariable[] a = classRawType.getTypeParameters();
078              for (int i = 0;i < a.length;i++) {
079                if (a[i].equals(resolvedTV)) {
080                  return resolve(implementation, classRawType, i);
081                }
082              }
083              throw new AssertionError();
084            } else {
085              throw new UnsupportedOperationException("Cannot support resolution of " + resolved);
086            }
087          } else {
088            throw new UnsupportedOperationException();
089          }
090        } else {
091          throw new UnsupportedOperationException("todo " + implementation + " " + implementation.getClass());
092        }
093      }
094    }