Coverage Report - org.apache.shindig.social.core.util.InjectorBeanInstanceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectorBeanInstanceStrategy
83%
5/6
50%
1/2
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util;
 19  
 
 20  
 import com.google.inject.Injector;
 21  
 
 22  
 import java.lang.reflect.InvocationTargetException;
 23  
 
 24  
 import net.sf.json.JSONObject;
 25  
 import net.sf.json.util.NewBeanInstanceStrategy;
 26  
 
 27  
 /**
 28  
  * An Injector based NewBeanInstance strategy that will use Guice to create new
 29  
  * beans.
 30  
  */
 31  
 public class InjectorBeanInstanceStrategy extends NewBeanInstanceStrategy {
 32  
   /**
 33  
    * The injector to use to create beans.
 34  
    */
 35  
   private Injector injector;
 36  
 
 37  
   /**
 38  
    * Constructor.
 39  
    *
 40  
    * @param injector
 41  
    *                The injector to use.
 42  
    */
 43  10
   public InjectorBeanInstanceStrategy(Injector injector) {
 44  10
     this.injector = injector;
 45  10
   }
 46  
 
 47  
   /**
 48  
    * create a new instance of the requested bean using Guice.
 49  
    *
 50  
    * @param beanClass
 51  
    *                The bean class to create and instance of
 52  
    * @param jsonObject
 53  
    *                the json object to base that instance on
 54  
    * @return a new instance of the request class
 55  
    * @throws InstantiationException when the object cant be created
 56  
    * @throws IllegalAccessException when permission is denied to the class
 57  
    * @throws NoSuchMethodException when the constructor or one of the injectors has gone missing
 58  
    * @throws InvocationTargetException when the invocation method fails to invoke
 59  
    */
 60  
   @SuppressWarnings("unchecked")
 61  
   @Override
 62  
   public Object newInstance(Class beanClass, JSONObject jsonObject) throws InstantiationException,
 63  
       IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 64  19
     if (beanClass != null) {
 65  19
       return injector.getInstance(beanClass);
 66  
     }
 67  0
     return DEFAULT.newInstance(null, jsonObject);
 68  
   }
 69  
 
 70  
 }