View Javadoc

1   package org.apache.commons.ognl.internal.entry;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /*
23   * $Id: MethodCacheEntryFactory.java 1194957 2011-10-29 18:04:20Z mcucchiara $
24   */
25  
26  
27  import org.apache.commons.ognl.OgnlRuntime;
28  import org.apache.commons.ognl.internal.CacheException;
29  
30  import java.lang.reflect.Method;
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  public abstract class MethodCacheEntryFactory<T extends MethodCacheEntry>
37      implements CacheEntryFactory<T, Map<String, List<Method>>>
38  {
39      public Map<String, List<Method>> create( T key )
40          throws CacheException
41      {
42          Map<String, List<Method>> result = new HashMap<String, List<Method>>( 23 );
43  
44          Class<?> c = key.targetClass;
45          while ( c != null )
46          {
47              for ( Method method : c.getDeclaredMethods() )
48              {
49                  // skip over synthetic methods
50  
51                  if ( !OgnlRuntime.isMethodCallable( method ) )
52                  {
53                      continue;
54                  }
55  
56                  if ( shouldCache( key, method ) )
57                  {
58                      List<Method> ml = result.get( method.getName() );
59  
60                      if ( ml == null )
61                      {
62                          ml = new ArrayList<Method>();
63                          result.put( method.getName(), ml );
64                      }
65  
66                      ml.add( method );
67                  }
68              }
69              c = c.getSuperclass();
70          }
71          return result;
72      }
73  
74      protected abstract boolean shouldCache( T key, Method method );
75  }