+ /**
+ * Utility method for checking whether a target object is compatible with a particular identity class.
+ *
+ * @param requiredClass Required class
+ * @param fieldName name of the field being filled
+ * @param obj Object to check, may be null
+ * @return Object cast to required class, if it class matches requirement, or null
+ * @throws IllegalArgumentException if {@code obj} is not an Class representing {@code requiredClass}
+ * @throws NullPointerException if {@code requiredClass} or {@code fieldName} is null
+ */
+ public static <T extends BaseIdentity> @Nullable Class<? extends T> checkFieldCastIdentity(
+ final @NonNull Class<T> requiredClass, final @NonNull String fieldName, final @Nullable Object obj) {
+ if (obj == null) {
+ return null;
+ }
+ checkArgument(obj instanceof Class, "Invalid input value \"%s\" for property \"%s\"", obj, fieldName);
+
+ try {
+ return ((Class<?>) obj).asSubclass(requiredClass);
+ } catch (ClassCastException e) {
+ throw new IllegalArgumentException("Invalid input value \"" + obj + "\" for property \"" + fieldName + "\"",
+ e);
+ }
+ }
+