Coverage Report - org.simpleframework.xml.core.ObjectInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectInstance
100%
19/19
66%
4/6
1.667
 
 1  
 /*
 2  
  * ObjectInstance.java April 2007
 3  
  *
 4  
  * Copyright (C) 2007, 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.Value;
 22  
 
 23  
 /**
 24  
  * The <code>ObjectInstance</code> is used to instantiate an object
 25  
  * from the criteria provided in the given <code>Value</code>. If
 26  
  * the value contains a reference then the reference is provided
 27  
  * from this type. For performance the <code>Context</code> object
 28  
  * is used to instantiate the object as it contains a reflection
 29  
  * cache of constructors.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  */
 33  
 class ObjectInstance implements Instance {
 34  
    
 35  
    /**
 36  
     * This is the context that is used to create the instance.
 37  
     */
 38  
    private final Context context;
 39  
    
 40  
    /**
 41  
     * This is the value object that will be wrapped by this.
 42  
     */
 43  
    private final Value value;
 44  
    
 45  
    /**
 46  
     * This is the new class that is used for the instantiation.
 47  
     */
 48  
    private final Class type;
 49  
    
 50  
    /**
 51  
     * Constructor for the <code>ObjectInstance</code> object. This
 52  
     * is used to create an instance of the type described by the
 53  
     * value object. If the value object contains a reference then
 54  
     * this will simply provide that reference.
 55  
     * 
 56  
     * @param context this is used to instantiate the object
 57  
     * @param value this is the value describing the instance
 58  
     */
 59  25642
    public ObjectInstance(Context context, Value value) {
 60  25642
       this.type = value.getType();
 61  25642
       this.context = context;
 62  25642
       this.value = value;
 63  25642
    }
 64  
    
 65  
    /**
 66  
     * This method is used to acquire an instance of the type that
 67  
     * is defined by this object. If for some reason the type can
 68  
     * not be instantiated an exception is thrown from this.
 69  
     * 
 70  
     * @return an instance of the type this object represents
 71  
     */
 72  
    public Object getInstance() throws Exception {
 73  12888
       if(value.isReference()) {
 74  4176
          return value.getValue();
 75  
       }
 76  8712
       Object object = getInstance(type);
 77  
       
 78  8712
       if(value != null) {
 79  8712
          value.setValue(object);
 80  
       }
 81  8712
       return object;
 82  
    }
 83  
    
 84  
    /**
 85  
     * This method is used to acquire an instance of the type that
 86  
     * is defined by this object. If for some reason the type can
 87  
     * not be instantiated an exception is thrown from this.
 88  
     * 
 89  
     * @param type this is the type that is to be instantiated
 90  
     * 
 91  
     * @return an instance of the type this object represents
 92  
     */
 93  
    public Object getInstance(Class type) throws Exception {
 94  8712
       Instance value = context.getInstance(type);
 95  8712
       Object object = value.getInstance();
 96  
       
 97  8712
       return object;  
 98  
    }
 99  
    
 100  
    /**
 101  
     * This method is used acquire the value from the type and if
 102  
     * possible replace the value for the type. If the value can
 103  
     * not be replaced then an exception should be thrown. This 
 104  
     * is used to allow primitives to be inserted into a graph.
 105  
     * 
 106  
     * @param object this is the object to insert as the value
 107  
     * 
 108  
     * @return an instance of the type this object represents
 109  
     */
 110  
    public Object setInstance(Object object) {
 111  30329
       if(value != null) {
 112  30329
          value.setValue(object);
 113  
       }
 114  30329
       return object;
 115  
    }
 116  
 
 117  
    /**
 118  
     * This is used to determine if the type is a reference type.
 119  
     * A reference type is a type that does not require any XML
 120  
     * deserialization based on its annotations. Types that are
 121  
     * references could be substitutes objects are existing ones. 
 122  
     * 
 123  
     * @return this returns true if the object is a reference
 124  
     */
 125  
    public boolean isReference() {
 126  25642
       return value.isReference();
 127  
    }
 128  
 
 129  
    /**
 130  
     * This is the type of the object instance that will be created
 131  
     * by the <code>getInstance</code> method. This allows the 
 132  
     * deserialization process to perform checks against the field.
 133  
     * 
 134  
     * @return the type of the object that will be instantiated
 135  
     */
 136  
    public Class getType() {
 137  12222
       return type;
 138  
    } 
 139  
 }