public static void DefaultValue_PropertyGetFromStringCtorArg_MatchesCtorArgument()
        {
            const string defaultValue = "test";
            var          defaultAttr  = new DefaultAttribute(defaultValue);

            Assert.That(defaultAttr.DefaultValue, Is.EqualTo(defaultValue));
        }
        public static void DefaultValue_PropertyGetFromIntCtorArg_MatchesCtorArgument()
        {
            const int defaultValue = 100;
            var       defaultAttr  = new DefaultAttribute(defaultValue);

            Assert.That(defaultAttr.DefaultValue, Is.EqualTo(defaultValue.ToString(CultureInfo.InvariantCulture)));
        }
Beispiel #3
0
        public IAttribute <T> GetAttribute <T>(AttributeKey <T> key)
            where T : class
        {
            if (key is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
            }

            DefaultAttribute[] attrs = Volatile.Read(ref this.attributes);
            if (attrs is null)
            {
                attrs = new DefaultAttribute[BucketSize];
                // Not using ConcurrentHashMap due to high memory consumption.
                attrs = Interlocked.CompareExchange(ref this.attributes, attrs, null) ?? attrs;
            }

            int i = Index(key);
            DefaultAttribute head = Volatile.Read(ref attrs[i]);

            if (head is null)
            {
                // No head exists yet which means we may be able to add the attribute without synchronization and just
                // use compare and set. At worst we need to fallback to synchronization
                head = new DefaultAttribute <T>(key);

                if (Interlocked.CompareExchange(ref this.attributes[i], head, null) is null)
                {
                    // we were able to add it so return the head right away
                    return((IAttribute <T>)head);
                }

                head = Volatile.Read(ref attrs[i]);
            }

            lock (head)
            {
                DefaultAttribute curr = head;
                while (true)
                {
                    if (!curr.Removed && curr.GetKey() == key)
                    {
                        return((IAttribute <T>)curr);
                    }

                    DefaultAttribute next = curr.Next;
                    if (next is null)
                    {
                        var attr = new DefaultAttribute <T>(head, key);
                        curr.Next = attr;
                        attr.Prev = curr;
                        return(attr);
                    }
                    else
                    {
                        curr = next;
                    }
                }
            }
        }
Beispiel #4
0
        public void ShowHelpMessage()
        {
            AssemblyName assembly = Assembly.GetEntryAssembly().GetName();

            // VERSION
            string message = ""
                             + "\n"
                             + (assembly.Name + "".PadLeft(2) + assembly.Version.ToString())
            ;

            // USAGE
            message += ""
                       + "\n"
                       + "\n" + "".PadLeft(0) + "USAGE : "
                       + "\n" + "".PadLeft(10) + string.Format("Mineral.dll {0} <dir> {1} <password> [options]", OptionName.KeyStoreDir, OptionName.KeyStorePassword)
                       + "\n" + "".PadLeft(10) + string.Format("Mineral.dll {0} <key> [options]", OptionName.PrivateKey)
            ;

            // DEFAULT OPTIONS
            message += ""
                       + "\n"
                       + "\n" + "".PadLeft(1) + "--DEFAULT OPTIONS : ";
            foreach (PropertyInfo info in typeof(OptionDefault).GetProperties())
            {
                DefaultAttribute attr = (DefaultAttribute)info.GetCustomAttribute(typeof(DefaultAttribute));
                if (attr != null)
                {
                    message += "\n" + "".PadLeft(4);
                    message += string.Format("{0,-25} {1}", attr.Name, attr.Description);
                }
            }

            // WALLET OPTIONS
            message += ""
                       + "\n"
                       + "\n" + "".PadLeft(1) + "--WALLET OPTIONS : ";
            foreach (PropertyInfo info in typeof(OptionWallet).GetProperties())
            {
                WalletAttribute attr = (WalletAttribute)info.GetCustomAttribute(typeof(WalletAttribute));
                if (attr != null)
                {
                    message += "\n" + "".PadLeft(4);
                    message += string.Format("{0,-25} {1}", attr.Name, attr.Description);
                }
            }

            message += ""
                       + "\n"
                       + "\n" + "".PadLeft(0) + "MISC OPTION :"
                       + "\n" + "".PadLeft(4) + "-h   -help"
            ;

            Console.WriteLine(message);
        }
        public void ApplyTransform_NullStringValue_DefaultValueSet()
        {
            var record           = new MockRecord();
            var defaultValue     = "Default";
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.AreEqual(defaultValue, record.StringField);
        }
Beispiel #6
0
		internal static void AddAttributes(IProjectContent pc, IList<IAttribute> list, IList<CustomAttributeData> attributes)
		{
			foreach (CustomAttributeData att in attributes) {
				DefaultAttribute a = new DefaultAttribute(ReflectionReturnType.Create(pc, null, att.Constructor.DeclaringType, false));
				foreach (CustomAttributeTypedArgument arg in att.ConstructorArguments) {
					a.PositionalArguments.Add(ReplaceTypeByIReturnType(pc, arg.Value));
				}
				foreach (CustomAttributeNamedArgument arg in att.NamedArguments) {
					a.NamedArguments.Add(arg.MemberInfo.Name, ReplaceTypeByIReturnType(pc, arg.TypedValue.Value));
				}
				list.Add(a);
			}
		}
		internal static void AddAttributes(IProjectContent pc, IList<IAttribute> list, IList<CustomAttributeData> attributes)
		{
			foreach (CustomAttributeData att in attributes) {
				DefaultAttribute a = new DefaultAttribute(ReflectionReturnType.Create(pc, att.Constructor.DeclaringType));
				foreach (CustomAttributeTypedArgument arg in att.ConstructorArguments) {
					a.PositionalArguments.Add(ReplaceTypeByIReturnType(pc, arg.Value));
				}
				foreach (CustomAttributeNamedArgument arg in att.NamedArguments) {
					a.NamedArguments.Add(arg.MemberInfo.Name, ReplaceTypeByIReturnType(pc, arg.TypedValue.Value));
				}
				list.Add(a);
			}
		}
Beispiel #8
0
        /// <summary>
        /// Construct a builtin attribute.
        /// </summary>
        internal IAttribute MakeAttribute(KnownAttribute type)
        {
            var attr = LazyInit.VolatileRead(ref knownAttributes[(int)type]);

            if (attr != null)
            {
                return(attr);
            }
            attr = new DefaultAttribute(GetAttributeType(type),
                                        ImmutableArray.Create <CustomAttributeTypedArgument <IType> >(),
                                        ImmutableArray.Create <CustomAttributeNamedArgument <IType> >());
            return(LazyInit.GetOrSet(ref knownAttributes[(int)type], attr));
        }
        public void ApplyTransform_NullZeroIntValue_DefaultValueSet()
        {
            var record = new MockRecord()
            {
                NullableIntField = null
            };
            var defaultValue     = 10;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableIntField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.AreEqual(10, record.NullableIntField);
        }
        public void ApplyTransform_NonZeroDecimalValue_OriginalValueReimains()
        {
            var record = new MockRecord()
            {
                DecimalField = 10m
            };
            var defaultValue     = 20m;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.DecimalField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.AreEqual(10m, record.DecimalField);
        }
        public void ApplyTransform_NullStringValueWithNullDefaultValue_NullValueReturned()
        {
            var record = new MockRecord()
            {
                StringField = null
            };
            var defaultValue     = (string)null;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.IsNull(record.StringField);
        }
        public void ApplyTransform_NonNullStringValueWithNullDefaultValue_OriginalValueRemains()
        {
            var record = new MockRecord()
            {
                StringField = "test"
            };
            var defaultValue     = (string)null;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.AreEqual("test", record.StringField);
        }
        public void ApplyTransform_ZeroIntValue_OriginalValueReimains()
        {
            var record = new MockRecord()
            {
                IntField = 0
            };
            var defaultValue     = 10;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.IntField));
            var defaultAttribute = new DefaultAttribute(defaultValue);

            defaultAttribute.ApplyTransform(property, record);

            Assert.AreEqual(0, record.IntField);
        }
		static void AddAttributes(IProjectContent pc, IList<IAttribute> list, IList<CustomAttributeData> attributes)
		{
			foreach (CustomAttributeData att in attributes) {
				DefaultAttribute a = new DefaultAttribute(att.Constructor.DeclaringType.FullName);
				foreach (CustomAttributeTypedArgument arg in att.ConstructorArguments) {
					IReturnType type = ReflectionReturnType.Create(pc, null, arg.ArgumentType, false);
					a.PositionalArguments.Add(new AttributeArgument(type, arg.Value));
				}
				foreach (CustomAttributeNamedArgument arg in att.NamedArguments) {
					IReturnType type = ReflectionReturnType.Create(pc, null, arg.TypedValue.ArgumentType, false);
					a.NamedArguments.Add(arg.MemberInfo.Name, new AttributeArgument(type, arg.TypedValue.Value));
				}
				list.Add(a);
			}
		}
Beispiel #15
0
        public bool HasAttribute <T>(AttributeKey <T> key)
            where T : class
        {
            if (key is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
            }

            DefaultAttribute[] attrs = Volatile.Read(ref this.attributes);
            if (attrs is null)
            {
                // no attribute exists
                return(false);
            }

            int i = Index(key);
            DefaultAttribute head = Volatile.Read(ref attrs[i]);

            if (head is null)
            {
                // No attribute exists which point to the bucket in which the head should be located
                return(false);
            }

            // check on the head can be done without synchronization
            if (head.GetKey() == key && !head.Removed)
            {
                return(true);
            }

            lock (head)
            {
                // we need to synchronize on the head
                DefaultAttribute curr = head.Next;
                while (curr is object)
                {
                    if (!curr.Removed && curr.GetKey() == key)
                    {
                        return(true);
                    }
                    curr = curr.Next;
                }
                return(false);
            }
        }
Beispiel #16
0
        public bool HasAttribute <T>(AttributeKey <T> key)
            where T : class
        {
            Contract.Requires(key != null);

            DefaultAttribute[] attrs = this.attributes;
            if (attrs == null)
            {
                // no attribute exists
                return(false);
            }

            int i = Index(key);
            DefaultAttribute head = Volatile.Read(ref attrs[i]);

            if (head == null)
            {
                // No attribute exists which point to the bucket in which the head should be located
                return(false);
            }

            // check on the head can be done without synchronization
            if (head.GetKey() == key && !head.Removed)
            {
                return(true);
            }

            lock (head)
            {
                // we need to synchronize on the head
                DefaultAttribute curr = head.Next;
                while (curr != null)
                {
                    if (!curr.Removed && curr.GetKey() == key)
                    {
                        return(true);
                    }
                    curr = curr.Next;
                }
                return(false);
            }
        }
Beispiel #17
0
 static string TranslateDefaultAttribute(DefaultAttribute a)
 {
     bool isBooleanDefault = a.Value != null && a.Value.GetType() == typeof(bool);
     bool isStringDefault = a.Value != null && a.Value.GetType() == typeof(string);
     bool isEnumDefault = a.Value != null && a.Value.GetType().IsEnum;
     string defaultValue = a.Value.ToString();
     if (isBooleanDefault) defaultValue = defaultValue.ToLower();
     else if (isEnumDefault) defaultValue = $"{a.Value.GetType().Name}.{a.Value}";
     if (isStringDefault)
     {
         return
              new AttributeBuilder(typeof(DefaultAttribute).FullName)
              .AddStringParam(defaultValue)
              .ToString();
     }
     else
     {
         return
             new AttributeBuilder(typeof(DefaultAttribute).FullName)
             .AddParam(defaultValue)
             .ToString();
     }
 }
Beispiel #18
0
 protected DefaultAttribute()
 {
     this.Head = this;
 }
Beispiel #19
0
 public static bool HasDefaultAttribute(PropertyInfo propertyInfo, out DefaultAttribute defaultAttribute)
 {
     defaultAttribute = propertyInfo.GetCustomAttribute <DefaultAttribute>(true);
     return(defaultAttribute != null);
 }
        List <IAttribute> VisitAttributes(IList <AST.AttributeSection> attributes, ClassFinder context)
        {
            // TODO Expressions???
            List <IAttribute> result = new List <IAttribute>();

            foreach (AST.AttributeSection section in attributes)
            {
                AttributeTarget target = AttributeTarget.None;
                if (section.AttributeTarget != null && section.AttributeTarget != "")
                {
                    switch (section.AttributeTarget.ToUpperInvariant())
                    {
                    case "ASSEMBLY":
                        target = AttributeTarget.Assembly;
                        break;

                    case "FIELD":
                        target = AttributeTarget.Field;
                        break;

                    case "EVENT":
                        target = AttributeTarget.Event;
                        break;

                    case "METHOD":
                        target = AttributeTarget.Method;
                        break;

                    case "MODULE":
                        target = AttributeTarget.Module;
                        break;

                    case "PARAM":
                        target = AttributeTarget.Param;
                        break;

                    case "PROPERTY":
                        target = AttributeTarget.Property;
                        break;

                    case "RETURN":
                        target = AttributeTarget.Return;
                        break;

                    case "TYPE":
                        target = AttributeTarget.Type;
                        break;

                    default:
                        target = AttributeTarget.None;
                        break;
                    }
                }

                foreach (AST.Attribute attribute in section.Attributes)
                {
                    List <object> positionalArguments = new List <object>();
                    foreach (AST.Expression positionalArgument in attribute.PositionalArguments)
                    {
                        positionalArguments.Add(ConvertAttributeArgument(positionalArgument));
                    }
                    Dictionary <string, object> namedArguments = new Dictionary <string, object>();
                    foreach (AST.NamedArgumentExpression namedArgumentExpression in attribute.NamedArguments)
                    {
                        namedArguments.Add(namedArgumentExpression.Name, ConvertAttributeArgument(namedArgumentExpression.Expression));
                    }
                    var defaultAttribute = new DefaultAttribute(new AttributeReturnType(context, attribute.Name),
                                                                target,
                                                                positionalArguments,
                                                                namedArguments);
                    defaultAttribute.Region          = GetRegion(attribute.StartLocation, attribute.EndLocation);
                    defaultAttribute.CompilationUnit = cu;

                    result.Add(defaultAttribute);
                }
            }
            return(result);
        }
        /// <summary>
        /// Applies attribute configurations to the map.
        /// </summary>
        /// <param name="memberMap">The member map.</param>
        protected virtual void ApplyAttributes(MemberMap memberMap)
        {
            var member = memberMap.Data.Member;

            //if( member.GetCustomAttribute( typeof( IndexAttribute ) ) is IndexAttribute indexAttribute )
            IndexAttribute indexAttribute = member.GetCustomAttributes(typeof(IndexAttribute), false).FirstOrDefault() as IndexAttribute;

            if (indexAttribute != null)
            {
                memberMap.Data.Index      = indexAttribute.Index;
                memberMap.Data.IndexEnd   = indexAttribute.IndexEnd;
                memberMap.Data.IsIndexSet = true;
            }

            //if( member.GetCustomAttribute( typeof( NameAttribute ) ) is NameAttribute nameAttribute )
            NameAttribute nameAttribute = member.GetCustomAttributes(typeof(NameAttribute), false).FirstOrDefault() as NameAttribute;

            if (nameAttribute != null)
            {
                memberMap.Data.Names.Clear();
                memberMap.Data.Names.AddRange(nameAttribute.Names);
                memberMap.Data.IsNameSet = true;
            }

            //if( member.GetCustomAttribute( typeof( NameIndexAttribute ) ) is NameIndexAttribute nameIndexAttribute )
            NameIndexAttribute nameIndexAttribute = member.GetCustomAttributes(typeof(NameIndexAttribute), false).FirstOrDefault() as NameIndexAttribute;

            if (nameIndexAttribute != null)
            {
                memberMap.Data.NameIndex = nameIndexAttribute.NameIndex;
            }


            //if( member.GetCustomAttribute( typeof( IgnoreAttribute ) ) is IgnoreAttribute ignoreAttribute )
            IgnoreAttribute ignoreAttribute = member.GetCustomAttributes(typeof(IgnoreAttribute), false).FirstOrDefault() as IgnoreAttribute;

            if (ignoreAttribute != null)
            {
                memberMap.Data.Ignore = true;
            }

            //if( member.GetCustomAttribute( typeof( DefaultAttribute ) ) is DefaultAttribute defaultAttribute )
            DefaultAttribute defaultAttribute = member.GetCustomAttributes(typeof(DefaultAttribute), false).FirstOrDefault() as DefaultAttribute;

            if (defaultAttribute != null)
            {
                memberMap.Data.Default      = defaultAttribute.Default;
                memberMap.Data.IsDefaultSet = true;
            }

            //if( member.GetCustomAttribute( typeof( ConstantAttribute ) ) is ConstantAttribute constantAttribute )
            ConstantAttribute constantAttribute = member.GetCustomAttributes(typeof(ConstantAttribute), false).FirstOrDefault() as ConstantAttribute;

            if (constantAttribute != null)
            {
                memberMap.Data.Constant      = constantAttribute.Constant;
                memberMap.Data.IsConstantSet = true;
            }

            //if( member.GetCustomAttribute( typeof( TypeConverterAttribute ) ) is TypeConverterAttribute typeConverterAttribute )
            TypeConverterAttribute typeConverterAttribute = member.GetCustomAttributes(typeof(TypeConverterAttribute), false).FirstOrDefault() as TypeConverterAttribute;

            if (typeConverterAttribute != null)
            {
                memberMap.Data.TypeConverter = typeConverterAttribute.TypeConverter;
            }

            //if( member.GetCustomAttribute( typeof( CultureInfoAttribute ) ) is CultureInfoAttribute cultureInfoAttribute )
            CultureInfoAttribute cultureInfoAttribute = member.GetCustomAttributes(typeof(CultureInfoAttribute), false).FirstOrDefault() as CultureInfoAttribute;

            if (cultureInfoAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.CultureInfo = cultureInfoAttribute.CultureInfo;
            }

            //if( member.GetCustomAttribute( typeof( DateTimeStylesAttribute ) ) is DateTimeStylesAttribute dateTimeStylesAttribute )
            DateTimeStylesAttribute dateTimeStylesAttribute = member.GetCustomAttributes(typeof(DateTimeStylesAttribute), false).FirstOrDefault() as DateTimeStylesAttribute;

            if (dateTimeStylesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.DateTimeStyle = dateTimeStylesAttribute.DateTimeStyles;
            }

            //if( member.GetCustomAttribute( typeof( NumberStylesAttribute ) ) is NumberStylesAttribute numberStylesAttribute )
            NumberStylesAttribute numberStylesAttribute = member.GetCustomAttributes(typeof(NumberStylesAttribute), false).FirstOrDefault() as NumberStylesAttribute;

            if (numberStylesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.NumberStyle = numberStylesAttribute.NumberStyles;
            }

            FormatAttribute formatAttribute = member.GetCustomAttributes(typeof(FormatAttribute), false).FirstOrDefault() as FormatAttribute;

            if (formatAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.Formats = formatAttribute.Formats;
            }

            BooleanTrueValuesAttribute booleanTrueValuesAttribute = member.GetCustomAttributes(typeof(BooleanTrueValuesAttribute), false).FirstOrDefault() as BooleanTrueValuesAttribute;

            if (booleanTrueValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.BooleanTrueValues.Clear();
                memberMap.Data.TypeConverterOptions.BooleanTrueValues.AddRange(booleanTrueValuesAttribute.TrueValues);
            }

            BooleanFalseValuesAttribute booleanFalseValuesAttribute = member.GetCustomAttributes(typeof(BooleanFalseValuesAttribute), false).FirstOrDefault() as BooleanFalseValuesAttribute;

            if (booleanFalseValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.BooleanFalseValues.Clear();
                memberMap.Data.TypeConverterOptions.BooleanFalseValues.AddRange(booleanFalseValuesAttribute.FalseValues);
            }

            NullValuesAttribute nullValuesAttribute = member.GetCustomAttributes(typeof(NullValuesAttribute), false).FirstOrDefault()  as NullValuesAttribute;

            if (nullValuesAttribute != null)
            {
                memberMap.Data.TypeConverterOptions.NullValues.Clear();
                memberMap.Data.TypeConverterOptions.NullValues.AddRange(nullValuesAttribute.NullValues);
            }
        }
		List<IAttribute> VisitAttributes(IList<AST.AttributeSection> attributes, ClassFinder context)
		{
			// TODO Expressions???
			List<IAttribute> result = new List<IAttribute>();
			foreach (AST.AttributeSection section in attributes) {
				
				AttributeTarget target = AttributeTarget.None;
				if (section.AttributeTarget != null && section.AttributeTarget != "") {
					switch (section.AttributeTarget.ToUpperInvariant()) {
						case "ASSEMBLY":
							target = AttributeTarget.Assembly;
							break;
						case "FIELD":
							target = AttributeTarget.Field;
							break;
						case "EVENT":
							target = AttributeTarget.Event;
							break;
						case "METHOD":
							target = AttributeTarget.Method;
							break;
						case "MODULE":
							target = AttributeTarget.Module;
							break;
						case "PARAM":
							target = AttributeTarget.Param;
							break;
						case "PROPERTY":
							target = AttributeTarget.Property;
							break;
						case "RETURN":
							target = AttributeTarget.Return;
							break;
						case "TYPE":
							target = AttributeTarget.Type;
							break;
						default:
							target = AttributeTarget.None;
							break;
							
					}
				}
				
				foreach (AST.Attribute attribute in section.Attributes) {
					List<object> positionalArguments = new List<object>();
					foreach (AST.Expression positionalArgument in attribute.PositionalArguments) {
						positionalArguments.Add(ConvertAttributeArgument(positionalArgument));
					}
					Dictionary<string, object> namedArguments = new Dictionary<string, object>();
					foreach (AST.NamedArgumentExpression namedArgumentExpression in attribute.NamedArguments) {
						namedArguments.Add(namedArgumentExpression.Name, ConvertAttributeArgument(namedArgumentExpression.Expression));
					}
					var defaultAttribute = new DefaultAttribute(new AttributeReturnType(context, attribute.Name),
															 	target, 
															 	positionalArguments, 
															 	namedArguments);
					defaultAttribute.Region = GetRegion(attribute.StartLocation, attribute.EndLocation);
					defaultAttribute.CompilationUnit = cu;
					
					result.Add(defaultAttribute);
				}
			}
			return result;
		}
Beispiel #23
0
 protected DefaultAttribute(DefaultAttribute head)
 {
     this.Head = head;
 }