| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Instance |
|
| 1.1666666666666667;1.167 |
| 1 | /* | |
| 2 | * Instance.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.graph; | |
| 22 | ||
| 23 | import org.simpleframework.xml.load.Type; | |
| 24 | import java.lang.reflect.Constructor; | |
| 25 | ||
| 26 | /** | |
| 27 | * The <code>Instance</code> is an implementation of the type that | |
| 28 | * is used to instantiate an object using its default no argument | |
| 29 | * constructor. This will simply ensure that the constructor is an | |
| 30 | * accessible method before invoking the types new instance method. | |
| 31 | * | |
| 32 | * @author Niall Gallagher | |
| 33 | * | |
| 34 | * @see org.simpleframework.xml.graph.Allocate | |
| 35 | */ | |
| 36 | class Instance implements Type { | |
| 37 | ||
| 38 | /** | |
| 39 | * This is the type that this object is used to represent. | |
| 40 | */ | |
| 41 | private Class type; | |
| 42 | ||
| 43 | /** | |
| 44 | * Constructor for the <code>Instance</code> object. This is | |
| 45 | * used to create a type object that can be used to instantiate | |
| 46 | * and object with that objects default no argument constructor. | |
| 47 | * | |
| 48 | * @param type this is the type of object that is created | |
| 49 | */ | |
| 50 | 9292 | public Instance(Class type) { |
| 51 | 9292 | this.type = type; |
| 52 | 9292 | } |
| 53 | ||
| 54 | /** | |
| 55 | * This method is used to acquire an instance of the type that | |
| 56 | * is defined by this object. If for some reason the type can | |
| 57 | * not be instantiated an exception is thrown from this. | |
| 58 | * | |
| 59 | * @return an instance of the type this object represents | |
| 60 | */ | |
| 61 | public Object getInstance() throws Exception { | |
| 62 | 5086 | return getInstance(type); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * This method will instantiate an object of the provided type. If | |
| 67 | * the object or constructor does not have public access then this | |
| 68 | * will ensure the constructor is accessible and can be used. | |
| 69 | * | |
| 70 | * @param type this is used to ensure the object is accessible | |
| 71 | * | |
| 72 | * @return this returns an instance of the specifiec class type | |
| 73 | */ | |
| 74 | public Object getInstance(Class type) throws Exception { | |
| 75 | 6098 | Constructor method = type.getDeclaredConstructor(); |
| 76 | ||
| 77 | 6098 | if(!method.isAccessible()) { |
| 78 | 6098 | method.setAccessible(true); |
| 79 | } | |
| 80 | 6098 | return method.newInstance(); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * This method is used acquire the value from the type and if | |
| 85 | * possible replace the value for the type. If the value can | |
| 86 | * not be replaced then an exception should be thrown. This | |
| 87 | * is used to allow primitives to be inserted into a graph. | |
| 88 | * | |
| 89 | * @param value this is the value to insert as the type | |
| 90 | * | |
| 91 | * @return an instance of the type this object represents | |
| 92 | */ | |
| 93 | public Object getInstance(Object value) throws Exception { | |
| 94 | 3194 | return value; |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * This is the type of the object instance that will be created | |
| 99 | * by the <code>getInstance</code> method. This allows the | |
| 100 | * deserialization process to perform checks against the field. | |
| 101 | * | |
| 102 | * @return the type of the object that will be instantiated | |
| 103 | */ | |
| 104 | public Class getType() { | |
| 105 | 10383 | return type; |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * This method always returns false for the default type. This | |
| 110 | * is because by default all elements encountered within the | |
| 111 | * XML are to be deserialized based on there XML annotations. | |
| 112 | * | |
| 113 | * @return this returns false for each type encountered | |
| 114 | */ | |
| 115 | public boolean isReference() { | |
| 116 | 118 | return false; |
| 117 | } | |
| 118 | } |