Coverage Report - org.apache.commons.clazz.reflect.common.ReflectedPropertyIntrospectorSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectedPropertyIntrospectorSupport
0%
0/50
0%
0/32
3.667
 
 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.common;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.clazz.ClazzProperty;
 26  
 import org.apache.commons.clazz.reflect.ReflectedClazz;
 27  
 import org.apache.commons.clazz.reflect.ReflectedPropertyIntrospector;
 28  
 
 29  
 /**
 30  
  * 
 31  
  * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
 32  
  * @version $Id: ReflectedPropertyIntrospectorSupport.java 155436 2005-02-26 13:17:48Z dirkv $
 33  
  */
 34  0
 public abstract class ReflectedPropertyIntrospectorSupport
 35  
     implements ReflectedPropertyIntrospector 
 36  
 {
 37  
     /**
 38  
      */
 39  
     public List introspectProperties(ReflectedClazz clazz, Class javaClass) {
 40  0
         HashMap parseResultMap = new HashMap();
 41  0
         introspectProperties(clazz, javaClass, parseResultMap);
 42  
 
 43  0
         boolean loggingEnabled = clazz.isLoggingEnabled();
 44  
 
 45  0
         ArrayList list = new ArrayList();
 46  0
         Iterator iter = parseResultMap.values().iterator();
 47  0
         while (iter.hasNext()) {
 48  0
             ReflectedPropertyParseResults parseResults =
 49  
                 (ReflectedPropertyParseResults) iter.next();
 50  0
             if (clazz.getProperty(parseResults.getPropertyName()) != null) {
 51  0
                 continue;
 52  
             }
 53  
 
 54  0
             boolean aliasExists = false;
 55  0
             String aliases[] = parseResults.getAliases();
 56  0
             for (int i = 0; i < aliases.length; i++) {
 57  0
                 if (clazz.getProperty(aliases[i]) != null) {
 58  0
                     aliasExists = true;
 59  0
                     break;
 60  
                 }
 61  
             }
 62  0
             if (aliasExists) {
 63  0
                 continue;
 64  
             }
 65  
 
 66  0
             boolean consistent = parseResults.checkConsistency();
 67  0
             if (consistent) {
 68  0
                 list.add(createProperty(clazz, parseResults));
 69  
             }
 70  0
             if (loggingEnabled) {
 71  0
                 clazz.logPropertyParseResults(parseResults);
 72  
             }
 73  0
         }
 74  0
         return list;
 75  
     }
 76  
     
 77  
     public abstract void introspectProperties(
 78  
         ReflectedClazz clazz,
 79  
         Class javaClass,
 80  
         Map parseResultMap);
 81  
 
 82  
     public List introspectDeclaredProperties(
 83  
             ReflectedClazz clazz,
 84  
             Class javaClass)
 85  
     {
 86  0
         Class instanceClass = clazz.getInstanceClass();
 87  0
         List declaredPropertyList = null;
 88  0
         if (instanceClass.getSuperclass() == null) {
 89  0
             declaredPropertyList = clazz.getProperties();
 90  
         }
 91  
         else {
 92  0
             List superProperties = clazz.getSuperclazz().getProperties();
 93  0
             if (superProperties.size() == 0) {
 94  0
                 declaredPropertyList = clazz.getProperties();
 95  
             }
 96  
             else {
 97  0
                 HashSet superNames = new HashSet();
 98  0
                 for (int i = 0; i < superProperties.size(); i++) {
 99  0
                     ClazzProperty property =
 100  
                             (ClazzProperty) superProperties.get(i);
 101  0
                     superNames.add(property.getName());
 102  
                 }
 103  
 
 104  0
                 List properties = clazz.getProperties();
 105  0
                 declaredPropertyList = new ArrayList();
 106  0
                 for (int i = 0; i < properties.size(); i++) {
 107  0
                     ClazzProperty property =
 108  
                         (ClazzProperty) properties.get(i);
 109  0
                     String name = property.getName();
 110  0
                     if (!superNames.contains(name)) {
 111  0
                         declaredPropertyList.add(property);
 112  
                     }
 113  
                 }
 114  
             }
 115  
         }
 116  0
         return declaredPropertyList;
 117  
     }
 118  
                 
 119  
     /**
 120  
      * Creates a new ReflectedAccessorPairProperty based on parse results. 
 121  
      */
 122  
     protected abstract ReflectedAccessorPairProperty createProperty(
 123  
         ReflectedClazz clazz,
 124  
         ReflectedPropertyParseResults parseResults);
 125  
     
 126  
     
 127  
     /**
 128  
      * Returns true if the <code>plural</code> parameter is
 129  
      * a correct plural form for the property names <code>singular</code>.
 130  
      * If the plural.startsWith(singular), calls isCorrectPluralSuffix
 131  
      * with the singular form and the remaining part of the plural form.
 132  
      * Separately handles the "~y" - "~ies" substitution for English.
 133  
      */
 134  
     protected boolean isCorrectPluralForm(String singular, String plural) {
 135  0
         if (plural.startsWith(singular)) {
 136  0
             return isCorrectPluralSuffix(
 137  
                 singular,
 138  
                 plural.substring(singular.length()));
 139  
         }
 140  0
         else if (
 141  
             singular.endsWith("y")
 142  
                 && plural.endsWith("ies")
 143  
                 && plural.substring(0, plural.length() - 3).equals(
 144  
                     singular.substring(singular.length() - 1))) {
 145  0
             return true;
 146  
         }
 147  0
         return false;
 148  
     }
 149  
     
 150  
     protected boolean isCorrectPluralSuffix(String singular, String suffix) {
 151  0
         return suffix.equals("s");
 152  
     }
 153  
 }