Coverage Report - org.simpleframework.xml.core.ParameterContact
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterContact
80%
17/21
100%
4/4
1.25
 
 1  
 /*
 2  
  * ParameterContact.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 java.lang.annotation.Annotation;
 22  
 import java.lang.reflect.Constructor;
 23  
 
 24  
 /**
 25  
  * The <code>ParameterContact</code> object is used to represent
 26  
  * a contact that is provided so that a <code>Label</code> can be
 27  
  * used to determine a consistent name for the parameter. Unlike
 28  
  * field and method contacts this is essentially an adapter that
 29  
  * is used so that the parameter name can be determined in a 
 30  
  * similar way to a method or field.
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  */
 34  
 abstract class ParameterContact<T extends Annotation> implements Contact {
 35  
 
 36  
    /**
 37  
     * This is used to hold the annotations for this parameter.
 38  
     */
 39  
    protected final Annotation[] labels;
 40  
 
 41  
    /**
 42  
     * This is the constructor the parameter was declared within. 
 43  
     */
 44  
    protected final Constructor factory;
 45  
    
 46  
    /**
 47  
     * This represents the class that this parameter was declared in.
 48  
     */
 49  
    protected final Class owner;
 50  
    
 51  
    /**
 52  
     * This is the index of the parameter within the constructor.
 53  
     */
 54  
    protected final int index;
 55  
    
 56  
    /**
 57  
     * This is the annotation used to label the parameter.
 58  
     */
 59  
    protected final T label;
 60  
 
 61  
    /**
 62  
     * Constructor for the <code>ParameterContact</code> object. This
 63  
     * is used to create a contact that can be used to determine a
 64  
     * consistent name for the parameter. It requires the annotation,
 65  
     * the constructor, and the parameter declaration index.
 66  
     * 
 67  
     * @param label this is the annotation used for the parameter
 68  
     * @param factory this is the constructor that is represented
 69  
     * @param index this is the index for the parameter
 70  
     */
 71  595
    public ParameterContact(T label, Constructor factory, int index) {
 72  595
       this.labels = factory.getParameterAnnotations()[index];
 73  595
       this.owner = factory.getDeclaringClass();
 74  595
       this.factory = factory;
 75  595
       this.index = index;
 76  595
       this.label = label;
 77  595
    }
 78  
    
 79  
    /**
 80  
     * This is the annotation associated with the point of contact.
 81  
     * This will be an XML annotation that describes how the contact
 82  
     * should be serialized and deserialized from the object.
 83  
     *
 84  
     * @return this provides the annotation associated with this
 85  
     */
 86  
    public Annotation getAnnotation() {
 87  2218
       return label;
 88  
    }
 89  
    
 90  
    /**
 91  
     * This will provide the contact type. The contact type is the
 92  
     * class that is to be set and get on the object. Typically the
 93  
     * type will be a serializable object or a primitive type.
 94  
     *
 95  
     * @return this returns the type that this contact represents
 96  
     */ 
 97  
    public Class getType() {
 98  691
       return factory.getParameterTypes()[index];
 99  
    }
 100  
 
 101  
    /**
 102  
     * This provides the dependent class for the contact. This will
 103  
     * typically represent a generic type for the actual type. For
 104  
     * contacts that use a <code>Collection</code> type this will
 105  
     * be the generic type parameter for that collection.
 106  
     * 
 107  
     * @return this returns the dependent type for the contact
 108  
     */
 109  
    public Class getDependent() {
 110  31
       return Reflector.getParameterDependent(factory, index);
 111  
    }
 112  
 
 113  
    /**
 114  
     * This provides the dependent classes for the contact. This will
 115  
     * typically represent a generic types for the actual type. For
 116  
     * contacts that use a <code>Map</code> type this will be the 
 117  
     * generic type parameter for that map type declaration.
 118  
     * 
 119  
     * @return this returns the dependent types for the contact
 120  
     */
 121  
    public Class[] getDependents() {
 122  6
       return Reflector.getParameterDependents(factory, index);
 123  
    }
 124  
    
 125  
    /**
 126  
     * This is the class that declares the contact. The declaring
 127  
     * class is where the parameter has been defined. Typically
 128  
     * this will not be a class rather than an interface.
 129  
     * 
 130  
     * @return the class this parameter is declared within
 131  
     */
 132  
    public Class getDeclaringClass() {
 133  0
       return owner;
 134  
    }
 135  
    
 136  
    /**
 137  
     * This is used to get the value from the specified object using
 138  
     * the point of contact. Typically the value is retrieved from
 139  
     * the specified object by invoking a get method of by acquiring
 140  
     * the value from a field within the specified object.
 141  
     *
 142  
     * @param source this is the object to acquire the value from
 143  
     *
 144  
     * @return this is the value acquired from the point of contact
 145  
     */ 
 146  
    public Object get(Object source) {
 147  0
       return null;
 148  
    }  
 149  
 
 150  
    /**
 151  
     * This is used to set the value on the specified object through
 152  
     * this contact. Depending on the type of contact this will set
 153  
     * the value given, typically this will be done by invoking a
 154  
     * method or setting the value on the object field.
 155  
     *
 156  
     * @param source this is the object to set the value on
 157  
     * @param value this is the value to be set through the contact
 158  
     */ 
 159  
    public void set(Object source, Object value) {  
 160  0
       return;
 161  
    }
 162  
 
 163  
    /**
 164  
     * This is the annotation associated with the point of contact.
 165  
     * This will be an XML annotation that describes how the contact
 166  
     * should be serialized and deserialized from the object.
 167  
     * 
 168  
     * @param type this is the type of the annotation to acquire
 169  
     *
 170  
     * @return this provides the annotation associated with this
 171  
     */
 172  
    public <A extends Annotation> A getAnnotation(Class<A> type) {
 173  4315
       for(Annotation label : labels) {
 174  2299
          Class expect = label.annotationType();
 175  
          
 176  2299
          if(expect.equals(type)) {
 177  95
             return (A)label;
 178  
          }
 179  
       }
 180  2016
       return null;
 181  
    }
 182  
    
 183  
    /**
 184  
     * This is used to determine if the annotated contact is for a
 185  
     * read only variable. A read only variable is a field that
 186  
     * can be set from within the constructor such as a blank final
 187  
     * variable. It can also be a method with no set counterpart.
 188  
     * 
 189  
     * @return this returns true if the contact is a constant one
 190  
     */
 191  
    public boolean isReadOnly() {
 192  0
       return false;
 193  
    }
 194  
    
 195  
    /**
 196  
     * This is used to provide a textual representation of the 
 197  
     * parameter. Providing a string describing the parameter is
 198  
     * useful for debugging and for exception messages.
 199  
     * 
 200  
     * @return this returns the string representation for this
 201  
     */
 202  
    public String toString() {
 203  556
       return String.format("parameter %s of constructor %s", index, factory);
 204  
    }
 205  
 
 206  
    /**
 207  
     * This represents the name of the parameter. Because the name
 208  
     * of the parameter does not exist at runtime the name must
 209  
     * be taken directly from the annotation and the parameter type.
 210  
     * Each XML annotation must provide their own unique way of
 211  
     * providing a name for the parameter contact.
 212  
     * 
 213  
     * @return this returns the name of the contact represented
 214  
     */
 215  
    public abstract String getName();
 216  
 }