|
1 | | -package com.microsoft.graph.serializer; |
2 | | - |
3 | | -import com.google.common.base.CaseFormat; |
4 | | -import com.google.gson.JsonObject; |
5 | | -import com.microsoft.graph.logger.ILogger; |
6 | | - |
7 | | -import java.util.Objects; |
8 | | -import javax.annotation.Nonnull; |
9 | | -import javax.annotation.Nullable; |
10 | | - |
11 | | -public class DerivedClassIdentifier { |
12 | | - |
13 | | - private final static String ODATA_TYPE_KEY = "@odata.type"; |
14 | | - private final ILogger logger; |
15 | | - |
16 | | - public DerivedClassIdentifier(ILogger logger) { |
17 | | - this.logger = logger; |
18 | | - } |
19 | | - |
20 | | - /** |
21 | | - * Get the derived class for the given JSON object |
22 | | - * This covers scenarios in which the service may return one of several derived types |
23 | | - * of a base object, which it defines using the odata.type parameter |
24 | | - * |
25 | | - * @param jsonObject the raw JSON object of the response |
26 | | - * @param parentClass the parent class the derived class should inherit from |
27 | | - * @return the derived class if found, or null if not applicable |
28 | | - */ |
29 | | - @Nullable |
30 | | - public Class<?> getDerivedClass(@Nonnull final JsonObject jsonObject, @Nullable final Class<?> parentClass) { |
31 | | - Objects.requireNonNull(jsonObject, "parameter jsonObject cannot be null"); |
32 | | - //Identify the odata.type information if provided |
33 | | - if (jsonObject.get(ODATA_TYPE_KEY) != null) { |
34 | | - /** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */ |
35 | | - final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString(); |
36 | | - final int lastDotIndex = odataType.lastIndexOf("."); |
37 | | - final String derivedType = (odataType.substring(0, lastDotIndex) + |
38 | | - ".models." + |
39 | | - CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, |
40 | | - odataType.substring(lastDotIndex + 1))) |
41 | | - .replace("#", "com."); |
42 | | - try { |
43 | | - Class<?> derivedClass = Class.forName(derivedType); |
44 | | - //Check that the derived class inherits from the given parent class |
45 | | - if (parentClass == null || parentClass.isAssignableFrom(derivedClass)) { |
46 | | - return derivedClass; |
47 | | - } |
48 | | - return null; |
49 | | - } catch (ClassNotFoundException e) { |
50 | | - logger.logDebug("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class."); |
51 | | - //If we cannot determine the derived type to cast to, return null |
52 | | - //This may happen if the API and the SDK are out of sync |
53 | | - return null; |
54 | | - } |
55 | | - } |
56 | | - //If there is no defined OData type, return null |
57 | | - return null; |
58 | | - } |
59 | | -} |
| 1 | +package com.microsoft.graph.serializer; |
| 2 | + |
| 3 | +import com.google.common.base.CaseFormat; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.microsoft.graph.logger.ILogger; |
| 6 | + |
| 7 | +import java.util.Objects; |
| 8 | +import javax.annotation.Nonnull; |
| 9 | +import javax.annotation.Nullable; |
| 10 | + |
| 11 | +public class DerivedClassIdentifier { |
| 12 | + |
| 13 | + private final static String ODATA_TYPE_KEY = "@odata.type"; |
| 14 | + |
| 15 | + private final ILogger logger; |
| 16 | + |
| 17 | + public DerivedClassIdentifier(@Nonnull ILogger logger) { |
| 18 | + this.logger = Objects.requireNonNull(logger, "logger parameter cannot be null");; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the derived class for the given JSON object |
| 23 | + * This covers scenarios in which the service may return one of several derived types |
| 24 | + * of a base object, which it defines using the odata.type parameter |
| 25 | + * |
| 26 | + * @param jsonObject the raw JSON object of the response |
| 27 | + * @param parentClass the parent class the derived class should inherit from |
| 28 | + * @return the derived class if found, or null if not applicable |
| 29 | + */ |
| 30 | + @Nullable |
| 31 | + public Class<?> identify(@Nonnull final JsonObject jsonObject, @Nullable final Class<?> parentClass) { |
| 32 | + Objects.requireNonNull(jsonObject, "parameter jsonObject cannot be null"); |
| 33 | + //Identify the odata.type information if provided |
| 34 | + if (jsonObject.get(ODATA_TYPE_KEY) != null) { |
| 35 | + /** #microsoft.graph.user or #microsoft.graph.callrecords.callrecord */ |
| 36 | + final String odataType = jsonObject.get(ODATA_TYPE_KEY).getAsString(); |
| 37 | + final int lastDotIndex = odataType.lastIndexOf("."); |
| 38 | + final String derivedType = (odataType.substring(0, lastDotIndex) + |
| 39 | + ".models." + |
| 40 | + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, |
| 41 | + odataType.substring(lastDotIndex + 1))) |
| 42 | + .replace("#", "com."); |
| 43 | + try { |
| 44 | + Class<?> derivedClass = Class.forName(derivedType); |
| 45 | + //Check that the derived class inherits from the given parent class |
| 46 | + if (parentClass == null || parentClass.isAssignableFrom(derivedClass)) { |
| 47 | + return derivedClass; |
| 48 | + } |
| 49 | + return null; |
| 50 | + } catch (ClassNotFoundException e) { |
| 51 | + logger.logDebug("Unable to find a corresponding class for derived type " + derivedType + ". Falling back to parent class."); |
| 52 | + //If we cannot determine the derived type to cast to, return null |
| 53 | + //This may happen if the API and the SDK are out of sync |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + //If there is no defined OData type, return null |
| 58 | + return null; |
| 59 | + } |
| 60 | +} |
0 commit comments