Ejemplo n.º 1
0
        public static string ToStringProvided(
            DotMethodFPProvided provided,
            bool isLambdaApplies)
        {
            if (provided.Parameters.Length == 0)
            {
                return("no parameters");
            }

            var buf       = new StringWriter();
            var delimiter = "";

            if (!isLambdaApplies)
            {
                buf.Write(provided.Parameters.Length);
                buf.Write(" expressions");
            }
            else
            {
                foreach (DotMethodFPProvidedParam param in provided.Parameters)
                {
                    buf.Write(delimiter);

                    if (param.LambdaParamNum == 0)
                    {
                        buf.Write("an (non-lambda)");
                    }
                    else if (param.LambdaParamNum == 1)
                    {
                        buf.Write("a lambda");
                    }
                    else
                    {
                        buf.Write("a " + param.LambdaParamNum + "-parameter lambda");
                    }

                    buf.Write(" expression");
                    delimiter = " and ";
                }
            }

            return(buf.ToString());
        }
Ejemplo n.º 2
0
        public static DotMethodFP ValidateParametersDetermineFootprint(
            DotMethodFP[] footprints,
            DotMethodTypeEnum methodType,
            string methodUsedName,
            DotMethodFPProvided providedFootprint,
            DotMethodInputTypeMatcher inputTypeMatcher)
        {
            bool isLambdaApplies = DotMethodTypeEnum.ENUM == methodType;

            // determine footprint candidates strictly based on parameters
            List <DotMethodFP> candidates = null;
            DotMethodFP        bestMatch  = null;

            foreach (var footprint in footprints)
            {
                var requiredParams = footprint.Parameters;
                if (requiredParams.Length != providedFootprint.Parameters.Length)
                {
                    continue;
                }

                if (bestMatch == null)   // take first if number of parameters matches
                {
                    bestMatch = footprint;
                }

                var paramMatch = true;
                var count      = 0;
                foreach (var requiredParam in requiredParams)
                {
                    var providedParam = providedFootprint.Parameters[count++];
                    if (requiredParam.LambdaParamNum != providedParam.LambdaParamNum)
                    {
                        paramMatch = false;
                    }
                }

                if (paramMatch)
                {
                    if (candidates == null)
                    {
                        candidates = new List <DotMethodFP>();
                    }

                    candidates.Add(footprint);
                }
            }

            // if there are multiple candidates, eliminate by input (event bean collection or component collection)
            if (candidates != null && candidates.Count > 1)
            {
                candidates
                .Where(fp => !inputTypeMatcher.Matches(fp))
                .ToList()
                .ForEach(fp => candidates.Remove(fp));
            }

            // handle single remaining candidate
            if (candidates != null && candidates.Count == 1)
            {
                DotMethodFP found = candidates[0];
                ValidateSpecificTypes(methodUsedName, methodType, found.Parameters, providedFootprint.Parameters);
                return(found);
            }

            // check all candidates in detail to see which one matches, take first one
            if (candidates != null && !candidates.IsEmpty())
            {
                bestMatch = candidates[0];
                var candidateIt = candidates.GetEnumerator();
                ExprValidationException firstException = null;
                while (candidateIt.MoveNext())
                {
                    DotMethodFP fp = candidateIt.Current;
                    try {
                        ValidateSpecificTypes(methodUsedName, methodType, fp.Parameters, providedFootprint.Parameters);
                        return(fp);
                    }
                    catch (ExprValidationException ex) {
                        if (firstException == null)
                        {
                            firstException = ex;
                        }
                    }
                }

                if (firstException != null)
                {
                    throw firstException;
                }
            }

            var message = string.Format(
                "Parameters mismatch for {0} method '{1}', the method ",
                methodType.GetTypeName(),
                methodUsedName);

            if (bestMatch != null)
            {
                var buf = new StringWriter();
                buf.Write(bestMatch.ToStringFootprint(isLambdaApplies));
                buf.Write(", but receives ");
                buf.Write(DotMethodFP.ToStringProvided(providedFootprint, isLambdaApplies));
                throw new ExprValidationException(
                          string.Format("{0}requires {1}", message, buf));
            }

            if (footprints.Length == 1)
            {
                throw new ExprValidationException(
                          string.Format("{0}requires {1}", message, footprints[0].ToStringFootprint(isLambdaApplies)));
            }
            else
            {
                var buf       = new StringWriter();
                var delimiter = "";
                foreach (DotMethodFP footprint in footprints)
                {
                    buf.Write(delimiter);
                    buf.Write(footprint.ToStringFootprint(isLambdaApplies));
                    delimiter = ", or ";
                }

                throw new ExprValidationException(
                          message +
                          "has multiple footprints accepting " +
                          buf +
                          ", but receives " +
                          DotMethodFP.ToStringProvided(providedFootprint, isLambdaApplies));
            }
        }