Package com.tonic.analysis.dependency
Class DependencyAnalyzer
- java.lang.Object
-
- com.tonic.analysis.dependency.DependencyAnalyzer
-
public class DependencyAnalyzer extends Object
Analyzes class dependencies by scanning constant pools.
-
-
Constructor Summary
Constructors Constructor Description DependencyAnalyzer(ClassPool classPool)Creates a new DependencyAnalyzer and analyzes the given ClassPool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleandependsOn(String classA, String classB)Checks for a direct dependency edge between two classes.intedgeCount()Sums the outgoing dependency edges over every node.List<List<String>>findCircularDependencies()Walks the pool classes depth-first collecting every dependency cycle found.Set<String>findClasses(Predicate<DependencyNode> predicate)Finds classes whose node satisfies a predicate.Set<String>findLeafClasses()Finds pool classes that depend on nothing.Set<String>findRootClasses()Finds pool classes that nothing else depends on.Collection<DependencyNode>getAllNodes()Set<String>getClassesInPackage(String packagePrefix)Selects pool classes whose name starts with the given prefix.Set<String>getDependencies(String className)Looks up the direct dependencies recorded for a class.Set<String>getDependents(String className)Looks up the direct dependents recorded for a class.DependencyNodegetNode(String className)Looks up the graph node for a class.Collection<DependencyNode>getPoolNodes()Selects the nodes backed by a class present in the pool.Set<String>getTransitiveDependencies(String className)Walks the dependency edges breadth-first from the given class.Set<String>getTransitiveDependents(String className)Walks the dependent edges breadth-first from the given class.intsize()StringtoString()booleantransitivelyDependsOn(String classA, String classB)Checks for a dependency reachable through any chain of intermediate classes.
-
-
-
Constructor Detail
-
DependencyAnalyzer
public DependencyAnalyzer(ClassPool classPool)
Creates a new DependencyAnalyzer and analyzes the given ClassPool.- Parameters:
classPool- the pool whose classes are scanned for dependencies
-
-
Method Detail
-
getNode
public DependencyNode getNode(String className)
Looks up the graph node for a class.- Parameters:
className- the class to look up- Returns:
- the node, or null if the class was never seen
-
getAllNodes
public Collection<DependencyNode> getAllNodes()
- Returns:
- an unmodifiable view of every node in the graph
-
getPoolNodes
public Collection<DependencyNode> getPoolNodes()
Selects the nodes backed by a class present in the pool.- Returns:
- the in-pool nodes, excluding referenced-only classes
-
getDependencies
public Set<String> getDependencies(String className)
Looks up the direct dependencies recorded for a class.- Parameters:
className- the class to query- Returns:
- the names of classes it depends on, or an empty set if it has no node
-
getDependents
public Set<String> getDependents(String className)
Looks up the direct dependents recorded for a class.- Parameters:
className- the class to query- Returns:
- the names of classes depending on it, or an empty set if it has no node
-
getTransitiveDependencies
public Set<String> getTransitiveDependencies(String className)
Walks the dependency edges breadth-first from the given class.- Parameters:
className- the class to start from- Returns:
- every class reachable from it, excluding the class itself
-
getTransitiveDependents
public Set<String> getTransitiveDependents(String className)
Walks the dependent edges breadth-first from the given class.- Parameters:
className- the class to start from- Returns:
- every class that transitively depends on it, excluding the class itself
-
findCircularDependencies
public List<List<String>> findCircularDependencies()
Walks the pool classes depth-first collecting every dependency cycle found.- Returns:
- one list of class names per cycle, repeating the entry class as the final element
-
findClasses
public Set<String> findClasses(Predicate<DependencyNode> predicate)
Finds classes whose node satisfies a predicate.- Parameters:
predicate- the test applied to every node in the graph- Returns:
- the matching class names, in graph insertion order
-
findLeafClasses
public Set<String> findLeafClasses()
Finds pool classes that depend on nothing.- Returns:
- the class names with a dependency count of zero
-
findRootClasses
public Set<String> findRootClasses()
Finds pool classes that nothing else depends on.- Returns:
- the class names with a dependent count of zero
-
getClassesInPackage
public Set<String> getClassesInPackage(String packagePrefix)
Selects pool classes whose name starts with the given prefix.- Parameters:
packagePrefix- the package name prefix to match against- Returns:
- the matching class names, in graph insertion order
-
dependsOn
public boolean dependsOn(String classA, String classB)
Checks for a direct dependency edge between two classes.- Parameters:
classA- the dependent class nameclassB- the candidate dependency class name- Returns:
- true if classA directly depends on classB, false if classA has no node
-
transitivelyDependsOn
public boolean transitivelyDependsOn(String classA, String classB)
Checks for a dependency reachable through any chain of intermediate classes.- Parameters:
classA- the dependent class nameclassB- the candidate dependency class name- Returns:
- true if classA transitively depends on classB
-
size
public int size()
- Returns:
- the number of nodes in the graph, including classes outside the pool
-
edgeCount
public int edgeCount()
Sums the outgoing dependency edges over every node.- Returns:
- the total number of dependency edges in the graph
-
-