001    /*
002     * Copyright 2002-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.clazz.reflect.standard;
017    
018    import java.lang.reflect.Method;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import org.apache.commons.clazz.reflect.ReflectedClazz;
024    import org.apache.commons.clazz.reflect.common.*;
025    
026    /**
027     * A ReflectedPropertyIntrospector that discovers list (aka indexed) properties.
028     * 
029     * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
030     * @version $Id: StandardReflectedListPropertyIntrospector.java 155436 2005-02-26 13:17:48Z dirkv $
031     */
032    public class StandardReflectedListPropertyIntrospector
033        extends ReflectedListPropertyIntrospectorSupport 
034    {
035        public void introspectProperties(
036                ReflectedClazz clazz,
037                Class javaClass,
038                Map parseResultMap)
039        {
040            HashMap parseResultMapSingular = new HashMap();
041            Method methods[] = javaClass.getMethods();
042            ReflectedListPropertyParseResults parseResults;
043            AccessorMethodParseResults results;
044            for (int i = 0; i < methods.length; i++) {
045                Method method = methods[i];
046                
047                results = getReadAccessMethodParser().parse(method);
048                if (results != null) {
049                    parseResults =
050                        getParseResults(
051                            clazz,
052                            parseResultMap,
053                            results.getPropertyName());
054                    parseResults.setReadMethodParseResults(results);
055                    continue;
056                }
057    
058                results = getWriteAccessMethodParser().parse(method);
059                if (results != null) {
060                    parseResults =
061                        getParseResults(
062                            clazz,
063                            parseResultMap,
064                            results.getPropertyName());
065                    parseResults.setWriteMethodParseResults(results);
066                    continue;
067                }
068    
069                results = getGetAccessMethodParser().parse(method);
070                if (results != null) {
071                    parseResults =
072                        getParseResults(
073                            clazz,
074                            parseResultMapSingular,
075                            results.getPropertyName());
076                    parseResults.setGetMethodParseResults(results);
077                    continue;
078                }
079                
080                results = getSetAccessMethodParser().parse(method);
081                if (results != null) {
082                    parseResults =
083                        getParseResults(
084                            clazz,
085                            parseResultMapSingular,
086                            results.getPropertyName());
087                    parseResults.setSetMethodParseResults(results);
088                    continue;
089                }            
090            }
091            
092            Iterator iter = parseResultMap.entrySet().iterator();
093            while (iter.hasNext()) {
094                Map.Entry entry = (Map.Entry) iter.next();
095                ReflectedListPropertyParseResults result = 
096                    (ReflectedListPropertyParseResults) entry.getValue();
097                    
098                Class propertyType = result.getPropertyType();
099                Class componentType = result.getContentType();
100    
101                if (propertyType != null
102                    && (!propertyType.isArray()
103                        || (componentType != null
104                            && propertyType.getComponentType() != componentType))) {
105                    iter.remove(); // @todo verify that this actually works
106                }
107            }
108            
109            mergeSingularMethods(parseResultMap, parseResultMapSingular);
110        }
111    
112        /**
113         * With the standard JavaBean indexed properties the plural form is the same
114         * as the singular form.
115         */
116        protected boolean isCorrectPluralForm(String singular, String plural) {
117            return singular.equals(plural);
118        }    
119    }