This class encapsulates generic type variables declarations on classes and fields. Different uses of this class:

When used for definition before 'extends' (in ClassDescriptor): name + constraintClassDescriptor (+ constraintGenericTypeVarArgs) | constraintGenericTypeVar:

name: the name of the new generic type var,

constraintClassDescriptor: when the constraint is a concrete class, this holds the class descriptor;

constraintGenericTypeVar: when the constraint is another generic type var, this refers to the definition of that generic type var;

constraintGenericTypeVarArgs: when the constraint is parameterized, this holds type arguments.

When used with field types (in FieldDescriptor):

if the field is purely generic, name + referredGenericTypeVar: name: the generic type var name used as the type, referredGenericTypeVar: refers to the definition of this generic type var in class definition.

if the field is parameterized, the FieldDescriptor should already have the class part of the field type, and its genericTypeVars field should have a list of type arguments. each type argument has classDescriptor / referredGenericTypeVar: classDescriptor: if the argument is a concrete class, referredGenericTypeVar: if the argument is parameterized or another generic type var. it should refer to the definition of that generic type var in class definition.

When used with base type after 'extends' (in ClassDescriptor): similar to the parameterized case when it is used for field type.

@author quyin
Пример #1
0
        /// <sumary>
        /// Creates a GenericTypeVar object as in a type reference (usage), from a java reflection Type
        /// object.
        /// <sumary>
        public static GenericTypeVar GetGenericTypeVarRef(Type type, List <GenericTypeVar> scope)
        {
            GenericTypeVar g = new GenericTypeVar();

            g.scope = scope;

            // case 1: arg is a concrete class
            if (!type.IsGenericParameter && !type.GetTypeInfo().IsGenericType)
            {
                g.classDescriptor = ClassDescriptor.GetClassDescriptor(type);
                return(g);
            }
            else if (type.IsGenericParameter)// case 2: arg is another generic type var
            {
                String argName = type.Name;
                if (argName != null && scope != null)
                {
                    foreach (GenericTypeVar var in scope)
                    {
                        if (argName.Equals(var.Name))
                        {
                            g.name = var.Name;
                            g.referredGenericTypeVar = var;
                            break;
                        }
                    }
                }
            }

            // case 3: arg is parameterized
            CheckTypeParameterizedTypeImpl(g, type);

            g.scope = null;
            return(g);
        }
Пример #2
0
        private void DeriveGenericTypeVariables()
        {
            //FieldInfo field = fieldDescriptor.Field;
            Type genericType = Field.FieldType;
            List <GenericTypeVar> derivedGenericTypeVars = new List <GenericTypeVar>();

            if (genericType.IsGenericParameter)
            {
                GenericTypeVar g = GenericTypeVar.GetGenericTypeVarRef(genericType, GetGenericTypeVarsContext());
                derivedGenericTypeVars.Add(g);
            }
            else
            {
                TypeInfo typeInfo = genericType.GetTypeInfo();
                if (typeInfo.IsGenericType)
                {
                    Type[] types = typeInfo.GenericTypeArguments;

                    if (types == null | types.Length <= 0)
                    {
                        return;
                    }

                    foreach (Type t in types)
                    {
                        GenericTypeVar g = GenericTypeVar.GetGenericTypeVarRef(t, GetGenericTypeVarsContext());
                        derivedGenericTypeVars.Add(g);
                    }
                }
            }

            SetGenericTypeVars(derivedGenericTypeVars);
        }
Пример #3
0
        public void AddGenericTypeVarArg(GenericTypeVar arg)
        {
            if (genericTypeVarArgs == null)
            {
                genericTypeVarArgs = new List <GenericTypeVar>();
            }

            genericTypeVarArgs.Add(arg);
        }
Пример #4
0
        public void AddContraintGenericTypeVarArg(GenericTypeVar g)
        {
            if (constraintGenericTypeVarArgs == null)
            {
                constraintGenericTypeVarArgs = new List <GenericTypeVar>();
            }

            constraintGenericTypeVarArgs.Add(g);
        }
Пример #5
0
        public override string ToString()
        {
            {
                StringBuilder sb = new StringBuilder();

                if (IsDef())
                {
                    sb.Append(name);
                    if (constraintGenericTypeVar != null)
                    {
                        sb.Append(" : ").Append(constraintGenericTypeVar.name);
                    }
                    else if (constraintClassDescriptor != null)
                    {
                        sb.Append(" : ").Append(constraintClassDescriptor.DescribedClassSimpleName);
                        if (constraintGenericTypeVarArgs != null && constraintGenericTypeVarArgs.Count > 0)
                        {
                            sb.Append("<");
                            for (int i = 0; i < constraintGenericTypeVarArgs.Count; ++i)
                            {
                                GenericTypeVar g = constraintGenericTypeVarArgs[i];
                                sb.Append(i == 0 ? "" : ",").Append(g.ToString());
                            }
                            sb.Append(">");
                        }
                    }
                }
                else
                {
                    if (name != null || referredGenericTypeVar != null)
                    {
                        sb.Append(name);
                    }
                    else if (classDescriptor != null)
                    {
                        sb.Append(classDescriptor.DescribedClassSimpleName);
                        if (genericTypeVarArgs != null && genericTypeVarArgs.Count > 0)
                        {
                            sb.Append("<");
                            for (int i = 0; i < genericTypeVarArgs.Count; ++i)
                            {
                                GenericTypeVar g = genericTypeVarArgs[i];
                                sb.Append(i == 0 ? "" : ",").Append(g.ToString());
                            }
                            sb.Append(">");
                        }
                    }
                }

                return(sb.ToString());
            }
        }
Пример #6
0
        /// <summary>
        /// Creates a GenericTypeVar object as the definition of a new generic type var, from a java
        /// reflection TypeVariable object.
        ///
        /// <param name='typeVariable'></param>
        /// <param name='scope'>the scope of current generic type vars; used to resolve generic type var names.</param>
        /// </summary>
        public static GenericTypeVar GetGenericTypeVarDef(Type typeVariable, List <GenericTypeVar> scope)
        {
            GenericTypeVar g = new GenericTypeVar();

            g.scope = scope;
            g.name  = typeVariable.Name;

            // resolve constraints
            ResolveGenericTypeVarDefinitionConstraints(g, typeVariable.GetTypeInfo().GetGenericParameterConstraints());

            g.scope = null;
            return(g);
        }
Пример #7
0
 private void DeriveGenericTypeVariables()
 {
     if (_describedClass != null) // for generated descriptors, describedClass == null
     {
         Type[] typeVariables = _describedClass.GetTypeInfo().GenericTypeArguments;
         if (typeVariables.Length > 0)
         {
             foreach (Type typeVariable in typeVariables)
             {
                 GenericTypeVar g = GenericTypeVar.GetGenericTypeVarDef(typeVariable, this.genericTypeVars);
                 this.genericTypeVars.Add(g);
             }
         }
     }
 }
Пример #8
0
        public static void CheckTypeParameterizedTypeImpl(GenericTypeVar g, Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();

            if (typeInfo.IsGenericType)
            {
                g.ClassDescriptor = ClassDescriptor.GetClassDescriptor(type);

                Type[] types = typeInfo.GenericTypeArguments;

                foreach (Type t in types)
                {
                    g.AddGenericTypeVarArg(GenericTypeVar.GetGenericTypeVarRef(t, g.Scope));
                }
            }
        }
Пример #9
0
        public static void CheckBoundParameterizedTypeImpl(GenericTypeVar g, Type bound)
        {
            TypeInfo typeInfo = bound.GetTypeInfo();

            if (typeInfo.IsGenericType)
            {
                g.ConstraintClassDescriptor = ClassDescriptor.GetClassDescriptor(bound);

                Type[] types = typeInfo.GenericTypeArguments;

                foreach (Type type in types)
                {
                    g.AddContraintGenericTypeVarArg(GenericTypeVar.GetGenericTypeVarRef(type, g.Scope));
                }
            }
        }
Пример #10
0
        public static List <GenericTypeVar> GetGenericTypeVars(Type parameterizedType, List <GenericTypeVar> scope)
        {
            Type[] types = parameterizedType.GenericTypeArguments;

            if (types.Length <= 0)
            {
                return(null);
            }

            List <GenericTypeVar> returnValue = new List <GenericTypeVar>();

            foreach (Type t in types)
            {
                GenericTypeVar g = GenericTypeVar.GetGenericTypeVarRef(t, scope);
                returnValue.Add(g);
            }

            return(returnValue);
        }
Пример #11
0
        /// <sumary>
        /// Resolves constraints on the definition of a generic type var.
        /// <sumary>
        public static void ResolveGenericTypeVarDefinitionConstraints(GenericTypeVar g, Type[] bounds)
        {
            if (bounds == null)
            {
                return;
            }

            Type bound = bounds[0];

            // case 1: constraint is a concrete class
            if (!bound.IsGenericParameter)
            {
                if (typeof(Object) != bound)
                {
                    g.constraintClassDescriptor = ClassDescriptor.GetClassDescriptor(bound);
                }
            }
            else // case 2: constraint is another generic type var
            {
                // look up the scope to find the bound generic type var (must have been defined)
                String boundName = bound.Name;
                if (boundName != null && g.scope != null)
                {
                    foreach (GenericTypeVar var in g.scope)
                    {
                        if (boundName.Equals(var.Name))
                        {
                            g.constraintGenericTypeVar = var;
                            break;
                        }
                    }
                }
            }

            // case 3: constraint is parameterized -- the most complicated case
            CheckBoundParameterizedTypeImpl(g, bound);
        }
Пример #12
0
        public static void CheckTypeParameterizedTypeImpl(GenericTypeVar g, Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType)
            {
                g.ClassDescriptor = ClassDescriptor.GetClassDescriptor(type);

                Type[] types = typeInfo.GenericTypeArguments;

                foreach (Type t in types)
                {
                    g.AddGenericTypeVarArg(GenericTypeVar.GetGenericTypeVarRef(t, g.Scope));
                }
            }
        }
Пример #13
0
        public static void CheckBoundParameterizedTypeImpl(GenericTypeVar g, Type bound)
        {
            TypeInfo typeInfo = bound.GetTypeInfo();
            if (typeInfo.IsGenericType)
            {
                g.ConstraintClassDescriptor = ClassDescriptor.GetClassDescriptor(bound);

                Type[] types = typeInfo.GenericTypeArguments;

                foreach (Type type in types)
                {
                    g.AddContraintGenericTypeVarArg(GenericTypeVar.GetGenericTypeVarRef(type, g.Scope));
                }
            }
        }
Пример #14
0
        public void AddGenericTypeVarArg(GenericTypeVar arg)
        {
            if (genericTypeVarArgs == null)
                genericTypeVarArgs = new List<GenericTypeVar>();

            genericTypeVarArgs.Add(arg);
        }
Пример #15
0
        public void AddContraintGenericTypeVarArg(GenericTypeVar g)
        {
            if (constraintGenericTypeVarArgs == null)
                constraintGenericTypeVarArgs = new List<GenericTypeVar>();

            constraintGenericTypeVarArgs.Add(g);
        }
Пример #16
0
        /// <sumary>
        /// Resolves constraints on a generic type var that is used in a type reference (usage).
        /// <sumary>
        public static void ResolveGenericTypeVarReferenceConstraints(GenericTypeVar g, Type[] bounds)
        {
            if (bounds == null)
                return;

            Type bound = bounds[0];

            // case 1: constraint is a concrete class
            if (!bound.IsGenericParameter)
            {
                if (typeof(Object) != bound)
                    g.ClassDescriptor = ClassDescriptor.GetClassDescriptor(bound);
            }
            else // case 2: constraint is another generic type var
            {
                String boundName = bound.Name;
                if (boundName != null && g.scope != null)
                {
                    foreach (GenericTypeVar var in g.scope)
                        if (boundName.Equals(var.Name))
                        {
                            g.ConstraintGenericTypeVar = var;
                            break;
                        }
                }
            }

            // case 3: constraint is parameterized -- the most complicated case
            CheckBoundParameterizedTypeImpl(g, bound);
        }
Пример #17
0
        /// <sumary>
        /// Creates a GenericTypeVar object as in a type reference (usage), from a java reflection Type
        /// object.
        /// <sumary>
        public static GenericTypeVar GetGenericTypeVarRef(Type type, List<GenericTypeVar> scope)
        {
            GenericTypeVar g = new GenericTypeVar();
            g.scope = scope;

            // case 1: arg is a concrete class
            if (!type.IsGenericParameter && !type.GetTypeInfo().IsGenericType)
            {
                g.classDescriptor = ClassDescriptor.GetClassDescriptor(type);
                return g;
            }
            else if (type.IsGenericParameter)// case 2: arg is another generic type var
            {
                String argName = type.Name;
                if (argName != null && scope != null)
                {
                    foreach (GenericTypeVar var in scope)
                        if (argName.Equals(var.Name))
                        {
                            g.name = var.Name;
                            g.referredGenericTypeVar = var;
                            break;
                        }
                }
            }

            // case 3: arg is parameterized
            CheckTypeParameterizedTypeImpl(g, type);

            g.scope = null;
            return g;
        }
Пример #18
0
        /// <summary>
        /// Creates a GenericTypeVar object as the definition of a new generic type var, from a java
        /// reflection TypeVariable object.
        ///
        /// <param name='typeVariable'></param>
        /// <param name='scope'>the scope of current generic type vars; used to resolve generic type var names.</param>
        /// </summary>
        public static GenericTypeVar GetGenericTypeVarDef(Type typeVariable, List<GenericTypeVar> scope)
        {
            GenericTypeVar g = new GenericTypeVar();
            g.scope = scope;
            g.name = typeVariable.Name;

            // resolve constraints
            ResolveGenericTypeVarDefinitionConstraints(g, typeVariable.GetTypeInfo().GetGenericParameterConstraints());

            g.scope = null;
            return g;
        }