Coverage Report - org.apache.commons.clazz.reflect.extended.ReflectedMappedPropertyParseResults
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectedMappedPropertyParseResults
0%
0/148
0%
0/130
4.947
 
 1  
 /*
 2  
  * Copyright 2002-2004 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.clazz.reflect.extended;
 17  
 
 18  
 import java.lang.reflect.Method;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.commons.clazz.Clazz;
 22  
 import org.apache.commons.clazz.reflect.ReflectedClazz;
 23  
 import org.apache.commons.clazz.reflect.common.AccessorMethodParseResults;
 24  
 import org.apache.commons.clazz.reflect.common.ReflectedPropertyParseResults;
 25  
 
 26  
 /**
 27  
  * Holds parse results for individual accessor methods for a 
 28  
  * mapped property.
 29  
  * 
 30  
  * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
 31  
  * @version $Id: ReflectedMappedPropertyParseResults.java 155436 2005-02-26 13:17:48Z dirkv $
 32  
  */
 33  
 public class ReflectedMappedPropertyParseResults 
 34  
             extends ReflectedPropertyParseResults
 35  
 {
 36  
     private AccessorMethodParseResults getMethodParseResults;
 37  
     private AccessorMethodParseResults putMethodParseResults;
 38  
     private AccessorMethodParseResults removeMethodParseResults;
 39  
     private AccessorMethodParseResults keySetMethodParseResults;
 40  
 
 41  
     /**
 42  
      * Constructor for ReflectedMappedPropertyParseResults.
 43  
      */
 44  
     public ReflectedMappedPropertyParseResults(
 45  
             ReflectedClazz clazz, String propertyName) 
 46  
     {
 47  0
         super(clazz, propertyName);
 48  0
     }
 49  
     
 50  
     protected String getPropertyCategory() {
 51  0
         return "mapped";
 52  
     }
 53  
 
 54  
     /**
 55  
      * Returns <code>true</code> if the property is 
 56  
      * java.util.Map or implements java.util.Map.
 57  
      */
 58  
     protected boolean isMap() {
 59  0
         if (readMethodParseResults != null
 60  
             && isMap(readMethodParseResults.getType())) {
 61  0
             return true;
 62  
         }
 63  0
         if (writeMethodParseResults != null
 64  
             && isMap(writeMethodParseResults.getType())) {
 65  0
             return true;
 66  
         }
 67  0
         return false;
 68  
     }
 69  
         
 70  
     private boolean isMap(Class javaClass) {
 71  0
         return javaClass != null && Map.class.isAssignableFrom(javaClass);
 72  
     }
 73  
     
 74  
     public Class getKeyType() {
 75  0
         if (getMethodParseResults != null) {
 76  0
             return getMethodParseResults.getParameterType();
 77  
         }
 78  0
         if (putMethodParseResults != null) {
 79  0
             return putMethodParseResults.getParameterType();
 80  
         }
 81  0
         if (removeMethodParseResults != null) {
 82  0
             return removeMethodParseResults.getParameterType();
 83  
         }
 84  0
         return null;        
 85  
     }
 86  
 
 87  
     public Class getContentType() {
 88  0
         if (getMethodParseResults != null) {
 89  0
             return getMethodParseResults.getType();
 90  
         }
 91  0
         if (putMethodParseResults != null) {
 92  0
             return putMethodParseResults.getType();
 93  
         }
 94  0
         if (removeMethodParseResults != null) {
 95  0
             return removeMethodParseResults.getType();
 96  
         }
 97  0
         return null;
 98  
         
 99  
     }
 100  
 
 101  
     public void setGetMethodParseResults(
 102  
                 AccessorMethodParseResults getMethodParseResults) 
 103  
     {
 104  0
         checkForExtraneousAccessor(
 105  
             this.getMethodParseResults,
 106  
             getMethodParseResults);
 107  0
         this.getMethodParseResults = getMethodParseResults;
 108  0
     }
 109  
 
 110  
     public Method getGetMethod() {
 111  0
         if (getMethodParseResults == null) {
 112  0
             return null;
 113  
         }
 114  0
         return getMethodParseResults.getMethod();
 115  
     }
 116  
     
 117  
 
 118  
     public void setPutMethodParseResults(
 119  
                 AccessorMethodParseResults putMethodParseResults) 
 120  
     {
 121  0
         checkForExtraneousAccessor(
 122  
             this.putMethodParseResults,
 123  
             putMethodParseResults);
 124  0
         this.putMethodParseResults = putMethodParseResults;
 125  0
     }
 126  
     
 127  
     public Method getPutMethod() {
 128  0
         if (putMethodParseResults == null) {
 129  0
             return null;
 130  
         }
 131  0
         return putMethodParseResults.getMethod();
 132  
     }
 133  
         
 134  
     public void setRemoveMethodParseResults(
 135  
                 AccessorMethodParseResults removeMethodParseResults) 
 136  
     {
 137  0
         checkForExtraneousAccessor(
 138  
             this.removeMethodParseResults,
 139  
             removeMethodParseResults);
 140  0
         this.removeMethodParseResults = removeMethodParseResults;
 141  0
     }
 142  
     
 143  
     public Method getRemoveMethod() {
 144  0
         if (removeMethodParseResults == null) {
 145  0
             return null;
 146  
         }
 147  0
         return removeMethodParseResults.getMethod();
 148  
     }
 149  
     
 150  
     public void setKeySetMethodParseResults(
 151  
                 AccessorMethodParseResults keySetMethodParseResults) 
 152  
     {
 153  0
         checkForExtraneousAccessor(
 154  
             this.keySetMethodParseResults,
 155  
             keySetMethodParseResults);
 156  0
         this.keySetMethodParseResults = keySetMethodParseResults;
 157  0
     }
 158  
     
 159  
     public Method getKeySetMethod() {
 160  0
         if (keySetMethodParseResults == null) {
 161  0
             return null;
 162  
         }
 163  0
         return keySetMethodParseResults.getMethod();
 164  
     }
 165  
     
 166  
     /**
 167  
      * Combines parse results from another instance of 
 168  
      * <code>ReflectedMappedPropertyParseResults</code> with
 169  
      * results contained by this object.
 170  
      * <p>
 171  
      * Node that the property name is not copied from the 
 172  
      * <code>other</code> object.
 173  
      */        
 174  
     public void merge(ReflectedMappedPropertyParseResults other) {
 175  0
         super.merge(other);
 176  0
         if (other.readMethodParseResults != null) {
 177  0
             setReadMethodParseResults(other.readMethodParseResults);
 178  
         }
 179  0
         if (other.writeMethodParseResults != null) {
 180  0
             setWriteMethodParseResults(other.writeMethodParseResults);
 181  
         }
 182  0
         if (other.getMethodParseResults != null) {
 183  0
             setGetMethodParseResults(other.getMethodParseResults);
 184  
         }
 185  0
         if (other.putMethodParseResults != null) {
 186  0
             setPutMethodParseResults(other.putMethodParseResults);
 187  
         }
 188  0
         if (other.removeMethodParseResults != null) {
 189  0
             setRemoveMethodParseResults(other.removeMethodParseResults);
 190  
         }
 191  0
         if (other.keySetMethodParseResults != null) {
 192  0
             setKeySetMethodParseResults(other.keySetMethodParseResults);
 193  
         }
 194  0
     }
 195  
     
 196  
     protected void appendDescription(StringBuffer buffer) {
 197  0
         super.appendDescription(buffer);
 198  0
         Class keyType = getKeyType();
 199  0
         if (keyType != null) {
 200  0
             buffer.append("\n  [key type]       ");
 201  0
             buffer.append(Clazz.getCanonicalClassName(keyType));
 202  
         }
 203  
         
 204  0
         Class contentType = getContentType();
 205  0
         if (contentType != null) {
 206  0
             buffer.append("\n  [content type]   ");
 207  0
             buffer.append(Clazz.getCanonicalClassName(contentType));
 208  
         }
 209  0
     }
 210  
     
 211  
     protected void appendMethodDescriptions(StringBuffer buffer) {
 212  0
         super.appendMethodDescriptions(buffer);
 213  0
         if (getMethodParseResults != null) {
 214  0
             buffer.append("\n    [get~(key)]    ");
 215  0
             buffer.append(getMethodParseResults.getMethod());
 216  
         }
 217  0
         if (putMethodParseResults != null) {
 218  0
             buffer.append("\n    [set~(key,v)]  ");
 219  0
             buffer.append(putMethodParseResults.getMethod());
 220  
         }
 221  0
         if (removeMethodParseResults != null) {
 222  0
             buffer.append("\n    [remove~(key)] ");
 223  0
             buffer.append(removeMethodParseResults.getMethod());
 224  
         }
 225  0
         if (keySetMethodParseResults != null) {
 226  0
             buffer.append("\n    [get~KeySet()] ");
 227  0
             buffer.append(keySetMethodParseResults.getMethod());
 228  
         }
 229  0
     }
 230  
     
 231  
     public boolean checkConsistency() {        
 232  0
         if (!super.checkConsistency()) {
 233  0
             return false;
 234  
         }
 235  
                 
 236  0
         if (readMethodParseResults == null && getMethodParseResults == null) {
 237  0
             return false;
 238  
         }
 239  
 
 240  0
         if (readMethodParseResults != null) {
 241  0
             Class type = readMethodParseResults.getType();
 242  0
             if (writeMethodParseResults != null) {
 243  0
                 if (!type.equals(writeMethodParseResults.getType())) {
 244  0
                     return false;
 245  
                 }
 246  
             }
 247  
         }
 248  
                 
 249  0
         Class keyType = null;
 250  0
         Class contentType = null;
 251  
          
 252  0
         if (getMethodParseResults != null) {
 253  0
             keyType = getMethodParseResults.getParameterType();
 254  0
             contentType = getMethodParseResults.getType();
 255  
         }
 256  
         
 257  0
         if (putMethodParseResults != null) {
 258  0
             if (keyType == null) {
 259  0
                 keyType = putMethodParseResults.getParameterType();
 260  
             }
 261  
             else {
 262  0
                 if (!keyType.equals(putMethodParseResults.getParameterType())) {
 263  0
                     return false;
 264  
                 }
 265  
             }
 266  
             
 267  0
             if (contentType == null) {
 268  0
                 contentType = putMethodParseResults.getType();
 269  
             }
 270  
             else {
 271  0
                 if (!contentType.equals(
 272  
                         putMethodParseResults.getType())) {
 273  0
                     return false;
 274  
                 }
 275  
             }
 276  
         }
 277  
         
 278  0
         if (removeMethodParseResults != null) {
 279  0
             if (keyType != null
 280  
                 && !keyType.
 281  
                         equals(removeMethodParseResults.getParameterType())) {
 282  0
                 return false;
 283  
             }
 284  
                         
 285  0
             if (contentType != null) {
 286  0
                 Class removeType = removeMethodParseResults.getType(); 
 287  0
                 if (removeType != null && !contentType.equals(removeType)) {
 288  0
                     return false;
 289  
                 }
 290  
             }
 291  
         }
 292  
         
 293  0
         return true;
 294  
     }
 295  
         
 296  
     protected boolean appendInconsistencyDescriptions(StringBuffer buffer) {
 297  0
         if (!super.appendInconsistencyDescriptions(buffer)) {
 298  0
             return false;
 299  
         }        
 300  
         
 301  0
         if (readMethodParseResults == null && getMethodParseResults == null) {
 302  0
             buffer.append(
 303  
                 "\n     - Does not have either get() or get(key) method");
 304  0
             return true;
 305  
         }
 306  
 
 307  0
         if (readMethodParseResults != null) {
 308  0
             Class type = readMethodParseResults.getType();
 309  0
             if (writeMethodParseResults != null) {
 310  0
                 if (!type.equals(writeMethodParseResults.getType())) {
 311  0
                     buffer.append(
 312  
                         "\n     - Get() and set(v) types do not match");
 313  
                 }
 314  
             }
 315  
         }
 316  
         
 317  0
         Class keyType = null;
 318  0
         Class contentType = null;
 319  
          
 320  0
         if (getMethodParseResults != null) {
 321  0
             keyType = getMethodParseResults.getParameterType();
 322  0
             contentType = getMethodParseResults.getType();
 323  
         }
 324  
         
 325  0
         if (putMethodParseResults != null) {
 326  0
             if (keyType == null) {
 327  0
                 keyType = putMethodParseResults.getParameterType();
 328  
             }
 329  
             else {
 330  0
                 if (!keyType.equals(putMethodParseResults.getParameterType())) {
 331  0
                     buffer.append(
 332  
                         "\n     - Key type mismatch between "
 333  
                             + "get(key) and set(key,v)");
 334  
                 }
 335  
             }
 336  
             
 337  0
             if (contentType == null) {
 338  0
                 contentType = putMethodParseResults.getType();
 339  
             }
 340  
             else {
 341  0
                 if (!contentType.equals(
 342  
                         putMethodParseResults.getType())) {
 343  0
                     buffer.append(
 344  
                         "\n     - Content type mismatch between "
 345  
                             + "get(key) and set(key,v)");
 346  
                 }
 347  
             }
 348  
         }
 349  
         
 350  0
         if (removeMethodParseResults != null) {
 351  0
             if (keyType != null
 352  
                 && !keyType.
 353  
                         equals(removeMethodParseResults.getParameterType())) {
 354  0
                 buffer.append(
 355  
                     "\n     - Key type mismatch between "
 356  
                         + "get(key) and remove(key)");
 357  
             }
 358  
                         
 359  0
             if (contentType != null) {
 360  0
                 Class removeType = removeMethodParseResults.getType(); 
 361  0
                 if (removeType != null && !contentType.equals(removeType)) {
 362  0
                     buffer.append(
 363  
                         "\n     - Content type mismatch between "
 364  
                             + "get(key) and set(key,v)");
 365  
                 }
 366  
             }
 367  
         }        
 368  0
         return true;
 369  
     }  
 370  
 }