Exemple #1
0
        /// <summary>Build the class header source code. This encompass :
        /// - The class package definition.
        /// - The import definitions
        /// - The class definition including the type it extends and the interfaces it
        ///   implements.</summary>
        /// <param name="definition">Class definition.</param>
        /// <param name="importedTypes"></param>
        /// <returns>A <see cref="StringBuilder"/> holding the source code.</returns>
        private StringBuilder BuildClassHeaderSourceCode(IClass definition,
            List<IJavaType> importedTypes)
        {
            StringBuilder resultBuilder = new StringBuilder();
            string thisClassJavaNamespace = definition.NamespaceJavaName;
            // string thisClassCanonicName = JavaHelpers.GetCanonicTypeName(definition.Name, out thisClassNamespace);
            string thisClassCanonicName = definition.Name;

            // Resolve implemented interfaces namespaces.
            List<string> simpleInterfaceNames = new List<string>();
            string simpleBaseClassName = definition.SuperClass.Name;

            foreach (string fullInterfaceName in definition.EnumerateImplementedInterfaces()) {
                string simpleInterfaceName = fullInterfaceName;
                simpleInterfaceNames.Add(simpleInterfaceName);
            }

            if (!string.IsNullOrEmpty(thisClassJavaNamespace)) {
                resultBuilder.AppendFormat("package {0};\n", thisClassJavaNamespace);
            }
            resultBuilder.Append("\n");
            foreach (IJavaType importedType in importedTypes) {
                // Types from the same namespace than the reversed class are automatically
                // imported. No needs to emit an import directive.
                if (object.ReferenceEquals(importedType.NamespaceJavaName, thisClassJavaNamespace)) { continue; }
                resultBuilder.AppendFormat(string.Format("import {0};\n", importedType.FullyQualifiedJavaName));
            }
            resultBuilder.AppendLine();
            resultBuilder.Append(Helpers.GetModifiersSourceCode(definition.Access));
            if (definition.IsEnumeration) { resultBuilder.Append("enum "); }
            else if (definition.IsInterface) { resultBuilder.Append("interface "); }
            else { resultBuilder.Append("class "); }
            resultBuilder.AppendFormat(" {0} ", thisClassCanonicName);
            if (!definition.IsInterface) { resultBuilder.AppendFormat("extends {0}", simpleBaseClassName); }
            foreach (string interfaceName in simpleInterfaceNames) {
                resultBuilder.AppendFormat(", {0}", interfaceName);
            }
            resultBuilder.AppendLine();
            resultBuilder.AppendLine("{");
            return resultBuilder;
        }