private static void GetMethodsFromType(List<MethodDefinition> ret, TypeDefinition type)
 {
     if (type.ContainsIgnoreAttribute()) return;
     foreach(var method in type.GetMethods())
     {
         if (method.ContainsIgnoreAttribute()) continue;
         ret.Add(method);
     }
     
     foreach(var nested in type.NestedTypes)
     {
         GetMethodsFromType(ret, nested);
     }
 }
Esempio n. 2
0
		void PreserveExportedMethods (TypeDefinition type)
		{
			if (!type.HasMethods)
				return;

			foreach (var method in type.GetMethods ()) {
				if (!IsExportedMethod (method))
					continue;

				if (!IsOverridenInUserCode (method))
					continue;

				PreserveMethod (type, method);
			}
		}
Esempio n. 3
0
        public static MethodReference InjectEqualsInternal(TypeDefinition type, TypeReference typeRef, MethodDefinition collectionEquals)
        {
            var methodAttributes = MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static;
            var method = new MethodDefinition("EqualsInternal", methodAttributes, ReferenceFinder.Boolean.TypeReference);
            method.CustomAttributes.MarkAsGeneratedCode();

            var left = method.Parameters.Add("left", typeRef);
            var right = method.Parameters.Add("right", typeRef);

            var body = method.Body;
            body.InitLocals = true;
            var ins = body.Instructions;

            var properties = type.GetPropertiesWithoutIgnores(ignoreAttributeName);

            foreach (var property in properties)
            {
                AddPropertyCode(type, collectionEquals, property, ins, left, right);
            }

            var methods = type.GetMethods();
            var customLogic = methods
                .Where(x => x.CustomAttributes.Any(y => y.AttributeType.Name == customAttribute)).ToArray();

            if (customLogic.Length > 2)
            {
                throw new WeavingException("Only one custom method can be specified.");
            }

            if (customLogic.Length == 1)
            {
                AddCustomLogicCall(type, body, ins, customLogic);
            }

            AddReturnTrue(ins);

            body.OptimizeMacros();

            type.Methods.AddOrReplace(method);

            var methodToCall = new MethodReference(method.Name, method.ReturnType, typeRef);
            foreach (var parameter in method.Parameters)
            {
                methodToCall.Parameters.Add(parameter);
            }

            return methodToCall;
        }
Esempio n. 4
0
        private void OnBindMethodsDefinitions(MonoAssemblyResolver assemblyTarget, TypeDefinition typeDefinition,
            TreeNode rootNode)
        {
            var methodDefinitions = typeDefinition.GetMethods(false);

            for (int i = 0; i < methodDefinitions.Count; i++)
            {
                var node = new TreeNode
                               {
                                   Text =
                                       string.Format("{0} ({1})", methodDefinitions[i].Name,
                                                     methodDefinitions[i].Parameters.Count),
                                   Tag = new BindItem { Method = methodDefinitions[i], Assembly = assemblyTarget }
                               };

                rootNode.Nodes.Add(node);
            }
        }
Esempio n. 5
0
		void ProcessMethods (TypeDefinition type, List<ExportedMethod> exported)
		{
			foreach (MethodDefinition method in type.GetMethods ()) {
				CustomAttribute attribute;
				if (TryGetExportAttribute (method, out attribute)) {
					exported.Add (new ExportedMethod (attribute, method));
					continue;
				}

				if (!method.IsVirtual)
					continue;

				var bases = Annotations.GetBaseMethods (method);
				if (bases == null)
					continue;

				foreach (MethodDefinition @base in bases) {
					if (@base.DeclaringType.IsInterface)
						continue;

					if (TryGetExportAttribute (@base, out attribute)) {
						exported.Add (new ExportedMethod (attribute, method));
						break;
					}
				}
			}
		}
Esempio n. 6
0
 protected string GenerateMethod(TypeDefinition type, string methodName, bool followDependencies)
 {
     var method = type.GetMethods(methodName).First();
     TextWriter writer = new StringWriter();
     var generator = new JsCodeGenerator(this.typeSystem, writer, false);
     var context = new TranslationContext(this.typeSystem, generator);
     var asmDependencies = new List<AssemblyDefinition>();
     context.GenerateMethod(method, followDependencies, asmDependencies);
     return writer.ToString();
 }
		static bool IsAllStatic (TypeDefinition type)
		{
			if (type == null)
				return false;

			if (type.HasMethods) {
				foreach (MethodDefinition ctor in type.GetConstructors ()) {
					// let's the default ctor pass (since it's always here for 1.x code)
					if (!ctor.IsStatic && ctor.HasParameters)
						return false;
				}

				foreach (MethodDefinition method in type.GetMethods ()) {
					if (!method.IsStatic)
						return false;
				}
			}

			if (type.HasFields) {
				foreach (FieldDefinition field in type.Fields) {
					if (!field.IsStatic)
						return false;
				}
			}

			if (type.BaseType.FullName == "System.Object")
				return true;

			return IsAllStatic (type.BaseType.Resolve ());
		}
		public RuleResult CheckType (TypeDefinition type)
		{
			if (type.IsEnum || type.IsGeneratedCode ())
				return RuleResult.DoesNotApply;

			CheckAbstractClassWithoutResponsability (type);
			if (avoidUnusedParameters != null) {
				foreach (MethodDefinition method in type.GetMethods ()) {
					avoidUnusedParameters.CheckMethod (method);
				}
			}

			CheckUnnecesaryDelegation (type);
			return Runner.CurrentRuleResult;
		}
		private static bool MostlyMethodsDelegatesCall (TypeDefinition type)
		{
			if (!type.HasMethods)
				return false; // 0 / 2 + 1 <= 0

			int delegationCounter = 0;
			IList<MethodDefinition> methods = type.GetMethods ().ToList ();
			foreach (MethodDefinition method in methods) {
				if (OnlyDelegatesCall (method))
					delegationCounter++;
			}

			return methods.Count / 2 + 1 <= delegationCounter;
		}
		static bool IsAllStatic (TypeDefinition type)
		{
			if (type.HasMethods) {
				foreach (MethodDefinition ctor in type.GetConstructors ()) {
					// let's the default ctor pass (since it's always here for 1.x code)
					if (!ctor.IsStatic && ctor.HasParameters)
						return false;
				}

				foreach (MethodDefinition method in type.GetMethods ()) {
					if (!method.IsStatic)
						return false;
				}
			}
			if (type.HasFields) {
				foreach (FieldDefinition field in type.Fields) {
					if (!field.IsStatic)
						return false;
				}
			}
			return true;
		}
		public RuleResult CheckType (TypeDefinition type)
		{
			if (type.IsEnum || type.IsDelegate ())
				return RuleResult.DoesNotApply;

			if (GetMethodCount (type) == 0)
				return RuleResult.DoesNotApply;

			// we look for a Parse method defined in the type
			bool has_parse = false;
			foreach (MethodDefinition method in type.GetMethods ()) {
				if (MethodSignatures.Parse.Matches (method)) {
					// the type provides a "<type> <type>::Parse(string)" method
					has_parse = true;
					break;
				}
			}

			// if no Parse method is found; or
			// if a Parse method is found along with a TryParse method
			if (!has_parse || HasTryParseMethod (type))
				return RuleResult.Success;

			// otherwise we report a defect to add a TryParse alternative
			Runner.Report (type, Severity.Medium, Confidence.High);
			return RuleResult.Failure;
		}
Esempio n. 12
0
        private void AutoModifyMethod(TypeDefinition targetType, MethodDefinition yourMethod,
			MemberActionAttribute memberAction)
        {
            Log_modifying_member("method", yourMethod);
            var bodySource = yourMethod;
            var insertAttribute = yourMethod.GetCustomAttribute<DuplicatesBodyAttribute>();
            if (insertAttribute != null) {
                //Note that the source type is resolved using yourMethod's module, which uses a different IMetadataResolver,
                //and thus will resolve the method from the target, unmodified assembly.

                var importSourceType = insertAttribute.SourceType != null
                    ? yourMethod.Module.Import((TypeReference) insertAttribute.SourceType)
                    : yourMethod.Module.Import(targetType);

                var importMethod = importSourceType.Resolve().GetMethods(insertAttribute.MethodName,
                    yourMethod.Parameters.Select(x => x.ParameterType)).SingleOrDefault();

                var others =
                    importSourceType.Resolve().Methods.Where(x => x.Name == insertAttribute.MethodName).ToArray();

                if (importMethod == null) {
                    throw Errors.Missing_member("method", yourMethod, insertAttribute.MethodName);
                }

                bodySource = importMethod;
            }
            var modifiesMemberAttr = memberAction as ModifiesMemberAttribute;
            var newMemberAttr = memberAction as NewMemberAttribute;
            ModificationScope scope;
            string targetMethodName;

            if (modifiesMemberAttr != null) {
                targetMethodName = modifiesMemberAttr.MemberName ?? yourMethod.Name;
                scope = modifiesMemberAttr.Scope;

            } else if (newMemberAttr != null) {
                targetMethodName = yourMethod.Name;
                scope = ModificationScope.All;
            } else {
                throw Errors.Unknown_action_attribute(memberAction);
            }
            var targetMethod =
                targetType.GetMethods(targetMethodName, yourMethod.Parameters.Select(x => x.ParameterType)).FirstOrDefault();

            if (targetMethod == null) {
                throw Errors.Missing_member("method", yourMethod, targetMethodName);
            }
            if (modifiesMemberAttr != null && targetMethod.IsAbstract && (scope & ModificationScope.Body) != 0) {
                throw Errors.Invalid_member("method", yourMethod, targetMethod.FullName,
                    "You cannot modify the body of an abstract method.");
            }

            ModifyMethod(targetMethod, yourMethod, scope & ~ModificationScope.Body, newMemberAttr != null);
            ModifyMethod(targetMethod, bodySource, ModificationScope.Body & scope, false);
        }