Coverage Report - org.simpleframework.xml.core.SetPart
 
Classes in this File Line Coverage Branch Coverage Complexity
SetPart
83%
20/24
100%
6/6
1.273
 
 1  
 /*
 2  
  * SetPart.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.Method;
 23  
 
 24  
 import org.simpleframework.xml.util.Cache;
 25  
 import org.simpleframework.xml.util.ConcurrentCache;
 26  
 
 27  
 /**
 28  
  * The <code>SetPart</code> object represents the setter method for
 29  
  * a Java Bean property. This composes the set part of the method
 30  
  * contact for an object. The set part contains the method that is
 31  
  * used to set the value on an object and the annotation that tells
 32  
  * the deserialization process how to deserialize the value.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.core.MethodContact
 37  
  */
 38  
 class SetPart implements MethodPart {
 39  
    
 40  
    /**
 41  
     * This cache contains the annotations present on the method.
 42  
     */
 43  
    private final Cache<Annotation> cache; 
 44  
    
 45  
    /**
 46  
     * This is the list of annotations associated with the method.
 47  
     */
 48  
    private final Annotation[] list;
 49  
    
 50  
    /**
 51  
     * This is the annotation for the set method provided.
 52  
     */
 53  
    private final Annotation label;
 54  
    
 55  
    /**
 56  
     * This represents the method type for the set part method.
 57  
     */
 58  
    private final MethodType type;
 59  
    
 60  
    /**
 61  
     * This method is used to set the value during deserialization. 
 62  
     */
 63  
    private final Method method;
 64  
    
 65  
    /**
 66  
     * This represents the name of this set part instance.
 67  
     */
 68  
    private final String name;
 69  
    
 70  
    /**
 71  
     * Constructor for the <code>SetPart</code> object. This is
 72  
     * used to create a method part that will provide a means for 
 73  
     * the deserialization process to set a value to a object.
 74  
     * 
 75  
     * @param method the method that is used to set the value
 76  
     * @param label this describes how to deserialize the value
 77  
     * @param list this is the list of annotations on the method
 78  
     */
 79  256
    public SetPart(MethodName method, Annotation label, Annotation[] list) {
 80  256
       this.cache = new ConcurrentCache<Annotation>();
 81  256
       this.method = method.getMethod();
 82  256
       this.name = method.getName();
 83  256
       this.type = method.getType();
 84  256
       this.label = label;
 85  256
       this.list = list;
 86  256
    }
 87  
    
 88  
    /**
 89  
     * This provides the name of the method part as acquired from the
 90  
     * method name. The name represents the Java Bean property name
 91  
     * of the method and is used to pair getter and setter methods.
 92  
     * 
 93  
     * @return this returns the Java Bean name of the method part
 94  
     */
 95  
    public String getName() {
 96  346
       return name;
 97  
    }
 98  
    
 99  
    /**
 100  
     * This is used to acquire the type for this method part. This
 101  
     * is used by the serializer to determine the schema class that
 102  
     * is used to match the XML elements to the object details.
 103  
     * 
 104  
     * @return this returns the schema class for this method
 105  
     */
 106  
    public Class getType() {
 107  284
       return method.getParameterTypes()[0];
 108  
    }
 109  
    
 110  
    /**
 111  
     * This is used to acquire the dependent class for the method 
 112  
     * part. The dependent type is the type that represents the 
 113  
     * generic type of the type. This is used when collections are
 114  
     * annotated as it allows a default entry class to be taken
 115  
     * from the generic information provided.
 116  
     * 
 117  
     * @return this returns the generic dependent for the type
 118  
     */  
 119  
    public Class getDependent() {
 120  0
       return Reflector.getParameterDependent(method, 0);
 121  
    }
 122  
    
 123  
    /**
 124  
     * This is used to acquire the dependent classes for the method 
 125  
     * part. The dependent types are the types that represents the 
 126  
     * generic types of the type. This is used when collections are
 127  
     * annotated as it allows a default entry classes to be taken
 128  
     * from the generic information provided.
 129  
     * 
 130  
     * @return this returns the generic dependents for the type
 131  
     */  
 132  
    public Class[] getDependents() {
 133  0
       return Reflector.getParameterDependents(method, 0);
 134  
    }
 135  
    
 136  
    /**
 137  
     * This is the class that declares the contact. The declaring
 138  
     * class is where the method represented has been defined. This
 139  
     * will typically be a class rather than an interface.
 140  
     * 
 141  
     * @return this returns the class the part is declared within
 142  
     */
 143  
    public Class getDeclaringClass() {
 144  0
       return method.getDeclaringClass();
 145  
    }
 146  
    
 147  
    /**
 148  
     * This is used to acquire the annotation that was used to label
 149  
     * the method this represents. This acts as a means to match the
 150  
     * set method with the get method using an annotation comparison.
 151  
     * 
 152  
     * @return this returns the annotation used to mark the method
 153  
     */
 154  
    public Annotation getAnnotation() {
 155  290
       return label;
 156  
    }
 157  
    
 158  
    /**
 159  
     * This is the annotation associated with the point of contact.
 160  
     * This will be an XML annotation that describes how the contact
 161  
     * should be serialized and deserialized from the object.
 162  
     * 
 163  
     * @param type this is the type of the annotation to acquire
 164  
     *
 165  
     * @return this provides the annotation associated with this
 166  
     */
 167  
    public <T extends Annotation> T getAnnotation(Class<T> type) {
 168  486
       if(cache.isEmpty()) {
 169  321
          for(Annotation entry : list) {
 170  137
             Class key = entry.annotationType();
 171  137
             cache.cache(key, entry);
 172  
          }
 173  
       }
 174  486
       return (T)cache.fetch(type);
 175  
    }
 176  
   
 177  
    /**
 178  
     * This is the method type for the method part. This is used in
 179  
     * the scanning process to determine which type of method a
 180  
     * instance represents, this allows set and get methods to be
 181  
     * paired.
 182  
     * 
 183  
     * @return the method type that this part represents
 184  
     */
 185  
    public MethodType getMethodType() {
 186  252
       return type;
 187  
    }
 188  
    
 189  
    /**
 190  
     * This is used to acquire the method that can be used to invoke
 191  
     * the Java Bean method on the object. If the method represented
 192  
     * by this is inaccessible then this will set it as accessible.
 193  
     * 
 194  
     * @return returns the method used to interface with the object
 195  
     */
 196  
    public Method getMethod() {
 197  176
       if(!method.isAccessible()) {
 198  127
          method.setAccessible(true);              
 199  
       }           
 200  176
       return method;
 201  
    }
 202  
    
 203  
    /**
 204  
     * This is used to describe the method as it exists within the
 205  
     * owning class. This is used to provide error messages that can
 206  
     * be used to debug issues that occur when processing a method.
 207  
     * This returns the method as a generic string representation.  
 208  
     * 
 209  
     * @return this returns a string representation of the method
 210  
     */
 211  
    public String toString() {
 212  0
       return method.toGenericString();
 213  
    }
 214  
 }