Commits

Tim Vernum committed 7031de0

Fixed MergedType

  • Participants
  • Parent commits a13c1c3

Comments (0)

Files changed (2)

File source/java/core/org/adjective/orthodox/types/MergedType.java

  */
 package org.adjective.orthodox.types;
 
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
 import java.util.Arrays;
 public class MergedType extends AbstractTypeInfo implements TypeInfo
 {
     private final TypeInfo _delegate;
+    private final TypeInfo _alternate;
 
     protected MergedType(TypeInfo shortChain, TypeInfo longChain)
     {
         super(null);
         _delegate = longChain;
+        if (shortChain.asClass() == longChain.asClass())
+        {
+            _alternate = shortChain;
+        }
+        else
+        {
+            _alternate = null;
+        }
         setHierarchy(new MergedHierarchy(this, shortChain, longChain));
     }
 
 
     public TypeInfo getGenericParameter(TypeVariable< ? > var)
     {
-        // Need to handle when this is the merge of Iterable<E> and Iterable<CharSequence>
-        // Iterable<E> knows about "E" and Iterable<CharSequence> knows that actual type. Need to merge them!
-        return _delegate.getGenericParameter(var);
+        TypeInfo parameter = getGenericParameter(var, _delegate);
+        if (parameter != null && parameter.isKnownType())
+        {
+            return parameter;
+        }
+        if (_alternate != null)
+        {
+            return getGenericParameter(var, _alternate);
+        }
+        return parameter;
+    }
+
+    private TypeInfo getGenericParameter(TypeVariable< ? > var, TypeInfo delegate)
+    {
+        TypeInfo delegateParameter = delegate.getGenericParameter(var);
+        if (delegateParameter != null && delegateParameter.isKnownType())
+        {
+            return delegateParameter;
+        }
+
+        if (delegate.getType() instanceof ParameterizedType)
+        {
+            ParameterizedType pType = (ParameterizedType) delegate.getType();
+            Type[] typeArguments = pType.getActualTypeArguments();
+            for (int i = 0; i < typeArguments.length; i++)
+            {
+                if (typeArguments[i].equals(var))
+                {
+                    // Will this loop indefinately?
+                    return getGenericParameters()[i];
+                }
+            }
+        }
+        return delegateParameter;
     }
 
     public Class< ? > asClass()

File source/java/core/org/adjective/orthodox/types/UnknownHierarchy.java

     {
         for (TypeInfo type : _parents)
         {
-            super.fillHierarchy(type, type, 0);
+            super.fillHierarchy(type, type, 1);
         }
     }