Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-500

ClassUtils.getAllInterfaces(...) could be more efficient

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 2.5
    • lang.*
    • None

    Description

      This could seem like a very minor thing but why not improve
      the code once in a while...

      Something like this could replace the current inefficient code:

          public static List<Class<?>> getAllInterfaces(Class<?> clazz)
          {
              if (clazz == null)
              {
                  return null;
              }
      
              HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
              LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
      
              getAllInterfaces(clazz, interfacesSet, interfacesList);
      
              return interfacesList;
          }
      
          private static void getAllInterfaces(
                  Class<?> clazz,
                  HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
          {
              while (clazz != null)
              {
                  Class<?>[] interfaces = clazz.getInterfaces();
      
                  for (Class<?> i : interfaces)
                  {
                      if (!interfacesSet.add(i))
                      {
                          interfacesList.add(i);
                          getAllInterfaces(i, interfacesSet, interfacesList);
                      }
                  }
      
                  clazz = clazz.getSuperclass();
              }
          }
      

      Attachments

        1. patch-ClassUtils-02.txt
          2 kB
          Pino Silvaggio
        2. patch-ClassUtils-01.txt
          2 kB
          Pino Silvaggio
        3. LANG-500.patch
          2 kB
          Henri Yandell

        Activity

          People

            Unassigned Unassigned
            pino Pino Silvaggio
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: