CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Java.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace CodeGen.generators
6 {
11  public class JavaGenerator : Generator
12  {
13  private const string ClassFormat = "{0}class {1} {2}{{{3}{4}{5}}}";
14  private string Indent { get; } = GeneratorConf.GetIndent(true, 4);
15 
17  protected override string GenerateClass(Class @class)
18  {
19  string access = "", fields = "", inherits = "", methods = "", classes = "";
20 
21  if (@class.Parent?.Length > 0)
22  {
23  inherits = "extends " + @class.Parent + " ";
24  }
25 
26  fields = @class.Fields?.Aggregate('\n' + fields,
27  (current, field) => current + GenerateField(field) + '\n');
28 
29  methods = @class.Methods?.Aggregate(methods,
30  (current, method) => current + '\n' + GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + '\n');
31 
32  classes = @class.Classes?.Aggregate(classes,
33  (current, cls) => current + '\n' + GeneratorConf.ShiftCode(GenerateClass(cls), 1, Indent) + '\n');
34 
35  if (@class.Access?.Length > 0)
36  {
37  access = @class.Access + ' ';
38  }
39 
40  return string.Format(ClassFormat, access, @class.Name, inherits, fields, methods, classes);
41  }
42 
45  public override string GenerateField(Field field)
46  {
47  if (!new JavaValidator().FieldIsValid(field))
48  throw new ArgumentNullException();
49 
50  var result = Indent;
51  if (string.IsNullOrWhiteSpace(field.Access) || field.Access == "default")
52  {
53  result += "private ";
54  }
55  else
56  {
57  result += field.Access + ' ';
58  }
59 
60  if (field.Const)
61  {
62  result += "const ";
63  }
64 
65  if (field.Static)
66  {
67  result += "static ";
68  }
69 
70  result += field.Type + " ";
71 
72  result += field.Name;
73  if (field.Default?.Length > 0)
74  {
75  result += " = " + field.Default;
76  }
77 
78  result += ';';
79  return result;
80  }
81 
83  public override string GenerateMethod(Method method)
84  {
85  var result = "";
86  if (method.Access == "" || method.Access == "default")
87  {
88  result += "private ";
89  }
90  else
91  {
92  result += method.Access + ' ';
93  }
94 
95  if (method.Static)
96  {
97  result += "static ";
98  }
99 
100  result += method.Type + " ";
101 
102  result += method.Name;
103  result += '(';
104 
105  for (var i = 0; i < method.Parameters?.Length; i++)
106  {
107  var parameter = method.Parameters[i];
108  result += parameter.Type + " " + parameter.Name;
109  if (i + 1 < method.Parameters.Length)
110  {
111  result += ", ";
112  }
113  }
114 
115  result += ") {";
116 
117  if (!string.IsNullOrWhiteSpace(method.Type) && method.Type != "void")
118  {
119  string defaultVal;
120  if (JavaNormalizer.BuiltInDefaults.ContainsKey(method.Type))
121  {
122  defaultVal = JavaNormalizer.BuiltInDefaults[method.Type];
123  }
124  else
125  {
126  defaultVal = "new " + method.Type + "()";
127  }
128 
129  result += '\n' + Indent + "return " + defaultVal + ";\n";
130  }
131 
132  result += '}';
133  return result;
134  }
135 
137  public override string GetIndent()
138  {
139  return Indent;
140  }
141  }
142 
145  public class JavaNormalizer : Normalizer
146  {
147  private static Normalizer _singletonInstance = null;
148 
149  private JavaNormalizer()
150  {
151  }
152 
156  public static readonly Dictionary<string, string> BuiltInDefaults = new Dictionary<string, string>
157  {
158  {"int", "0"},
159  {"double", "0.0"},
160  {"char", "'a'"},
161  {"boolean", "false"},
162  {"String", "\"\""},
163  };
164 
169  public static Normalizer GetNormalizer()
170  {
171  return _singletonInstance ?? (_singletonInstance = new JavaNormalizer());
172  }
173 
175  protected override string NormalizeType(string type)
176  {
177  if (type == "bool")
178  type = "boolean";
179  else if (type == "string")
180  type = "String";
181  else if (type == "float")
182  type = "double";
183  else if (string.IsNullOrWhiteSpace(type))
184  type = "void";
185 
186  return type;
187  }
188  }
189 
190  public class JavaValidator : Validator
191  {
192  }
193 }
override string GetIndent()
Gets indentation of current generator identation
Definition: Java.cs:137
bool Const
Denotes if field is constant or not. Type: boolean
Definition: Models.cs:133
string Type
Represents return type of the method. Type: string
Definition: Models.cs:178
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
Definition: Models.cs:48
bool Static
Denotes if the mathod is static or not. Type: boolean
Definition: Models.cs:196
bool Static
Denotes if field is static or not. Type: boolean
Definition: Models.cs:139
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: Java.cs:169
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
Holds the configuration of generator
Generator for Java
Definition: Java.cs:11
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: Java.cs:17
override string GenerateField(Field field)
Definition: Java.cs:45
Interface of language normalizer: normalizes package data according to specified language ...
static readonly Dictionary< string, string > BuiltInDefaults
The dictionary of built in values
Definition: Java.cs:156
Normalizer for Java
Definition: Java.cs:145
override string NormalizeType(string type)
Type normalizer: fixes the type to language's built in
Definition: Java.cs:175
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Access
Represents access level of the field. Type: string
Definition: Models.cs:127
string Default
Represents default value of the varibale. Type: string
Definition: Models.cs:114
Interface of language generator
string Access
Represents access level of the method. Type: string
Definition: Models.cs:184
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: Java.cs:83
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121