Coverage Report - org.simpleframework.xml.load.FieldContact
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldContact
100%
19/19
100%
2/2
1.2
 
 1  
 /*
 2  
  * FieldContact.java April 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.annotation.Annotation;
 24  
 import java.lang.reflect.Field;
 25  
 
 26  
 /**
 27  
  * The <code>FieldContact</code> object is used to act as a contact
 28  
  * for a field within an object. This allows a value to be set on an
 29  
  * object field during deserialization and acquired from the same
 30  
  * field during serialization.
 31  
  *
 32  
  * @author Niall Gallagher
 33  
  *
 34  
  * @see org.simpleframework.xml.load.FieldScanner
 35  
  */ 
 36  
 class FieldContact implements Contact {
 37  
    
 38  
    /**
 39  
     * This is the label that marks the field within the object.
 40  
     */           
 41  
    private Annotation label;
 42  
    
 43  
    /**
 44  
     * This represents the field within the schema class object.
 45  
     */ 
 46  
    private Field field;
 47  
    
 48  
    /**
 49  
     * This is the name for this contact as taken from the field.
 50  
     */
 51  
    private String name;
 52  
    
 53  
    /**
 54  
     * Constructor for the <code>FieldContact</code> object. This is 
 55  
     * used as a point of contact for a field within a schema class.
 56  
     * Values can be read and written directly to the field with this.
 57  
     *
 58  
     * @param field this is the field that is the point of contact
 59  
     * @param label this is the annotation that is used by the field
 60  
     */ 
 61  428
    public FieldContact(Field field, Annotation label) {
 62  428
       this.label = label;
 63  428
       this.field = field;
 64  428
    }
 65  
 
 66  
    /**
 67  
     * This will provide the contact type. The contact type is the
 68  
     * class that is to be set and get on the object. This represents
 69  
     * the return type for the get and the parameter for the set.
 70  
     *
 71  
     * @return this returns the type that this contact represents
 72  
     */
 73  
    public Class getType() {
 74  407
       return field.getType();
 75  
    }
 76  
    
 77  
    /**
 78  
     * This provides the dependant class for the contact. This will
 79  
     * actually represent a generic type for the actual type. For
 80  
     * contacts that use a <code>Collection</code> type this will
 81  
     * be the generic type parameter for that collection.
 82  
     * 
 83  
     * @return this returns the dependant type for the contact
 84  
     */
 85  
    public Class getDependant() {
 86  32
       return Reflector.getDependant(field);
 87  
    }
 88  
    
 89  
    /**
 90  
     * This provides the dependant classes for the contact. This will
 91  
     * typically represent a generic types for the actual type. For
 92  
     * contacts that use a <code>Map</code> type this will be the 
 93  
     * generic type parameter for that map type declaration.
 94  
     * 
 95  
     * @return this returns the dependant type for the contact
 96  
     */
 97  
    public Class[] getDependants() {
 98  199
       return Reflector.getDependants(field);
 99  
    }
 100  
    
 101  
    /**
 102  
     * This is used to acquire the name of the field. This will return
 103  
     * the name of the field wich can then be used to determine the 
 104  
     * XML attribute or element the contact represents. This ensures
 105  
     * that the name provided string is internalized for performance.  
 106  
     * 
 107  
     *  @return this returns the name of the field represented
 108  
     */
 109  
    public String getName() {
 110  181
       if(name == null) {
 111  156
          name = getName(field);
 112  
       }
 113  181
       return name;
 114  
    }
 115  
    
 116  
    /**
 117  
     * This is used to acquire the name of the field such that it is
 118  
     * an internalized string. Internalization of the contact name
 119  
     * ensures that comparisons can be made to annotation names with
 120  
     * a simple reference comparison rather than a string comparison.
 121  
     * 
 122  
     * @param field the field to acquire the internalized name from
 123  
     * 
 124  
     * @return this returns the name of the string, internalized
 125  
     */
 126  
    private String getName(Field field) {
 127  156
       String name = field.getName();
 128  
       
 129  156
       if(name != null) {
 130  156
          name = name.intern();
 131  
       }
 132  156
       return name;
 133  
    }
 134  
 
 135  
    /**
 136  
     * This is the annotation associated with the point of contact.
 137  
     * This will be an XML annotation that describes how the contact
 138  
     * should be serializaed and deserialized from the object.
 139  
     *
 140  
     * @return this provides the annotation associated with this
 141  
     */
 142  
    public Annotation getAnnotation() {
 143  808
       return label;
 144  
    }
 145  
 
 146  
    /**
 147  
     * This is used to set the specified value on the provided object.
 148  
     * The value provided must be an instance of the contact class so
 149  
     * that it can be set without a runtime class compatibility error.
 150  
     *
 151  
     * @param source this is the object to set the value on
 152  
     * @param value this is the value that is to be set on the object
 153  
     */ 
 154  
    public void set(Object source, Object value) throws Exception {
 155  881615
       field.set(source, value);
 156  881615
    }
 157  
 
 158  
    /**
 159  
     * This is used to get the specified value on the provided object.
 160  
     * The value returned from this method will be an instance of the
 161  
     * contact class type. If the returned object is of a different
 162  
     * type then the serialization process will fail.
 163  
     *
 164  
     * @param source this is the object to acquire the value from
 165  
     *
 166  
     * @return this is the value that is acquired from the object
 167  
     */
 168  
    public Object get(Object source) throws Exception {
 169  302364
       return field.get(source);
 170  
    }
 171  
    
 172  
    /**
 173  
     * This is used to describe the contact as it exists within the
 174  
     * owning class. It is used to provide error messages that can
 175  
     * be used to debug issues that occur when processing a contact.
 176  
     * The string provided is the generic field string.
 177  
     * 
 178  
     * @return this returns a string representation of the contact
 179  
     */
 180  
    public String toString() {
 181  49
       return String.format("field '%s'", getName());
 182  
    }
 183  
 }