// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.fhirengine.resource;

import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.ResourceType;

import java.lang.reflect.InvocationTargetException;

/** Utilities for the HAPI FHIR resources. */
public class ResourceUtils {
  /** Returns the FHIR resource type. */
  public static <M extends Resource> ResourceType getResourceType(Class<M> clazz) {
    try {
      return clazz.getConstructor().newInstance().getResourceType();
    } catch (NoSuchMethodException e) {
      throw new IllegalArgumentException("Cannot resolve resource type for " + clazz.getName(), e);
    } catch (IllegalAccessException e) {
      throw new IllegalArgumentException("Cannot resolve resource type for " + clazz.getName(), e);
    } catch (InstantiationException e) {
      throw new IllegalArgumentException("Cannot resolve resource type for " + clazz.getName(), e);
    } catch (InvocationTargetException e) {
      throw new IllegalArgumentException("Cannot resolve resource type for " + clazz.getName(), e);
    }
  }
}

@@ -834,16 +834,158 @@
 Utils %7B%0A
+%0A  /** The HAPI Fhir package prefix for R4 resources. */%0A  public static final String R4_RESOURCE_PACKAGE_PREFIX = %22org.hl7.fhir.r4.model.%22;%0A%0A
   /** Re
@@ -1034,17 +1034,17 @@
 static %3C
-M
+R
  extends
@@ -1089,17 +1089,17 @@
 e(Class%3C
-M
+R
 %3E clazz)
@@ -1755,11 +1755,352 @@
    %7D%0A  %7D
+%0A%0A  /** Returns the %7B@link Class%7D object for the resource type. */%0A  public static %3CR extends Resource%3E Class%3CR%3E getResourceClass(String resourceType) %7B%0A    try %7B%0A      return (Class%3CR%3E) Class.forName(R4_RESOURCE_PACKAGE_PREFIX + resourceType);%0A    %7D catch (ClassNotFoundException e) %7B%0A      e.printStackTrace();%0A      return null;%0A    %7D%0A  %7D
 %0A%7D%0A

Create the capability to look up class objects for HAPI FHIR resources.