| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GetPart |
|
| 1.1111111111111112;1.111 |
| 1 | /* | |
| 2 | * GetPart.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.Method; | |
| 25 | ||
| 26 | /** | |
| 27 | * The <code>GetPart</code> object represents the getter method for | |
| 28 | * a Java Bean property. This composes the get part of the method | |
| 29 | * contact for an object. The get part contains the method that is | |
| 30 | * used to get the value in an object and the annotation that tells | |
| 31 | * the serialization process how to serialize the value. | |
| 32 | * | |
| 33 | * @author Niall Gallagher | |
| 34 | * | |
| 35 | * @see org.simpleframework.xml.load.MethodContact | |
| 36 | */ | |
| 37 | class GetPart implements MethodPart { | |
| 38 | ||
| 39 | /** | |
| 40 | * This is the annotation for the get method provided. | |
| 41 | */ | |
| 42 | private final Annotation label; | |
| 43 | ||
| 44 | /** | |
| 45 | * This represents the method type for the get part method. | |
| 46 | */ | |
| 47 | private final MethodType type; | |
| 48 | ||
| 49 | /** | |
| 50 | * This method is used to get the value during serialization. | |
| 51 | */ | |
| 52 | private final Method method; | |
| 53 | ||
| 54 | /** | |
| 55 | * This is the Java Bean name representation of the method. | |
| 56 | */ | |
| 57 | private final String name; | |
| 58 | ||
| 59 | /** | |
| 60 | * Constructor for the <code>GetPart</code> object. This is | |
| 61 | * used to create a method part that will provide a means for | |
| 62 | * the serialization process to set a value to a object. | |
| 63 | * | |
| 64 | * @param method the method that is used to get the value | |
| 65 | * @param label this describes how to serialize the value | |
| 66 | */ | |
| 67 | 58 | public GetPart(MethodName method, Annotation label) { |
| 68 | 58 | this.method = method.getMethod(); |
| 69 | 58 | this.name = method.getName(); |
| 70 | 58 | this.type = method.getType(); |
| 71 | 58 | this.label = label; |
| 72 | 58 | } |
| 73 | ||
| 74 | /** | |
| 75 | * This provdes the name of the method part as acquired from the | |
| 76 | * method name. The name represents the Java Bean property name | |
| 77 | * of the method and is used to pair getter and setter methods. | |
| 78 | * | |
| 79 | * @return this returns the Java Bean name of the method part | |
| 80 | */ | |
| 81 | public String getName() { | |
| 82 | 152 | return name; |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * This is used to acquire the type for this method part. This | |
| 87 | * is used by the serializer to determine the schema class that | |
| 88 | * is used to match the XML elements to the object details. | |
| 89 | * | |
| 90 | * @return this returns the schema class for this method | |
| 91 | */ | |
| 92 | public Class getType() { | |
| 93 | 93 | return method.getReturnType(); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * This is used to acquire the dependant class for the method | |
| 98 | * part. The dependant type is the type that represents the | |
| 99 | * generic type of the type. This is used when collections are | |
| 100 | * annotated as it allows a default entry class to be taken | |
| 101 | * from the generic information provided. | |
| 102 | * | |
| 103 | * @return this returns the generic dependant for the type | |
| 104 | */ | |
| 105 | public Class getDependant() { | |
| 106 | 46 | return Reflector.getReturnDependant(method); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * This is used to acquire the dependant classes for the method | |
| 111 | * part. The dependant types are the types that represent the | |
| 112 | * generic types of the type. This is used when collections are | |
| 113 | * annotated as it allows a default entry class to be taken | |
| 114 | * from the generic information provided. | |
| 115 | * | |
| 116 | * @return this returns the generic dependant for the type | |
| 117 | */ | |
| 118 | public Class[] getDependants() { | |
| 119 | 46 | return Reflector.getReturnDependants(method); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * This is used to acquire the annotation that was used to label | |
| 124 | * the method this represents. This acts as a means to match the | |
| 125 | * set method with the get method using an annotation comparison. | |
| 126 | * | |
| 127 | * @return this returns the annotation used to mark the method | |
| 128 | */ | |
| 129 | public Annotation getAnnotation() { | |
| 130 | 94 | return label; |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * This is the method type for the method part. This is used in | |
| 135 | * the scanning process to determine which type of method a | |
| 136 | * instance represents, this allows set and get methods to be | |
| 137 | * paired. | |
| 138 | * | |
| 139 | * @return the method type that this part represents | |
| 140 | */ | |
| 141 | public MethodType getMethodType() { | |
| 142 | 58 | return type; |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * This is used to acquire the method that can be used to invoke | |
| 147 | * the Java Bean method on the object. If the method represented | |
| 148 | * by this is inaccessible then this will set it as accessible. | |
| 149 | * | |
| 150 | * @return returns the method used to interace with the object | |
| 151 | */ | |
| 152 | public Method getMethod() { | |
| 153 | 94 | if(!method.isAccessible()) { |
| 154 | 48 | method.setAccessible(true); |
| 155 | } | |
| 156 | 94 | return method; |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * This is used to describe the method as it exists within the | |
| 161 | * owning class. This is used to provide error messages that can | |
| 162 | * be used to debug issues that occur when processing a method. | |
| 163 | * This returns the method as a generic string representation. | |
| 164 | * | |
| 165 | * @return this returns a string representation of the method | |
| 166 | */ | |
| 167 | public String toString() { | |
| 168 | 0 | return method.toGenericString(); |
| 169 | } | |
| 170 | } |