Coverage Report - org.simpleframework.xml.core.ObjectFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectFactory
88%
8/9
75%
3/4
3
 
 1  
 /*
 2  
  * ObjectFactory.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import org.simpleframework.xml.strategy.Type;
 22  
 import org.simpleframework.xml.strategy.Value;
 23  
 import org.simpleframework.xml.stream.InputNode;
 24  
 
 25  
 /**
 26  
  * The <code>ObjectFactory</code> is the most basic factory. This will
 27  
  * basically check to see if there is an override type within the XML
 28  
  * node provided, if there is then that is instantiated, otherwise the
 29  
  * field type is instantiated. Any type created must have a default
 30  
  * no argument constructor. If the override type is an abstract class
 31  
  * or an interface then this factory throws an exception.
 32  
  *  
 33  
  * @author Niall Gallagher
 34  
  */ 
 35  
 class ObjectFactory extends PrimitiveFactory {
 36  
    
 37  
    /**
 38  
     * Constructor for the <code>ObjectFactory</code> class. This is
 39  
     * given the field class that this should create object instances
 40  
     * of. If the field type is abstract then the XML node must have
 41  
     * sufficient information for the <code>Strategy</code> object to
 42  
     * resolve the implementation class to be instantiated.
 43  
     *
 44  
     * @param context the contextual object used by the persister 
 45  
     * @param type this is the object type to use for this factory 
 46  
     */
 47  
    public ObjectFactory(Context context, Type type, Class override) {
 48  935502
       super(context, type, override);
 49  935502
    }        
 50  
 
 51  
    /**
 52  
     * This method will instantiate an object of the field type, or if
 53  
     * the <code>Strategy</code> object can resolve a class from the
 54  
     * XML element then this is used instead. If the resulting type is
 55  
     * abstract or an interface then this method throws an exception.
 56  
     * 
 57  
     * @param node this is the node to check for the override
 58  
     * 
 59  
     * @return this returns an instance of the resulting type
 60  
     */       
 61  
    @Override
 62  
    public Instance getInstance(InputNode node) throws Exception {
 63  718094
       Value value = getOverride(node);
 64  718094
       Class expect = getType();
 65  
     
 66  718094
       if(value == null) { 
 67  705903
          if(!isInstantiable(expect)) {
 68  0
             throw new InstantiationException("Cannot instantiate %s for %s", expect, type);              
 69  
          }
 70  705903
          return context.getInstance(expect);         
 71  
       }
 72  12191
       return new ObjectInstance(context, value);      
 73  
    }     
 74  
 }