Skip to content

Commit 31c2d01

Browse files
authored
Merge pull request #274 from microsoftgraph/bugfix/java-import-keyword
Bugfix/java import keyword
2 parents 7ff9b5a + 23a115d commit 31c2d01

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

Templates/Android/BaseModel.template.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444

4545
public string MethodName(OdcmObject c) {
46-
return c.Name.Substring(c.Name.IndexOf(".") + 1).ToUpperFirstChar();
46+
return c.Name.Substring(c.Name.IndexOf(".") + 1).SanitizePropertyName(property).ToUpperFirstChar();
4747
}
4848

4949
public string MethodFullName(OdcmObject c) {

Templates/Java/BaseJavaModel.template.tt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
}
4444

4545
public string MethodName(OdcmObject c) {
46-
return c.Name.Substring(c.Name.IndexOf(".") + 1).ToUpperFirstChar();
46+
return c.Name.Substring(c.Name.IndexOf(".") + 1).SanitizePropertyName(c).ToUpperFirstChar();
4747
}
4848

4949
public string MethodFullName(OdcmObject c) {
@@ -1089,7 +1089,7 @@ public {1} {2}{3}{4} {{";
10891089
propertyName.SplitCamelCase(),
10901090
property.Name,
10911091
propertyType,
1092-
property.Name.SanitizePropertyName(property).ToLowerFirstChar(),
1092+
property.Name.ToLowerFirstChar().SanitizePropertyName(property),
10931093
GetSanitizedDescription(property));
10941094
}
10951095
return sb.ToString();

src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static bool IsComplex(this OdcmType type)
198198

199199
public static bool IsComplex(this string t)
200200
{
201-
return !TypeHelperCSharp.SimpleTypes.Contains(t);
201+
return !SimpleTypes.Contains(t);
202202
}
203203

204204
public static string GetNamespaceName(this OdcmNamespace namespaceObject)

src/GraphODataTemplateWriter/CodeHelpers/Java/TypeHelperJava.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ namespace Microsoft.Graph.ODataTemplateWriter.CodeHelpers.Java
1010

1111
public static class TypeHelperJava
1212
{
13-
private static Logger logger = LogManager.GetCurrentClassLogger();
13+
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
1414
public const string ReservedPrefix = "msgraph";
15-
public static HashSet<string> ReservedNames
16-
{
17-
get
18-
{
19-
return new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
20-
"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", "true", "false", "null"
21-
};
22-
}
23-
}
15+
public static Lazy<HashSet<string>> ReservedNames { get; private set; } = new Lazy<HashSet<string>>(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
16+
"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", "true", "false", "null", "import"
17+
});
2418

2519
public static string GetReservedPrefix(this OdcmType @type)
2620
{
@@ -73,7 +67,7 @@ public static string GetTypeString(this OdcmProperty property)
7367
{
7468
var propertyType = property.Projection.Type;
7569
var typeString = GetTypeString(propertyType);
76-
if (propertyType.Namespace != OdcmNamespace.Edm && ReservedNames.Contains(typeString))
70+
if (propertyType.Namespace != OdcmNamespace.Edm && ReservedNames.Value.Contains(typeString))
7771
{
7872
typeString = "com.microsoft.graph.models.extensions." + typeString;
7973
}
@@ -95,27 +89,19 @@ public static string GetToLowerFirstCharName(this OdcmProperty property)
9589

9690
public static string SanitizePropertyName(this string property, OdcmObject odcmProperty = null)
9791
{
98-
if (ReservedNames.Contains(property))
92+
if (ReservedNames.Value.Contains(property))
9993
{
100-
logger.Info("Property \"{0}\" is a reserved word in Java. Converting to \"{1}{0}\"", property, ReservedPrefix);
101-
return ReservedPrefix + property;
94+
var result = ReservedPrefix + property.ToUpperFirstChar();
95+
logger.Info($"Property \"{property}\" is a reserved word in Java. Converting to \"{result}\"");
96+
return result;
10297
}
103-
104-
if (odcmProperty != null && property == odcmProperty.Name.ToUpperFirstChar())
98+
else if (property == odcmProperty?.Name?.ToUpperFirstChar() && !(odcmProperty?.Name?.StartsWith("_") ?? false))
10599
{
106-
// Check whether the property type is the same as the class name.
107-
if (odcmProperty.Projection.Type.Name.ToUpperFirstChar() == odcmProperty.Name.ToUpperFirstChar())
108-
{
109-
// Name the property: {metadataName} + "Property"
110-
logger.Info("Property type \"{0}\" has the same name as the class. Converting to \"{0}Property\"", property);
111-
return string.Concat(property, "Property");
112-
}
113-
114100
// Name the property by its type. Sanitize it in case the type is a reserved name.
115-
return odcmProperty.Projection.Type.Name.ToUpperFirstChar().SanitizePropertyName(odcmProperty);
101+
return odcmProperty?.Projection?.Type?.Name?.ToUpperFirstChar()?.SanitizePropertyName(odcmProperty) ?? odcmProperty?.Name?.SanitizePropertyName();
116102
}
117-
118-
return property.Replace("@", string.Empty).Replace(".", string.Empty);
103+
else
104+
return property?.Replace("@", string.Empty)?.Replace(".", string.Empty);
119105
}
120106

121107
public static string GetToLowerImport(this OdcmProperty property)

0 commit comments

Comments
 (0)