View Javadoc
1   
2   package ca.uhn.fhir.jpa.util;
3   
4   /*-
5    * #%L
6    * HAPI FHIR JPA Server
7    * %%
8    * Copyright (C) 2014 - 2018 University Health Network
9    * %%
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   * #L%
22   */
23  
24  import org.springframework.aop.framework.Advised;
25  import org.springframework.aop.support.AopUtils;
26  
27  /**
28   * Utility to get the Spring proxy object's target object
29   */
30  public class SpringObjectCaster {
31  
32      /**
33       * Retrieve the Spring proxy object's target object
34       * @param proxy
35       * @param clazz
36       * @param <T>
37       * @return
38       * @throws Exception
39       */
40      public static <T> T getTargetObject(Object proxy, Class<T> clazz) throws Exception {
41          while( (AopUtils.isJdkDynamicProxy(proxy))) {
42              return clazz.cast(getTargetObject(((Advised)proxy).getTargetSource().getTarget(), clazz));
43          }
44  
45          return clazz.cast(proxy);
46      }
47  }