Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            // Console.WriteLine("App config path: " + Settings.Default);

            Type webClientType = typeof(WebClient);
            var wrapperRep = new WrapperRepository(typeof(HtmlPage).Assembly);
            //GetJavaPropsFromType(webClientType);
            //GetJavaPropsFromType(typeof(HtmlPage));
            //GetJavaPropsFromType(typeof(HtmlInput));

            wrapperRep.GenerateUntilDone(typeof(DomNode), typeof(WebClient), typeof(HtmlPage));

            //var classInfoList = CreateTypeList(
            //   new[] { typeof(HtmlPage).Assembly },
            //   new[]
            //   {
            //      typeof(HtmlPage),
            //      typeof(SgmlPage)
            //   }).Select(t => new WrapperClassInfo(t.Value, wrapperRep))
            //   .ToArray()
            //   .Select((ci => ci.GenerateClassCode(new StringBuilder()).ToString()));

            //foreach (var src in classInfoList)
            //   Console.WriteLine(src);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Done! Press any key to exit.");

            Console.ReadKey();
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            // Console.WriteLine("App config path: " + Settings.Default);

            Type webClientType = typeof(WebClient);
            var  wrapperRep    = new WrapperRepository(typeof(HtmlPage).Assembly);

            //GetJavaPropsFromType(webClientType);
            //GetJavaPropsFromType(typeof(HtmlPage));
            //GetJavaPropsFromType(typeof(HtmlInput));

            wrapperRep.GenerateUntilDone(typeof(DomNode), typeof(WebClient), typeof(HtmlPage));

            wrapperRep.GenerateAssemblyInfo();
            wrapperRep.GenerateNuspec();
            //var classInfoList = CreateTypeList(
            //   new[] { typeof(HtmlPage).Assembly },
            //   new[]
            //   {
            //      typeof(HtmlPage),
            //      typeof(SgmlPage)
            //   }).Select(t => new WrapperClassInfo(t.Value, wrapperRep))
            //   .ToArray()
            //   .Select((ci => ci.GenerateClassCode(new StringBuilder()).ToString()));

            //foreach (var src in classInfoList)
            //   Console.WriteLine(src);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Done! Press any key to exit.");

            Console.ReadKey();
        }
Exemplo n.º 3
0
 public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
 {
     this.wrapperRepository = wrapperRepository;
     this.wrappedType = wrappedFromType;
     this.properties = new Dictionary<string, WrapperPropInfo>();
     this.methods = new Dictionary<string, WrapperMethodInfo>();
     this.constructors = new List<WrapperConstructorInfo>();
     this.staticPublicFields = new List<WrapperStaticPublicField>();
     ScanMembers(wrappedFromType);
 }
Exemplo n.º 4
0
 public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
 {
     this.wrapperRepository  = wrapperRepository;
     this.wrappedType        = wrappedFromType;
     this.properties         = new Dictionary <string, WrapperPropInfo>();
     this.methods            = new Dictionary <string, WrapperMethodInfo>();
     this.constructors       = new List <WrapperConstructorInfo>();
     this.staticPublicFields = new List <WrapperStaticPublicField>();
     ScanMembers(wrappedFromType);
 }
Exemplo n.º 5
0
 public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
 {
     this.wrapperRepository = wrapperRepository;
     this.wrappedType = wrappedFromType;
     this.properties = new Dictionary<string, WrapperPropInfo>();
     this.methods = new Dictionary<string, WrapperMethodInfo>();
     this.constructors = new List<WrapperConstructorInfo>();
     this.staticPublicFields = new List<WrapperStaticPublicField>();
     ScanMembers(wrappedFromType);
     Assembly htmlUnitAssembly = Assembly.GetAssembly(typeof(WebClient));
     this.htmlUnitVersion = htmlUnitAssembly.GetName().Version.ToString();
 }
Exemplo n.º 6
0
        public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
        {
            this.wrapperRepository  = wrapperRepository;
            this.wrappedType        = wrappedFromType;
            this.properties         = new Dictionary <string, WrapperPropInfo>();
            this.methods            = new Dictionary <string, WrapperMethodInfo>();
            this.constructors       = new List <WrapperConstructorInfo>();
            this.staticPublicFields = new List <WrapperStaticPublicField>();
            ScanMembers(wrappedFromType);
            Assembly htmlUnitAssembly = Assembly.GetAssembly(typeof(WebClient));

            this.htmlUnitVersion = htmlUnitAssembly.GetName().Version.ToString();
        }
Exemplo n.º 7
0
        public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
        {
            this.wrapperRepository = wrapperRepository;
            this.wrappedType = wrappedFromType;
            this.properties = new Dictionary<string, WrapperPropInfo>();
            this.methods = new Dictionary<string, WrapperMethodInfo>();
            this.constructors = new List<WrapperConstructorInfo>();
            this.staticPublicFields = new List<WrapperStaticPublicField>();
            ScanMembers(wrappedFromType);
            Assembly htmlUnitAssembly = Assembly.GetAssembly(typeof(WebClient));
            this.htmlUnitVersion = htmlUnitAssembly.GetName().Version.ToString();

            const string reservedKeywordsList = "object,void,event,delegate,int,double," +
                                                "namespace,checked,string,decimal,where,if,else,params,as";
            reservedKeywords = reservedKeywordsList.Split(',');
            this.wrappedTypeFullNameSanitized = SanitizeTypeName(this.wrappedType.FullName);
        }
        public WrapperClassInfo(Type wrappedFromType, WrapperRepository wrapperRepository)
        {
            this.wrapperRepository  = wrapperRepository;
            this.wrappedType        = wrappedFromType;
            this.properties         = new Dictionary <string, WrapperPropInfo>();
            this.methods            = new Dictionary <string, WrapperMethodInfo>();
            this.constructors       = new List <WrapperConstructorInfo>();
            this.staticPublicFields = new List <WrapperStaticPublicField>();
            ScanMembers(wrappedFromType);
            Assembly htmlUnitAssembly = Assembly.GetAssembly(typeof(WebClient));

            this.htmlUnitVersion = htmlUnitAssembly.GetName().Version.ToString();


            const string reservedKeywordsList = "object,void,event,delegate,int,double," +
                                                "namespace,checked,string,decimal,where,if,else,params,as";

            reservedKeywords = reservedKeywordsList.Split(',');
            this.wrappedTypeFullNameSanitized = SanitizeTypeName(this.wrappedType.FullName);
        }
Exemplo n.º 9
0
 public WrapperPropInfo(WrapperClassInfo wci, string propName, WrapperRepository wrapperRepository)
 {
     Name            = propName;
     this.classInfo  = wci;
     this.repository = wrapperRepository;
 }
Exemplo n.º 10
0
        public void GeneratePropertyCode(StringBuilder sb)
        {
            if (PropertyType.IsNested)
            {
                Console.WriteLine("TODO: Support for nested class: " + PropertyType.FullName);
                return;
            }

            var targetType = PropertyType;

            var validMapping = WrapperRepository.GetValidMapping(targetType);

            string getBody = string.Empty;

            if (validMapping != null)
            {
                Type collectionType = Repository.GetMethodReturnListType(GetterMethod);

                if (collectionType == null)
                {
                    Console.WriteLine("TODO: Recognize collection " + FullNameDest);
                }
                else if (collectionType.IsNested)
                {
                    Console.WriteLine("TODO: Support for nested class: " + collectionType.FullName);
                }
                else
                {
                    if (SetterMethod != null)
                    {
                        Console.WriteLine("TODO: Can't generate setter wrapper for wrapped collection " + FullNameDest);
                    }

                    if (Repository.TypeIsWrapped(collectionType))
                    {
                        Repository.MarkUsageOfType(collectionType);
                    }

                    string collectionTypeName = Repository.TypeIsWrapped(collectionType)
                                                    ? Repository.GetTargetFullName(collectionType)
                                                    : collectionType.FullName;

                    if (ClassInfo.IsInterface)
                    {
                        sb.AppendFormat(
                            "      {0}<{1}> {2} {{ get; }}\r\n", validMapping.ToTypeName, collectionTypeName, Name);
                    }
                    else
                    {
                        getBody =
                            @"
      public {0}<{1}> {2}
      {{
         get
         {{
            return new {3}<{1}>(
               WObj.{4}());
         }}
       }}
";

                        sb.AppendFormat(
                            getBody,
                            /* {0} */ validMapping.ToTypeName,
                            /* {1} */ collectionTypeName,
                            /* {2} */ Name,
                            /* {3} */ Repository.TypeIsWrapped(collectionType)
                                          ? validMapping.FullWrapperName
                                          : validMapping.ShallowWrapperName,
                            /* {4} */ GetterMethod.Name);
                    }
                }
            }
            else
            {
                var propTypeIsWrapped = Repository.TypeIsWrapped(PropertyType);

                if (propTypeIsWrapped)
                {
                    Repository.MarkUsageOfType(PropertyType);
                }

                string typeName = propTypeIsWrapped
                                      ? Repository.GetTargetFullName(PropertyType)
                                      : PropertyType.FullName;

                if (ClassInfo.IsInterface)
                {
                    string propBody =
                        SetterMethod != null
                            ? "{ get; set; }"
                            : " { get; }";

                    sb.AppendFormat("      {0} {1} {2}\r\n", typeName, Name, propBody);
                }
                else
                {
                    if (!propTypeIsWrapped)
                    {
                        var setterBodyTemplate =
                            @"
         set
         {{
            WObj.{0}(value);
         }}
";

                        var propBodyTemplate =
                            @"
      public {0} {1}
      {{
         get
         {{
            return WObj.{2}();
         }}{3}
      }}
";

                        sb.AppendFormat(
                            propBodyTemplate,
                            /* {0} */ typeName,
                            /* {1} */ Name,
                            /* {2} */ GetterMethod.Name,
                            /* {3} */ SetterMethod != null
                                          ? string.Format(setterBodyTemplate, SetterMethod.Name)
                                          : string.Empty);
                    }
                    else
                    {
                        var setterBodyTemplate =
                            @"
         set
         {{
            WObj.{0}(({1})value.WrappedObject);
         }}
";

                        var propBodyTemplate =
                            @"
      public {0} {1}
      {{
         get
         {{
            return ObjectWrapper.CreateWrapper<{0}>(
               WObj.{2}());
         }}{3}
      }}

";

                        sb.AppendFormat(
                            propBodyTemplate,
                            /* {0} */ typeName,
                            /* {1} */ Name,
                            /* {2} */ GetterMethod.Name,
                            /* {3} */ SetterMethod != null
                                          ? string.Format(setterBodyTemplate, SetterMethod.Name, PropertyType.FullName)
                                          : string.Empty);
                    }
                }
            }
        }
Exemplo n.º 11
0
        internal void GenerateMethodCode(StringBuilder sb)
        {
            sb.AppendLine("// Generating method code for " + TargetMethodInfo.Name);

            var validListMapping         = WrapperRepository.GetValidMapping(TargetMethodInfo.ReturnType);
            var returnValueIsWrappedList = validListMapping != null;

            string returnTypeName         = null;
            bool   returnTypeIsWrapped    = false;
            Type   listElementType        = null;
            bool   listElementsAreWrapped = false;

            if (returnValueIsWrappedList)
            {
                listElementType = Repository.GetMethodReturnListType(TargetMethodInfo);

                if (listElementType == null)
                {
                    listElementType = typeof(object);
                }

                listElementsAreWrapped =
                    Repository.TypeIsWrapped(listElementType);

                var genericParameter =
                    listElementsAreWrapped
                        ? Repository.GetTargetFullName(listElementType)
                        : listElementType.FullName;

                returnTypeName = string.Format("{0}<{1}>", validListMapping.ToTypeName, genericParameter);
            }
            else
            {
                returnTypeIsWrapped = Repository.TypeIsWrapped(TargetMethodInfo.ReturnType);

                returnTypeName = returnTypeIsWrapped
                    ? Repository.GetTargetFullName(TargetMethodInfo.ReturnType)
                    : TargetMethodInfo.ReturnType.FullName;

                if (!returnTypeIsWrapped)
                {
                    string nativeTypeName = Repository.TranslateToNativeTypeName(TargetMethodInfo.ReturnType);

                    if (!string.IsNullOrEmpty(nativeTypeName))
                    {
                        returnTypeName = nativeTypeName;
                    }
                }
                else
                {
                    Repository.MarkUsageOfType(TargetMethodInfo.ReturnType);
                }
            }

            // Change from camelCase to UpperCamelCase

            string origName        = TargetMethodInfo.Name;
            string transformedName = origName.Substring(0, 1).ToUpper() + origName.Substring(1);

            // Check for condition where a method name has same name as property
            if (transformedName == ClassInfo.TargetNameWithoutNamespace)
            {
                if (transformedName == "Cache")
                {
                    transformedName = "AddToCache";
                }
            }

            // No "public" prefix on interface definition
            var publicStr = ClassInfo.IsInterface ? "" : "public ";

            var virtualStr = (!ClassInfo.IsInterface && TargetMethodInfo.IsVirtual) ? "virtual " : string.Empty;

            sb.AppendFormat("      {0}{1}{2} {3}(", publicStr, virtualStr, returnTypeName, transformedName);

            bool firstParameter = true;

            var parameters =
                TargetMethodInfo.GetParameters().Select(pi => (new WrapperParameterInfo(this, pi))).ToArray();

            foreach (var mp in parameters)
            {
                if (Repository.TypeIsWrapped(mp.ParameterType))
                {
                    Repository.MarkUsageOfType(mp.ParameterType);
                }
            }

            foreach (var mp in parameters)
            {
                if (!firstParameter)
                {
                    sb.Append(", ");
                }
                firstParameter = false;

                var parameterTypeName = mp.ParameterTypeName.Replace('+', '.');
                sb.AppendFormat("{0} {1}", parameterTypeName, mp.ParameterName);
            }
            sb.Append(")");

            if (ClassInfo.IsInterface)
            {
                sb.Append(";\r\n");
            }
            else
            {
                sb.Append("\r\n      {\r\n");

                // Generate function call
                var functionCallSb = new StringBuilder();

                functionCallSb.AppendFormat("WObj.{0}(", TargetMethodInfo.Name);

                firstParameter = true;
                foreach (var mp in parameters)
                {
                    if (!firstParameter)
                    {
                        functionCallSb.Append(", ");
                    }
                    firstParameter = false;

                    if (mp.IsWrapped)
                    {
                        functionCallSb.AppendFormat(
                            "({0}){1}.WrappedObject", mp.ParameterType.FullName, mp.ParameterName);
                    }
                    else
                    {
                        functionCallSb.Append(mp.ParameterName);
                    }
                }

                functionCallSb.Append(")");

                if (TargetMethodInfo.ReturnType != typeof(void))
                {
                    if (returnValueIsWrappedList)
                    {
                        const string template = @"
         return new {0}<{1}>({2});
";
                        sb.AppendFormat(
                            template,
                            listElementsAreWrapped
                                ? validListMapping.FullWrapperName
                                : validListMapping.ShallowWrapperName,
                            listElementsAreWrapped
                                ? Repository.GetTargetFullName(listElementType)
                                : listElementType.FullName,
                            functionCallSb);
                    }
                    else
                    {
                        if (returnTypeIsWrapped)
                        {
                            sb.AppendFormat(
                                "         return ObjectWrapper.CreateWrapper<{0}>({1});\r\n",
                                returnTypeName,
                                functionCallSb);
                        }
                        else
                        {
                            sb.AppendFormat("         return {0};\r\n", functionCallSb);
                        }
                    }
                }
                else
                {
                    sb.AppendFormat("         {0};\r\n", functionCallSb);
                }

                sb.Append("      }\r\n\r\n");
            }
        }
Exemplo n.º 12
0
 public WrapperPropInfo(WrapperClassInfo wci, string propName, WrapperRepository wrapperRepository)
 {
     this.name = propName;
     this.classInfo = wci;
     this.repository = wrapperRepository;
 }