Coverage Report - org.apache.commons.clazz.reflect.extended.ExtendedReflectedListPropertyIntrospector
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtendedReflectedListPropertyIntrospector
0%
0/72
0%
0/32
1.72
ExtendedReflectedListPropertyIntrospector$AddAccessorMethodParser
0%
0/4
N/A
1.72
ExtendedReflectedListPropertyIntrospector$AddIndexedAccessorMethodParser
0%
0/7
0%
0/2
1.72
ExtendedReflectedListPropertyIntrospector$RemoveAccessorMethodParser
0%
0/5
0%
0/2
1.72
ExtendedReflectedListPropertyIntrospector$RemoveIndexedAccessorMethodParser
0%
0/4
N/A
1.72
ExtendedReflectedListPropertyIntrospector$SizeAccessorMethodParser
0%
0/9
0%
0/4
1.72
 
 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.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.commons.clazz.reflect.ReflectedClazz;
 24  
 import org.apache.commons.clazz.reflect.common.*;
 25  
 
 26  
 /**
 27  
  * A ReflectedPropertyIntrospector that discovers list (aka indexed) properties.
 28  
  * 
 29  
  * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
 30  
  * @version $Id: ExtendedReflectedListPropertyIntrospector.java 155436 2005-02-26 13:17:48Z dirkv $
 31  
  */
 32  0
 public class ExtendedReflectedListPropertyIntrospector
 33  
     extends ReflectedListPropertyIntrospectorSupport 
 34  
 {
 35  0
     protected static final AccessorMethodParser ADD_METHOD_PARSER =
 36  
         new AddAccessorMethodParser();
 37  
 
 38  0
     protected static final AccessorMethodParser ADD_INDEXED_METHOD_PARSER =
 39  
         new AddIndexedAccessorMethodParser();
 40  
 
 41  0
     protected static final AccessorMethodParser REMOVE_INDEXED_METHOD_PARSER =
 42  
         new RemoveIndexedAccessorMethodParser();
 43  
 
 44  0
     protected static final AccessorMethodParser REMOVE_METHOD_PARSER =
 45  
         new RemoveAccessorMethodParser();
 46  
 
 47  0
     protected static final AccessorMethodParser SIZE_METHOD_PARSER =
 48  
         new SizeAccessorMethodParser();
 49  
                         
 50  
                            
 51  
     public void introspectProperties(
 52  
             ReflectedClazz clazz,
 53  
             Class javaClass,
 54  
             Map parseResultMap)
 55  
     {
 56  0
         HashMap parseResultMapSingular = new HashMap();
 57  0
         Method methods[] = javaClass.getMethods();
 58  
         ReflectedListPropertyParseResults parseResults;
 59  
         AccessorMethodParseResults results;
 60  0
         for (int i = 0; i < methods.length; i++) {
 61  0
             Method method = methods[i];
 62  0
             Method method3 = method;
 63  
 
 64  
             // Check getFooCount() before we check getFooList(),
 65  
             // because the parser for the latter is generic enough
 66  
             // to include the former            
 67  0
             results = getSizeAccessorMethodParser().parse(method3);
 68  0
             if (results != null) {
 69  0
                 parseResults =
 70  
                     getParseResults(
 71  
                         clazz,
 72  
                         parseResultMapSingular,
 73  
                         results.getPropertyName());
 74  0
                 parseResults.setSizeMethodParseResults(results);
 75  0
                 continue;
 76  
             }
 77  
             
 78  0
             results = getReadAccessMethodParser().parse(method);
 79  0
             if (results != null) {
 80  0
                 parseResults =
 81  
                     getParseResults(
 82  
                         clazz,
 83  
                         parseResultMap,
 84  
                         results.getPropertyName());
 85  0
                 parseResults.setReadMethodParseResults(results);
 86  0
                 continue;
 87  
             }
 88  
 
 89  0
             results = getWriteAccessMethodParser().parse(method);
 90  0
             if (results != null) {
 91  0
                 parseResults =
 92  
                     getParseResults(
 93  
                         clazz,
 94  
                         parseResultMap,
 95  
                         results.getPropertyName());
 96  0
                 parseResults.setWriteMethodParseResults(results);
 97  0
                 continue;
 98  
             }
 99  
 
 100  0
             results = getGetAccessMethodParser().parse(method);
 101  0
             if (results != null) {
 102  0
                 parseResults =
 103  
                     getParseResults(
 104  
                         clazz,
 105  
                         parseResultMapSingular,
 106  
                         results.getPropertyName());
 107  0
                 parseResults.setGetMethodParseResults(results);
 108  0
                 continue;
 109  
             }
 110  
             
 111  0
             results = getSetAccessMethodParser().parse(method);
 112  0
             if (results != null) {
 113  0
                 parseResults =
 114  
                     getParseResults(
 115  
                         clazz,
 116  
                         parseResultMapSingular,
 117  
                         results.getPropertyName());
 118  0
                 parseResults.setSetMethodParseResults(results);
 119  0
                 continue;
 120  
             }
 121  
             
 122  0
             results = getAddAccessorMethodParser().parse(method);
 123  0
             if (results != null) {
 124  0
                 parseResults =
 125  
                     getParseResults(
 126  
                         clazz,
 127  
                         parseResultMapSingular,
 128  
                         results.getPropertyName());
 129  0
                 parseResults.setAddMethodParseResults(results);
 130  0
                 continue;
 131  
             }
 132  
 
 133  0
             results = getAddIndexedAccessorMethodParser().parse(method);
 134  0
             if (results != null) {
 135  0
                 parseResults =
 136  
                     getParseResults(
 137  
                         clazz,
 138  
                         parseResultMapSingular,
 139  
                         results.getPropertyName());
 140  0
                 parseResults.setAddIndexedMethodParseResults(results);
 141  0
                 continue;
 142  
             }
 143  
 
 144  0
             results = getRemoveAccessorMethodParser().parse(method);
 145  0
             if (results != null) {
 146  0
                 parseResults =
 147  
                     getParseResults(
 148  
                         clazz,
 149  
                         parseResultMapSingular,
 150  
                         results.getPropertyName());
 151  0
                 parseResults.setRemoveMethodParseResults(results);
 152  0
                 continue;
 153  
             }
 154  
 
 155  0
             results = getRemoveIndexedAccessorMethodParser().parse(method);
 156  0
             if (results != null) {
 157  0
                 parseResults =
 158  
                     getParseResults(
 159  
                         clazz,
 160  
                         parseResultMapSingular,
 161  
                         results.getPropertyName());
 162  0
                 parseResults.setRemoveIndexedMethodParseResults(results);
 163  0
                 continue;
 164  
             }
 165  
         }
 166  
         
 167  0
         Iterator iter = parseResultMap.entrySet().iterator();
 168  0
         while (iter.hasNext()) {
 169  0
             Map.Entry entry = (Map.Entry) iter.next();
 170  0
             ReflectedListPropertyParseResults result = 
 171  
                 (ReflectedListPropertyParseResults) entry.getValue();
 172  0
             if (!result.isList()) {
 173  0
                 iter.remove();
 174  
             }
 175  0
         }
 176  
         
 177  0
         mergeSingularMethods(parseResultMap, parseResultMapSingular);
 178  0
     }
 179  
     
 180  
     protected AccessorMethodParser getAddAccessorMethodParser() {
 181  0
         return ADD_METHOD_PARSER;
 182  
     }
 183  
 
 184  
     protected AccessorMethodParser getAddIndexedAccessorMethodParser() {
 185  0
         return ADD_INDEXED_METHOD_PARSER;
 186  
     }
 187  
 
 188  
     protected AccessorMethodParser getRemoveAccessorMethodParser() {
 189  0
         return REMOVE_METHOD_PARSER;
 190  
     }
 191  
 
 192  
     protected AccessorMethodParser getRemoveIndexedAccessorMethodParser() {
 193  0
         return REMOVE_INDEXED_METHOD_PARSER;
 194  
     }
 195  
 
 196  
     protected AccessorMethodParser getSizeAccessorMethodParser() {
 197  0
         return SIZE_METHOD_PARSER;
 198  
     }
 199  
 
 200  
     /**
 201  
      * Returns <code>true</code> if the suffix is "s" or 
 202  
      * "List", "Array" or "Vector".
 203  
      *  
 204  
      * @see ReflectedPropertyIntrospectorSupport#isCorrectPluralSuffix(String,String)
 205  
      */
 206  
     protected boolean isCorrectPluralSuffix(String singular, String suffix) {
 207  0
         return super.isCorrectPluralSuffix(singular, suffix)
 208  
             || suffix.equals("List")
 209  
             || suffix.equals("Array")
 210  
             || suffix.equals("Vector");
 211  
     }
 212  
 
 213  
     /**
 214  
      * Parser for the <code>addFoo(value)</code> method:
 215  
      * <ul>
 216  
      *  <li>Return type void</li>
 217  
      *  <li>Name starts with "add" followed by capitalized singular
 218  
      *      form of the property name</li>
 219  
      *  <li>One parameter</li>
 220  
      * </ul>
 221  
      */                        
 222  0
     public static class AddAccessorMethodParser extends AccessorMethodParser {
 223  
         protected String requiredPrefix() {
 224  0
             return "add";
 225  
         }
 226  
         protected int requiredParameterCount() {
 227  0
             return 1;
 228  
         }
 229  
         protected Class getValueType(Method method) {
 230  0
             return method.getParameterTypes()[0];
 231  
         }
 232  
     }
 233  
     
 234  
     /**
 235  
      * Parser for the <code>addFoo(index, value)</code> method:
 236  
      * <ul>
 237  
      *  <li>Return type void</li>
 238  
      *  <li>Name starts with "add" followed by capitalized singular
 239  
      *      form of the property name</li>
 240  
      *  <li>Two parameters, first integer</li>
 241  
      * </ul>
 242  
      */                        
 243  0
     public static class AddIndexedAccessorMethodParser
 244  
         extends AccessorMethodParser 
 245  
     {
 246  
         protected String requiredPrefix() {
 247  0
             return "add";
 248  
         }
 249  
         protected int requiredParameterCount() {
 250  0
             return 2;
 251  
         }
 252  
         protected boolean testParameterType(int index, Class parameterType) {
 253  0
             if (index == 0) {
 254  0
                 return parameterType.equals(Integer.TYPE);
 255  
             }
 256  0
             return true;
 257  
         }
 258  
         protected Class getValueType(Method method) {
 259  0
             return method.getParameterTypes()[1];
 260  
         }
 261  
     }
 262  
         
 263  
     /**
 264  
      * Parser for the <code>removeFoo(index)</code> method:
 265  
      * <ul>
 266  
      *  <li>Name starts with "remove" followed by capitalized singular
 267  
      *      form of the property name</li>
 268  
      *  <li>One integer parameter</li>
 269  
      * </ul>
 270  
      */
 271  0
     public static class RemoveIndexedAccessorMethodParser
 272  
         extends AccessorMethodParser 
 273  
     {
 274  
         protected String requiredPrefix() {
 275  0
             return "remove";
 276  
         }
 277  
         protected int requiredParameterCount() {
 278  0
             return 1;
 279  
         }
 280  
         protected boolean testParameterType(int index, Class parameterType) {
 281  0
             return parameterType.equals(Integer.TYPE);
 282  
         }
 283  
     }
 284  
 
 285  
     /**
 286  
      * Parser for the <code>removeFoo(value)</code> method:
 287  
      * <ul>
 288  
      *  <li>Name starts with "remove" followed by capitalized singular
 289  
      *      form of the property name</li>
 290  
      *  <li>One parameter</li>
 291  
      * </ul>
 292  
      */                        
 293  0
     public static class RemoveAccessorMethodParser 
 294  
         extends AccessorMethodParser 
 295  
     {
 296  
         protected String requiredPrefix() {
 297  0
             return "remove";
 298  
         }
 299  
         protected int requiredParameterCount() {
 300  0
             return 1;
 301  
         }
 302  
         protected boolean testParameterType(int index, Class parameterType) {
 303  0
             return !parameterType.equals(Integer.TYPE);
 304  
         }
 305  
         protected Class getValueType(Method method) {
 306  0
             return method.getParameterTypes()[0];
 307  
         }
 308  
     }
 309  
     
 310  
     /**
 311  
      * Parser for the <code>getFooCount()</code> method:
 312  
      * <ul>
 313  
      *  <li>Returns integer</li>
 314  
      *  <li>Name starts with "get" followed by capitalized singular
 315  
      *      form of the property name, followed by "Count" or "Size"</li>
 316  
      *  <li>No parameters</li>
 317  
      * </ul>
 318  
      */                        
 319  0
     public static class SizeAccessorMethodParser extends AccessorMethodParser {
 320  
         protected boolean testReturnType(Class javaClass) {
 321  0
             return javaClass.equals(Integer.TYPE);
 322  
         }
 323  
         protected String requiredPrefix() {
 324  0
             return "get";
 325  
         }
 326  
         protected int requiredParameterCount() {
 327  0
             return 0;
 328  
         }
 329  
         protected String testAndRemoveSuffix(String methodName) {
 330  0
             if (methodName.endsWith("Count")) {                
 331  0
                 return methodName.substring(0, methodName.length() - 5);
 332  
             }
 333  0
             if (methodName.endsWith("Size")) {
 334  0
                 return methodName.substring(0, methodName.length() - 4);
 335  
             }
 336  0
             return null;
 337  
         }
 338  
     }
 339  
             
 340  
 }