View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.annotations;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import org.eclipse.jetty.annotations.AnnotationParser.AbstractHandler;
24  import org.eclipse.jetty.annotations.AnnotationParser.ClassInfo;
25  import org.eclipse.jetty.util.ConcurrentHashSet;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.log.Logger;
28  
29  /**
30   * ClassInheritanceHandler
31   *
32   * As asm scans for classes, remember the type hierarchy.
33   */
34  public class ClassInheritanceHandler extends AbstractHandler
35  {
36      private static final Logger LOG = Log.getLogger(ClassInheritanceHandler.class);
37      
38      ConcurrentHashMap<String, ConcurrentHashSet<String>> _inheritanceMap;
39   
40      
41      public ClassInheritanceHandler(ConcurrentHashMap<String, ConcurrentHashSet<String>> map)
42      {
43          _inheritanceMap = map;
44      }
45  
46      public void handle(ClassInfo classInfo)
47      {
48          try
49          {
50              for (int i=0; classInfo.getInterfaces() != null && i < classInfo.getInterfaces().length;i++)
51              {
52                  addToInheritanceMap(classInfo.getInterfaces()[i], classInfo.getClassName());
53                  //_inheritanceMap.add (classInfo.getInterfaces()[i], classInfo.getClassName());
54              }
55              //To save memory, we don't record classes that only extend Object, as that can be assumed
56              if (!"java.lang.Object".equals(classInfo.getSuperName()))
57              {
58                  addToInheritanceMap(classInfo.getSuperName(), classInfo.getClassName());
59                  //_inheritanceMap.add(classInfo.getSuperName(), classInfo.getClassName());
60              }
61          }
62          catch (Exception e)
63          {
64              LOG.warn(e);
65          }  
66      }
67      
68      private void addToInheritanceMap (String interfaceOrSuperClassName, String implementingOrExtendingClassName)
69      {
70        
71          //As it is likely that the interfaceOrSuperClassName is already in the map, try getting it first
72          ConcurrentHashSet<String> implementingClasses = _inheritanceMap.get(interfaceOrSuperClassName);
73          //If it isn't in the map, then add it in, but test to make sure that someone else didn't get in 
74          //first and add it
75          if (implementingClasses == null)
76          {
77              implementingClasses = new ConcurrentHashSet<String>();
78              ConcurrentHashSet<String> tmp = _inheritanceMap.putIfAbsent(interfaceOrSuperClassName, implementingClasses);
79              if (tmp != null)
80                  implementingClasses = tmp;
81          }
82          
83          implementingClasses.add(implementingOrExtendingClassName);
84      }
85  }