Coverage Report - org.simpleframework.xml.load.ClassType
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassType
100%
16/16
100%
2/2
1.333
 
 1  
 /*
 2  
  * ClassType.java January 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  
  * GNU Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General 
 16  
  * Public License along with this library; if not, write to the 
 17  
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 18  
  * Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package org.simpleframework.xml.load;
 22  
 
 23  
 import java.lang.reflect.Constructor;
 24  
 
 25  
 /**
 26  
  * The <code>ClassType</code> is an implementation of the type that
 27  
  * is used to instantiate an object using its default no argument
 28  
  * constructor. This will simply ensure that the constructor is an
 29  
  * accessible method before invoking the types new instance method.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  * 
 33  
  * @see org.simpleframework.xml.load.DefaultStrategy
 34  
  */
 35  
 class ClassType implements Type {
 36  
    
 37  
    /**
 38  
     * Caches the constructors used to convert composite types.
 39  
     * 
 40  
     * @see org.simpleframework.xml.load.Composite
 41  
     */
 42  
    private static ConstructorCache cache;
 43  
 
 44  
    static {
 45  45
       cache = new ConstructorCache();           
 46  45
    }
 47  
    
 48  
    /**
 49  
     * This is the type that this object is used to represent.
 50  
     */
 51  
    private Class type;
 52  
 
 53  
    /**
 54  
     * Constructor for the <code>ClassType</code> object. This is
 55  
     * used to create a type object that can be used to instantiate
 56  
     * and object with that objects default no argument constructor.
 57  
     * 
 58  
     * @param type this is the type of object that is created
 59  
     */
 60  752827
    public ClassType(Class type) {
 61  752827
       this.type = type;
 62  752827
    }        
 63  
    
 64  
    /**
 65  
     * This method is used to acquire an instance of the type that
 66  
     * is defined by this object. If for some reason the type can
 67  
     * not be instantiated an exception is thrown from this.
 68  
     * 
 69  
     * @return an instance of the type this object represents
 70  
     */
 71  
    public Object getInstance() throws Exception {
 72  300752
       return getInstance(type);
 73  
    }
 74  
    
 75  
    /**
 76  
     * This method will instantiate an object of the provided type. If
 77  
     * the object or constructor does not have public access then this
 78  
     * will ensure the constructor is accessible and can be used.
 79  
     * 
 80  
     * @param type this is used to ensure the object is accessible
 81  
     *
 82  
     * @return this returns an instance of the specifiec class type
 83  
     */ 
 84  
    public Object getInstance(Class type) throws Exception {
 85  330787
       Constructor method = cache.fetch(type);
 86  
       
 87  330787
       if(method == null) {
 88  155
          method = type.getDeclaredConstructor();      
 89  
 
 90  155
          if(!method.isAccessible()) {
 91  155
             method.setAccessible(true);              
 92  
          }
 93  155
          cache.cache(type, method);
 94  
       }
 95  330787
       return method.newInstance();   
 96  
    }
 97  
    
 98  
    /**
 99  
     * This method is used acquire the value from the type and if
 100  
     * possible replace the value for the type. If the value can
 101  
     * not be replaced then an exception should be thrown. This 
 102  
     * is used to allow primitives to be inserted into a graph.
 103  
     * 
 104  
     * @param value this is the value to insert as the type
 105  
     * 
 106  
     * @return an instance of the type this object represents
 107  
     */
 108  
    public Object getInstance(Object value) throws Exception {
 109  422017
       return value;
 110  
    }
 111  
    
 112  
    /**
 113  
     * This is the type of the object instance that will be created
 114  
     * by the <code>getInstance</code> method. This allows the 
 115  
     * deserialization process to perform checks against the field.
 116  
     * 
 117  
     * @return the type of the object that will be instantiated
 118  
     */
 119  
    public Class getType() {
 120  60876
       return type;
 121  
    }
 122  
 
 123  
    /**
 124  
     * This method always returns false for the default type. This
 125  
     * is because by default all elements encountered within the 
 126  
     * XML are to be deserialized based on there XML annotations.
 127  
     * 
 128  
     * @return this returns false for each type encountered     
 129  
     */
 130  
    public boolean isReference() {
 131  752823
       return false;
 132  
    }
 133  
 }