/// <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); }
private void DerivePolymorphicDescriptors(FieldInfo pField) { SimplScope scopeAttribute = XmlTools.GetAnnotation <SimplScope>(pField); String scopeAttributeValue = scopeAttribute == null ? null : scopeAttribute.TranslationScope; if (!String.IsNullOrEmpty(scopeAttributeValue)) { if (!ResolveScopeAttribute(scopeAttributeValue)) { unresolvedScopeAnnotation = scopeAttributeValue; declaringClassDescriptor.RegisterUnresolvedScopeAnnotationFD(this); } } SimplClasses classesAttribute = XmlTools.GetAnnotation <SimplClasses>(pField); Type[] classesAttributeValue = classesAttribute == null ? null : classesAttribute.Classes; if ((classesAttribute != null) && classesAttributeValue.Length > 0) { unresolvedClassesAnnotation = classesAttributeValue; declaringClassDescriptor.RegisterUnresolvedScopeAnnotationFD(this); InitPolymorphicClassDescriptorsList(classesAttributeValue.Length); foreach (Type thatType in classesAttributeValue) { ClassDescriptor classDescriptor = ClassDescriptor.GetClassDescriptor(thatType); RegisterPolymorphicDescriptor(classDescriptor); polymorphClasses.Put(classDescriptor.TagName, classDescriptor.DescribedClass); } } }
/** * Generate tag -> class mappings for a @serial_scope declaration. * * @param scopeAnnotation * Name of the scope to lookup in the global space. Must be non-null. * * @return true if the scope annotation is successfully resolved to a TranslationScope. */ private Boolean ResolveClassesAnnotation(Type[] classesAnnotation) { InitPolymorphicClassDescriptorsList(classesAnnotation.Length); foreach (Type thatClass in classesAnnotation) { ClassDescriptor classDescriptor = ClassDescriptor.GetClassDescriptor(thatClass); RegisterPolymorphicDescriptor(classDescriptor); polymorphClasses.Add(classDescriptor.TagName, classDescriptor.DescribedClass); } return(true); }
public Object GetMapKeyFieldValue(Object mapElement) { if (this.mapKeyFieldName != null) { ClassDescriptor cd = ClassDescriptor.GetClassDescriptor(mapElement); if (cd != null) { FieldDescriptor fd = cd.GetFieldDescriptorByFieldName(mapKeyFieldName); return(fd.field.GetValue(mapElement)); } } return(null); }
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)); } } }
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)); } } }
/// <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); }
private int DeriveNestedSerialization(FieldInfo thatField, int annotationType) { int result = annotationType; Type thatFieldType = thatField.FieldType; switch (annotationType) { case FieldTypes.CompositeElement: String compositeTag = XmlTools.GetAnnotation <SimplComposite>(thatField).TagName; Boolean isWrap = XmlTools.IsAnnotationPresent <SimplWrap>(thatField); Boolean compositeTagIsNullOrEmpty = String.IsNullOrEmpty(compositeTag); if (!IsPolymorphic) { if (isWrap && compositeTagIsNullOrEmpty) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplComposite] " + thatField.Name + " because its tag argument is missing."; Debug.WriteLine(msg); return(FieldTypes.IgnoredAttribute); } _elementClassDescriptor = ClassDescriptor.GetClassDescriptor(thatFieldType); _elementClass = _elementClassDescriptor.DescribedClass; compositeTag = XmlTools.GetXmlTagName(thatField); } else { if (!compositeTagIsNullOrEmpty) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplComposite] " + thatField.Name + " because its tag argument is missing."; Debug.WriteLine(msg); } } compositeTagName = compositeTag; break; case FieldTypes.CollectionElement: if (!(typeof(IList).GetTypeInfo().IsAssignableFrom(thatField.FieldType.GetTypeInfo()))) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate " + "[SimplCollection] " + field.Name + " because the annotated field is not an instance of " + typeof(IList).Name + "."; Debug.WriteLine(msg); return(FieldTypes.IgnoredAttribute); } String collectionTag = XmlTools.GetAnnotation <SimplCollection>(thatField).TagName; if (!IsPolymorphic) { Type collectionElementType = GetTypeArgs(thatField, 0); if (String.IsNullOrEmpty(collectionTag)) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplCollection]" + field.Name + " because its tag argument is missing."; Debug.WriteLine(msg); return(FieldTypes.IgnoredElement); } if (collectionElementType == null) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplCollection] " + field.Name + " because the parameterized type argument for the Collection is missing."; Debug.WriteLine(msg); return(FieldTypes.IgnoredElement); } if (!TypeRegistry.ScalarTypes.Contains(collectionElementType)) { _elementClassDescriptor = ClassDescriptor.GetClassDescriptor(collectionElementType); _elementClass = _elementClassDescriptor.DescribedClass; } else { result = FieldTypes.CollectionScalar; DeriveScalarSerialization(collectionElementType, field); if (ScalarType == null) { result = FieldTypes.IgnoredElement; String msg = "Can't identify ScalarType for serialization of " + collectionElementType; Debug.WriteLine(msg); } } } else { if (!String.IsNullOrEmpty(collectionTag)) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tIgnoring argument to [SimplCollection] " + field.Name + " because it is declared polymorphic with [SimplClasses]."; } } _collectionOrMapTagName = collectionTag; collectionType = TypeRegistry.GetCollectionType(thatField); break; case FieldTypes.MapElement: if (!(typeof(IDictionary).GetTypeInfo().IsAssignableFrom(thatField.FieldType.GetTypeInfo()))) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate " + "[SimplMap] " + field.Name + " because the annotated field is not an instance of " + typeof(IDictionary).Name + "."; Debug.WriteLine(msg); return(FieldTypes.IgnoredAttribute); } String mapTag = XmlTools.GetAnnotation <SimplMap>(thatField).TagName; if (!IsPolymorphic) { Type mapElementType = GetTypeArgs(thatField, 1); if (String.IsNullOrEmpty(mapTag)) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplMap]" + field.Name + " because its tag argument is missing."; Debug.WriteLine(msg); return(FieldTypes.IgnoredElement); } if (mapElementType == null) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tCan't translate [SimplMap] " + field.Name + " because the parameterized type argument for the map is missing."; Debug.WriteLine(msg); return(FieldTypes.IgnoredElement); } if (!TypeRegistry.ScalarTypes.Contains(mapElementType)) { _elementClassDescriptor = ClassDescriptor.GetClassDescriptor(mapElementType); _elementClass = _elementClassDescriptor.DescribedClass; } } else { if (!String.IsNullOrEmpty(mapTag)) { String msg = "In " + declaringClassDescriptor.DescribedClass + "\n\tIgnoring argument to [SimplMap] " + field.Name + " because it is declared polymorphic with [SimplClasses]."; } } _collectionOrMapTagName = mapTag; collectionType = TypeRegistry.GetCollectionType(thatField); break; } switch (annotationType) { case FieldTypes.CollectionElement: case FieldTypes.MapElement: if (!XmlTools.IsAnnotationPresent <SimplNoWrap>(thatField)) { _wrapped = true; } collectionType = TypeRegistry.GetCollectionType(thatField); break; case FieldTypes.CompositeElement: if (XmlTools.IsAnnotationPresent <SimplWrap>(thatField)) { _wrapped = true; } break; } return(result); }
/// <summary> /// /// </summary> /// <param name="thatClass"></param> public void AddTranslation(Type thatClass) { ClassDescriptor entry = ClassDescriptor.GetClassDescriptor(thatClass); AddTranslation(entry); }