11package com .microsoft .graph .serializer ;
22
3+ import com .google .common .annotations .VisibleForTesting ;
34import com .google .common .base .CaseFormat ;
45import com .google .gson .JsonObject ;
56import com .microsoft .graph .logger .ILogger ;
67
8+ import java .util .Locale ;
79import java .util .Objects ;
810import javax .annotation .Nonnull ;
911import javax .annotation .Nullable ;
@@ -14,12 +16,13 @@ public class DerivedClassIdentifier {
1416 private final static String ODATA_TYPE_KEY = "@odata.type" ;
1517
1618 private final ILogger logger ;
19+
1720 /**
1821 * Creates a new instance of the dereived class identifier.
1922 * @param logger The logger to use.
2023 */
2124 public DerivedClassIdentifier (@ Nonnull ILogger logger ) {
22- this .logger = Objects .requireNonNull (logger , "logger parameter cannot be null" );;
25+ this .logger = Objects .requireNonNull (logger , "logger parameter cannot be null" );
2326 }
2427
2528 /**
@@ -36,14 +39,9 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
3639 Objects .requireNonNull (jsonObject , "parameter jsonObject cannot be null" );
3740 //Identify the odata.type information if provided
3841 if (jsonObject .get (ODATA_TYPE_KEY ) != null ) {
39- /** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */
42+ // #microsoft.graph.user or #microsoft.graph.callrecords.callrecord
4043 final String odataType = jsonObject .get (ODATA_TYPE_KEY ).getAsString ();
41- final int lastDotIndex = odataType .lastIndexOf ("." );
42- final String derivedType = (odataType .substring (0 , lastDotIndex ) +
43- ".models." +
44- CaseFormat .LOWER_CAMEL .to (CaseFormat .UPPER_CAMEL ,
45- odataType .substring (lastDotIndex + 1 )))
46- .replace ("#" , "com." );
44+ final String derivedType = oDataTypeToClassName (odataType );
4745 try {
4846 Class <?> derivedClass = Class .forName (derivedType );
4947 //Check that the derived class inherits from the given parent class
@@ -61,4 +59,20 @@ public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final C
6159 //If there is no defined OData type, return null
6260 return null ;
6361 }
62+
63+ /**
64+ * Convert {@code @odata.type} to proper java class name
65+ *
66+ * @param odataType to convert
67+ * @return converted class name
68+ */
69+ @ VisibleForTesting
70+ static String oDataTypeToClassName (@ Nonnull String odataType ) {
71+ Objects .requireNonNull (odataType );
72+ final int lastDotIndex = odataType .lastIndexOf ("." );
73+ return (odataType .substring (0 , lastDotIndex ).toLowerCase (Locale .ROOT ) +
74+ ".models." +
75+ CaseFormat .LOWER_CAMEL .to (CaseFormat .UPPER_CAMEL , odataType .substring (lastDotIndex + 1 )))
76+ .replace ("#" , "com." );
77+ }
6478}
0 commit comments