Coverage Report - org.kuali.rice.kcb.util.BeanFactoryInvocationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanFactoryInvocationHandler
0%
0/15
0%
0/10
5
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kcb.util;
 17  
 
 18  
 import java.lang.reflect.InvocationHandler;
 19  
 import java.lang.reflect.Method;
 20  
 
 21  
 import org.springframework.beans.factory.BeanFactory;
 22  
 
 23  
 /**
 24  
  * Maps accessor invocations to beans in a BeanFactory. 
 25  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 26  
  *
 27  
  */
 28  
 public class BeanFactoryInvocationHandler implements InvocationHandler {
 29  
      private static final String PREFIX = "get";
 30  
 
 31  
      private BeanFactory beanFactory;
 32  
      
 33  0
      public BeanFactoryInvocationHandler(BeanFactory beanFactory) {
 34  0
          this.beanFactory = beanFactory;
 35  0
      }
 36  
 
 37  
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 38  0
          if (args != null && args.length > 0) {
 39  0
              throw new RuntimeException("BeanFactoryInvocationHandler only serves getters");
 40  
          }
 41  0
          String methodName = method.getName();
 42  0
          if (!methodName.startsWith(PREFIX)) {
 43  0
              throw new RuntimeException("BeanFactoryInvocationHandler only serves getters");
 44  
          }
 45  0
          String beanName = methodName.substring(PREFIX.length());
 46  0
          if (beanName.length() == 0) {
 47  0
              throw new RuntimeException("Illegal accessor, no bean name specified: " + methodName);
 48  
          }
 49  0
          char firstChar = beanName.charAt(0);
 50  0
          if (!Character.isLowerCase(firstChar)) {
 51  0
              beanName = Character.toLowerCase(firstChar) + beanName.substring(1);
 52  
          }
 53  0
          return beanFactory.getBean(beanName);
 54  
      }
 55  
 }