Class ClassInfoList

All Implemented Interfaces:
Serializable, Cloneable, Iterable<ClassInfo>, Collection<ClassInfo>, List<ClassInfo>, RandomAccess

public class ClassInfoList extends MappableInfoList<ClassInfo>
A uniquified (deduplicated) list of ClassInfo objects, which stores both reachable classes (obtained through a given class relationship, either by direct relationship or through an indirect path), and directly related classes (classes reachable through a direct relationship only). (By default, accessing a ClassInfoList as a List returns only reachable classes; by calling directOnly(), you can get the directly related classes.)

Most ClassInfoList objects returned by ClassGraph are sorted into lexicographical order by the value of ClassInfo.getName(). One exception to this is the classes returned by ClassInfo.getSuperclasses(), which are in ascending order of the class hierarchy.

See Also:
  • Constructor Details

    • ClassInfoList

      public ClassInfoList()
      Construct a new empty modifiable list of ClassInfo objects.
    • ClassInfoList

      public ClassInfoList(int sizeHint)
      Construct a new empty modifiable list of ClassInfo objects, given a size hint.
      Parameters:
      sizeHint - the size hint.
    • ClassInfoList

      public ClassInfoList(Collection<ClassInfo> classInfoCollection)
      Construct a new modifiable empty ClassInfoList, given an initial list of ClassInfo objects.

      If the passed Collection is not a Set, then the ClassInfo objects will be uniquified (by adding them to a set) before they are added to the returned list. ClassInfo objects in the returned list will be sorted by name.

      Parameters:
      classInfoCollection - the initial collection of ClassInfo objects to add to the ClassInfoList.
  • Method Details

    • emptyList

      public static ClassInfoList emptyList()
      Return an unmodifiable empty ClassInfoList.
      Returns:
      the unmodifiable empty ClassInfoList.
    • loadClasses

      public <T> List<Class<T>> loadClasses(Class<T> superclassOrInterfaceType, boolean ignoreExceptions)
      Convert this list of ClassInfo objects to a list of Class<?> objects, casting each item in the list to the requested superclass or interface type. Causes the classloader to load the class named by each ClassInfo object, if it is not already loaded.

      Important note: since superclassOrInterfaceType is a class reference for an already-loaded class, it is critical that superclassOrInterfaceType is loaded by the same classloader as the class referred to by this ClassInfo object, otherwise the class cast will fail.

      Type Parameters:
      T - The superclass or interface.
      Parameters:
      superclassOrInterfaceType - The superclass or interface class reference to cast each loaded class to.
      ignoreExceptions - If true, ignore any exceptions or errors thrown during classloading, or when attempting to cast the resulting Class<?> reference to the requested type -- instead, skip the element (i.e. the returned list may contain fewer items than this input list). If false, IllegalArgumentException is thrown if the class could not be loaded or could not be cast to the requested type.
      Returns:
      The loaded Class<?> objects corresponding to each ClassInfo object in this list.
      Throws:
      IllegalArgumentException - if ignoreExceptions is false and an exception or error was thrown while trying to load or cast any of the classes.
    • loadClasses

      public <T> List<Class<T>> loadClasses(Class<T> superclassOrInterfaceType)
      Convert this list of ClassInfo objects to a list of Class<?> objects, casting each item in the list to the requested superclass or interface type. Causes the classloader to load the class named by each ClassInfo object, if it is not already loaded.

      Important note: since superclassOrInterfaceType is a class reference for an already-loaded class, it is critical that superclassOrInterfaceType is loaded by the same classloader as the class referred to by this ClassInfo object, otherwise the class cast will fail.

      Type Parameters:
      T - The superclass or interface.
      Parameters:
      superclassOrInterfaceType - The superclass or interface class reference to cast each loaded class to.
      Returns:
      The loaded Class<?> objects corresponding to each ClassInfo object in this list.
      Throws:
      IllegalArgumentException - if an exception or error was thrown while trying to load or cast any of the classes.
    • loadClasses

      public List<Class<?>> loadClasses(boolean ignoreExceptions)
      Convert this list of ClassInfo objects to a list of Class<?> objects. Causes the classloader to load the class named by each ClassInfo object, if it is not already loaded.
      Parameters:
      ignoreExceptions - If true, ignore any exceptions or errors thrown during classloading. If an exception or error is thrown during classloading, no Class<?> reference is added to the output class for the corresponding ClassInfo object, so the returned list may contain fewer items than this input list. If false, IllegalArgumentException is thrown if the class could not be loaded.
      Returns:
      The loaded Class<?> objects corresponding to each ClassInfo object in this list.
      Throws:
      IllegalArgumentException - if ignoreExceptions is false and an exception or error was thrown while trying to load any of the classes.
    • loadClasses

      public List<Class<?>> loadClasses()
      Convert this list of ClassInfo objects to a list of Class<?> objects. Causes the classloader to load the class named by each ClassInfo object, if it is not already loaded.
      Returns:
      The loaded Class<?> objects corresponding to each ClassInfo object in this list.
      Throws:
      IllegalArgumentException - if an exception or error was thrown while trying to load any of the classes.
    • directOnly

      public ClassInfoList directOnly()
      Get the list of classes that were directly related, as opposed to reachable through multiple steps. For example, if this ClassInfoList was produced by querying for all superclasses of a given class, then directOnly() will return only the direct superclass of this class.
      Returns:
      The list of directly-related classes.
    • union

      public ClassInfoList union(ClassInfoList... others)
      Find the union of this ClassInfoList with one or more others.
      Parameters:
      others - The other ClassInfoLists to union with this one.
      Returns:
      The union of this ClassInfoList with the others.
    • intersect

      public ClassInfoList intersect(ClassInfoList... others)
      Find the intersection of this ClassInfoList with one or more others.
      Parameters:
      others - The other ClassInfoLists to intersect with this one.
      Returns:
      The intersection of this ClassInfoList with the others.
    • exclude

      public ClassInfoList exclude(ClassInfoList other)
      Find the set difference between this ClassInfoList and another ClassInfoList, i.e. (this \ other).
      Parameters:
      other - The other ClassInfoList to subtract from this one.
      Returns:
      The set difference of this ClassInfoList and other, i.e. (this \ other).
    • filter

      Find the subset of this ClassInfoList for which the given filter predicate is true.
      Parameters:
      filter - The ClassInfoList.ClassInfoFilter to apply.
      Returns:
      The subset of this ClassInfoList for which the given filter predicate is true.
    • getStandardClasses

      public ClassInfoList getStandardClasses()
      Filter this ClassInfoList to include only standard classes (classes that are not interfaces or annotations).
      Returns:
      The filtered list, containing only standard classes.
    • getInterfaces

      public ClassInfoList getInterfaces()
      Filter this ClassInfoList to include only interfaces that are not annotations. See also getInterfacesAndAnnotations().
      Returns:
      The filtered list, containing only interfaces.
    • getInterfacesAndAnnotations

      public ClassInfoList getInterfacesAndAnnotations()
      Filter this ClassInfoList to include only interfaces and annotations (annotations are interfaces, and can be implemented). See also getInterfaces().
      Returns:
      The filtered list, containing only interfaces.
    • getImplementedInterfaces

      public ClassInfoList getImplementedInterfaces()
      Filter this ClassInfoList to include only implemented interfaces, i.e. non-annotation interfaces, or annotations that have been implemented by a class.
      Returns:
      The filtered list, containing only implemented interfaces.
    • getAnnotations

      public ClassInfoList getAnnotations()
      Filter this ClassInfoList to include only annotations.
      Returns:
      The filtered list, containing only annotations.
    • getEnums

      public ClassInfoList getEnums()
      Filter this ClassInfoList to include only Enum classes.
      Returns:
      The filtered list, containing only enums.
    • getRecords

      public ClassInfoList getRecords()
      Filter this ClassInfoList to include only record classes.
      Returns:
      The filtered list, containing only record classes.
    • getAssignableTo

      public ClassInfoList getAssignableTo(ClassInfo superclassOrInterface)
      Filter this ClassInfoList to include only classes that are assignable to the requested class, assignableToClass (i.e. where assignableToClass is a superclass or implemented interface of the list element).
      Parameters:
      superclassOrInterface - the superclass or interface to filter for.
      Returns:
      The filtered list, containing only classes for which assignableToClassRef.isAssignableFrom(listItemClassRef) is true for the corresponding Class<?> references for assignableToClass and the list items. Returns the empty list if no classes were assignable to the requested class.
      Throws:
      IllegalArgumentException - if classInfo is null.
    • generateGraphVizDotFileFromInterClassDependencies

      public String generateGraphVizDotFileFromInterClassDependencies(float sizeX, float sizeY, boolean includeExternalClasses)
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph. The returned graph shows inter-class dependencies only. The sizeX and sizeY parameters are the image output size to use (in inches) when GraphViz is asked to render the .dot file. You must have called ClassGraph.enableInterClassDependencies() before scanning to use this method.
      Parameters:
      sizeX - The GraphViz layout width in inches.
      sizeY - The GraphViz layout width in inches.
      includeExternalClasses - If true, and if ClassGraph.enableExternalClasses() was called before scanning, show "external classes" (non-accepted classes) within the dependency graph.
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableInterClassDependencies() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFileFromInterClassDependencies

      public String generateGraphVizDotFileFromInterClassDependencies(float sizeX, float sizeY)
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph. The returned graph shows inter-class dependencies only. The sizeX and sizeY parameters are the image output size to use (in inches) when GraphViz is asked to render the .dot file. You must have called ClassGraph.enableInterClassDependencies() before scanning to use this method.

      Equivalent to calling generateGraphVizDotFileFromInterClassDependencies(float, float, boolean) with parameters of (10.5f, 8f, scanSpec.enableExternalClasses), where scanSpec.enableExternalClasses is true if ClassGraph.enableExternalClasses() was called before scanning.

      Parameters:
      sizeX - The GraphViz layout width in inches.
      sizeY - The GraphViz layout width in inches.
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableInterClassDependencies() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFileFromInterClassDependencies

      public String generateGraphVizDotFileFromInterClassDependencies()
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph. The returned graph shows inter-class dependencies only. The sizeX and sizeY parameters are the image output size to use (in inches) when GraphViz is asked to render the .dot file. You must have called ClassGraph.enableInterClassDependencies() before scanning to use this method.

      Equivalent to calling generateGraphVizDotFileFromInterClassDependencies(float, float, boolean) with parameters of (10.5f, 8f, scanSpec.enableExternalClasses), where scanSpec.enableExternalClasses is true if ClassGraph.enableExternalClasses() was called before scanning.

      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableInterClassDependencies() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFileFromClassDependencies

      @Deprecated public String generateGraphVizDotFileFromClassDependencies()
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableInterClassDependencies() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFile

      public String generateGraphVizDotFile(float sizeX, float sizeY, boolean showFields, boolean showFieldTypeDependencyEdges, boolean showMethods, boolean showMethodTypeDependencyEdges, boolean showAnnotations, boolean useSimpleNames)
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph. The sizeX and sizeY parameters are the image output size to use (in inches) when GraphViz is asked to render the .dot file.

      To show non-public classes, call ClassGraph.ignoreClassVisibility() before scanning.

      To show fields, call ClassGraph.enableFieldInfo() before scanning. To show non-public fields, also call ClassGraph.ignoreFieldVisibility() before scanning.

      To show methods, call ClassGraph.enableMethodInfo() before scanning. To show non-public methods, also call ClassGraph.ignoreMethodVisibility() before scanning.

      To show annotations, call ClassGraph.enableAnnotationInfo() before scanning. To show non-public annotations, also call ClassGraph.ignoreFieldVisibility() before scanning (there is no separate visibility modifier for annotations).

      Parameters:
      sizeX - The GraphViz layout width in inches.
      sizeY - The GraphViz layout width in inches.
      showFields - If true, show fields within class nodes in the graph.
      showFieldTypeDependencyEdges - If true, show edges between classes and the types of their fields.
      showMethods - If true, show methods within class nodes in the graph.
      showMethodTypeDependencyEdges - If true, show edges between classes and the return types and/or parameter types of their methods.
      showAnnotations - If true, show annotations in the graph.
      useSimpleNames - whether to use simple names for classes in type signatures (if true, the package name is stripped from class names in method and field type signatures).
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableClassInfo() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFile

      public String generateGraphVizDotFile(float sizeX, float sizeY, boolean showFields, boolean showFieldTypeDependencyEdges, boolean showMethods, boolean showMethodTypeDependencyEdges, boolean showAnnotations)
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph. The sizeX and sizeY parameters are the image output size to use (in inches) when GraphViz is asked to render the .dot file.

      To show non-public classes, call ClassGraph.ignoreClassVisibility() before scanning.

      To show fields, call ClassGraph.enableFieldInfo() before scanning. To show non-public fields, also call ClassGraph.ignoreFieldVisibility() before scanning.

      To show methods, call ClassGraph.enableMethodInfo() before scanning. To show non-public methods, also call ClassGraph.ignoreMethodVisibility() before scanning.

      To show annotations, call ClassGraph.enableAnnotationInfo() before scanning. To show non-public annotations, also call ClassGraph.ignoreFieldVisibility() before scanning (there is no separate visibility modifier for annotations).

      This method uses simple names for class names in type signatures of fields and methods (package names are stripped).

      Parameters:
      sizeX - The GraphViz layout width in inches.
      sizeY - The GraphViz layout width in inches.
      showFields - If true, show fields within class nodes in the graph.
      showFieldTypeDependencyEdges - If true, show edges between classes and the types of their fields.
      showMethods - If true, show methods within class nodes in the graph.
      showMethodTypeDependencyEdges - If true, show edges between classes and the return types and/or parameter types of their methods.
      showAnnotations - If true, show annotations in the graph.
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableClassInfo() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFile

      public String generateGraphVizDotFile(float sizeX, float sizeY)
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph.

      Methods, fields and annotations are shown if enabled, via ClassGraph.enableMethodInfo(), ClassGraph.enableFieldInfo() and ClassGraph.enableAnnotationInfo().

      Only public classes, methods, and fields are shown, unless ClassGraph.ignoreClassVisibility(), ClassGraph.ignoreMethodVisibility(), and/or ClassGraph.ignoreFieldVisibility() has/have been called.

      Parameters:
      sizeX - The GraphViz layout width in inches.
      sizeY - The GraphViz layout width in inches.
      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableClassInfo() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFile

      public String generateGraphVizDotFile()
      Generate a .dot file which can be fed into GraphViz for layout and visualization of the class graph.

      Methods, fields and annotations are shown if enabled, via ClassGraph.enableMethodInfo(), ClassGraph.enableFieldInfo() and ClassGraph.enableAnnotationInfo().

      Only public classes, methods, and fields are shown, unless ClassGraph.ignoreClassVisibility(), ClassGraph.ignoreMethodVisibility(), and/or ClassGraph.ignoreFieldVisibility() has/have been called.

      Returns:
      the GraphViz file contents.
      Throws:
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableClassInfo() was not called before scanning (since there would be nothing to graph).
    • generateGraphVizDotFile

      public void generateGraphVizDotFile(File file) throws IOException
      Generate a and save a .dot file, which can be fed into GraphViz for layout and visualization of the class graph.

      Methods, fields and annotations are shown if enabled, via ClassGraph.enableMethodInfo(), ClassGraph.enableFieldInfo() and ClassGraph.enableAnnotationInfo().

      Only public classes, methods, and fields are shown, unless ClassGraph.ignoreClassVisibility(), ClassGraph.ignoreMethodVisibility(), and/or ClassGraph.ignoreFieldVisibility() has/have been called.

      Parameters:
      file - the file to save the GraphViz .dot file to.
      Throws:
      IOException - if the file could not be saved.
      IllegalArgumentException - if this ClassInfoList is empty or ClassGraph.enableClassInfo() was not called before scanning (since there would be nothing to graph).
    • equals

      public boolean equals(Object obj)
      Specified by:
      equals in interface Collection<ClassInfo>
      Specified by:
      equals in interface List<ClassInfo>
      Overrides:
      equals in class InfoList<ClassInfo>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<ClassInfo>
      Specified by:
      hashCode in interface List<ClassInfo>
      Overrides:
      hashCode in class InfoList<ClassInfo>
    • add

      public boolean add(ClassInfo element)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface List<T>
      Overrides:
      add in class ArrayList<T>
    • add

      public void add(int index, ClassInfo element)
      Specified by:
      add in interface List<T>
      Overrides:
      add in class ArrayList<T>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface List<T>
      Overrides:
      remove in class ArrayList<T>
    • remove

      public ClassInfo remove(int index)
      Specified by:
      remove in interface List<T>
      Overrides:
      remove in class ArrayList<T>
    • addAll

      public boolean addAll(Collection<? extends ClassInfo> c)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface List<T>
      Overrides:
      addAll in class ArrayList<T>
    • addAll

      public boolean addAll(int index, Collection<? extends ClassInfo> c)
      Specified by:
      addAll in interface List<T>
      Overrides:
      addAll in class ArrayList<T>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface List<T>
      Overrides:
      removeAll in class ArrayList<T>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface List<T>
      Overrides:
      retainAll in class ArrayList<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface List<T>
      Overrides:
      clear in class ArrayList<T>
    • set

      public ClassInfo set(int index, ClassInfo element)
      Specified by:
      set in interface List<T>
      Overrides:
      set in class ArrayList<T>
    • iterator

      public Iterator<ClassInfo> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface List<T>
      Overrides:
      iterator in class ArrayList<T>
    • listIterator

      public ListIterator<ClassInfo> listIterator()
      Specified by:
      listIterator in interface List<T>
      Overrides:
      listIterator in class ArrayList<T>