Example #1
0
        public static JavaTypeName Parse(string dottedFullName)
        {
            var ret = new JavaTypeName();

            foreach (var label in genericConstraintsLabels)
            {
                int gcidx  = dottedFullName.IndexOf(label, StringComparison.Ordinal);
                int gcgidx = gcidx < 0 ? -1 : dottedFullName.IndexOf('<', 0, gcidx);
                int gccidx = gcidx < 0 ? -1 : dottedFullName.IndexOf(',', 0, gcidx);
                if (gcidx > 0 && gcgidx < 0 && gccidx < 0)
                {
                    string args = dottedFullName.Substring(gcidx + label.Length).Trim();
                    ret.BoundsType         = label;
                    ret.GenericConstraints = ParseCommaSeparatedTypeNames(args).Select(s => Parse(s)).ToArray();
                    dottedFullName         = dottedFullName.Substring(0, gcidx).Trim();
                }
            }

            if (dottedFullName.EndsWith("...", StringComparison.Ordinal))
            {
                ret.ArrayPart  = "...";
                dottedFullName = dottedFullName.Substring(0, dottedFullName.Length - 3);
            }
            while (dottedFullName.LastOrDefault() == ']')
            {
                int aidx = dottedFullName.LastIndexOf('[');
                ret.ArrayPart += dottedFullName.Substring(aidx);
                dottedFullName = dottedFullName.Substring(0, aidx);
            }

            int idx       = dottedFullName.IndexOf('<');
            int nextIndex = dottedFullName.Length;

            if (idx > 0)
            {
                int last = GetMatchingGenericCloser(dottedFullName, idx + 1);
                ret.GenericArguments = ParseCommaSeparatedTypeNames(dottedFullName.Substring(idx + 1, last - idx - 1))
                                       .Select(s => JavaTypeName.Parse(s.Trim()))
                                       .ToArray();
                nextIndex = last + 1;
            }
            // at this state, there is no way to distinguish package name from this name specification.
            ret.DottedName = idx < 0 ? dottedFullName : dottedFullName.Substring(0, idx);

            if (nextIndex < dottedFullName.Length)
            {
                if (dottedFullName [nextIndex] != '.')
                {
                    throw new ArgumentException(nameof(dottedFullName));
                }
                // the generic parent is parsed, but the rest is still there.
                var parent = ret;
                ret = Parse(dottedFullName.Substring(nextIndex + 1));
                ret.GenericParent = parent;
            }

            return(ret);
        }
Example #2
0
        public static JavaTypeName Parse(string dottedFullName)
        {
            var ret = new JavaTypeName();

            foreach (var label in genericConstraintsLabels)
            {
                int gcidx  = dottedFullName.IndexOf(label, StringComparison.Ordinal);
                int gcgidx = gcidx < 0 ? -1 : dottedFullName.IndexOf('<', 0, gcidx);
                int gccidx = gcidx < 0 ? -1 : dottedFullName.IndexOf(',', 0, gcidx);
                if (gcidx > 0 && gcgidx < 0 && gccidx < 0)
                {
                    string args = dottedFullName.Substring(gcidx + label.Length).Trim();
                    ret.GenericConstraints = ParseCommaSeparatedTypeNames(args).Select(s => Parse(s)).ToArray();
                    dottedFullName         = dottedFullName.Substring(0, gcidx).Trim();
                }
            }

            if (dottedFullName.EndsWith("...", StringComparison.Ordinal))
            {
                ret.ArrayPart  = "...";
                dottedFullName = dottedFullName.Substring(0, dottedFullName.Length - 3);
            }
            while (dottedFullName.LastOrDefault() == ']')
            {
                int aidx = dottedFullName.LastIndexOf('[');
                ret.ArrayPart += dottedFullName.Substring(aidx);
                dottedFullName = dottedFullName.Substring(0, aidx);
            }

            int idx = dottedFullName.IndexOf('<');

            if (idx > 0)
            {
                int last = dottedFullName.LastIndexOf('>');
                ret.GenericArguments = ParseCommaSeparatedTypeNames(dottedFullName.Substring(idx + 1, last - idx - 1))
                                       .Select(s => JavaTypeName.Parse(s.Trim()))
                                       .ToArray();
                dottedFullName = dottedFullName.Substring(0, idx);
            }

            // at this state, there is no way to distinguish package name from this name specification.
            ret.FullNameNonGeneric = dottedFullName;

            return(ret);
        }
        public static JavaTypeReference Parse(this JavaApi api, string name, params JavaTypeParameters [] contextTypeParameters)
        {
            var tn = JavaTypeName.Parse(name);

            return(JavaTypeNameToReference(api, tn, contextTypeParameters));
        }