001    package org.apache.commons.ognl.internal.entry;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    /*
023     * $Id: MethodCacheEntryFactory.java 1194957 2011-10-29 18:04:20Z mcucchiara $
024     */
025    
026    
027    import org.apache.commons.ognl.OgnlRuntime;
028    import org.apache.commons.ognl.internal.CacheException;
029    
030    import java.lang.reflect.Method;
031    import java.util.ArrayList;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    public abstract class MethodCacheEntryFactory<T extends MethodCacheEntry>
037        implements CacheEntryFactory<T, Map<String, List<Method>>>
038    {
039        public Map<String, List<Method>> create( T key )
040            throws CacheException
041        {
042            Map<String, List<Method>> result = new HashMap<String, List<Method>>( 23 );
043    
044            Class<?> c = key.targetClass;
045            while ( c != null )
046            {
047                for ( Method method : c.getDeclaredMethods() )
048                {
049                    // skip over synthetic methods
050    
051                    if ( !OgnlRuntime.isMethodCallable( method ) )
052                    {
053                        continue;
054                    }
055    
056                    if ( shouldCache( key, method ) )
057                    {
058                        List<Method> ml = result.get( method.getName() );
059    
060                        if ( ml == null )
061                        {
062                            ml = new ArrayList<Method>();
063                            result.put( method.getName(), ml );
064                        }
065    
066                        ml.add( method );
067                    }
068                }
069                c = c.getSuperclass();
070            }
071            return result;
072        }
073    
074        protected abstract boolean shouldCache( T key, Method method );
075    }