View Javadoc

1   package org.apache.commons.ognl.internal;
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: ClassCacheHandler.java 1203584 2011-11-18 10:54:19Z mcucchiara $
24   */
25  public class ClassCacheHandler
26  {
27  
28      private ClassCacheHandler()
29      {
30      }
31  
32      public static <T> T getHandler( Class<?> forClass, ClassCache<T> handlers )
33          throws CacheException
34      {
35          T answer;
36  
37          synchronized ( handlers )
38          {
39              answer = handlers.get( forClass );
40              if ( answer == null )
41              {
42                  Class<?> keyFound;
43  
44                  if ( forClass.isArray() )
45                  {
46                      answer = handlers.get( Object[].class );
47                      keyFound = null;
48                  }
49                  else
50                  {
51                      keyFound = forClass;
52                      outer:
53                      for ( Class<?> clazz = forClass; clazz != null; clazz = clazz.getSuperclass() )
54                      {
55                          answer = handlers.get( clazz );
56                          if ( answer == null )
57                          {
58                              Class<?>[] interfaces = clazz.getInterfaces();
59                              for ( Class<?> iface : interfaces )
60                              {
61                                  answer = handlers.get( iface );
62                                  if ( answer == null )
63                                  {
64                                      /* Try super-interfaces */
65                                      answer = getHandler( iface, handlers );
66                                  }
67                                  if ( answer != null )
68                                  {
69                                      keyFound = iface;
70                                      break outer;
71                                  }
72                              }
73                          }
74                          else
75                          {
76                              keyFound = clazz;
77                              break;
78                          }
79                      }
80                  }
81                  if ( answer != null )
82                  {
83                      if ( keyFound != forClass )
84                      {
85                          handlers.put( forClass, answer );
86                      }
87                  }
88              }
89          }
90          return answer;
91  
92      }
93  }