1313public class GraphDependencyCollector implements DependencyCollector {
1414
1515 @ Getter
16- private final Graph <String , DefaultWeightedEdge > classReferencesGraph ;
16+ private final Graph <VertexInfo , DefaultWeightedEdge > classReferencesGraph ;
1717
1818 @ Getter
19- private final Graph <String , DefaultWeightedEdge > packageReferencesGraph ;
19+ private final Graph <VertexInfo , DefaultWeightedEdge > packageReferencesGraph ;
2020
2121 @ Getter
2222 private final Set <String > packagesInCodebase = new HashSet <>();
@@ -25,27 +25,55 @@ public class GraphDependencyCollector implements DependencyCollector {
2525 private final Map <DefaultWeightedEdge , Set <DefaultWeightedEdge >> classRelationshipsInPackageRelationship =
2626 new HashMap <>();
2727
28+ /**
29+ * Class locations (as reported by the visitors) captured during collection,
30+ * keyed by class FQN. The repository-relative locations used for rendering
31+ * are hydrated onto the {@link VertexInfo} vertices from the canonical
32+ * class-to-source-file mapping after the walk (see {@link VertexInfoHydrator});
33+ * this map keeps the raw information captured at collection time.
34+ */
35+ @ Getter
36+ private final Map <String , String > recordedClassLocations = new HashMap <>();
37+
38+ /** Registry of the VertexInfo instances created for each vertex name. */
39+ private final Map <String , VertexInfo > vertexRegistry = new HashMap <>();
40+
41+ /**
42+ * Creates a dependency collector that writes into the supplied graphs.
43+ *
44+ * @param classReferencesGraph the graph receiving class dependencies
45+ * @param packageReferencesGraph the graph receiving cross-package dependencies
46+ */
2847 public GraphDependencyCollector (
29- Graph <String , DefaultWeightedEdge > classReferencesGraph ,
30- Graph <String , DefaultWeightedEdge > packageReferencesGraph ) {
48+ Graph <VertexInfo , DefaultWeightedEdge > classReferencesGraph ,
49+ Graph <VertexInfo , DefaultWeightedEdge > packageReferencesGraph ) {
3150 this .classReferencesGraph = classReferencesGraph ;
3251 this .packageReferencesGraph = packageReferencesGraph ;
3352 }
3453
54+ /**
55+ * Adds or increments a class dependency and records its corresponding package relationship.
56+ * Dependencies from a class to itself are ignored.
57+ *
58+ * @param fromClassFqn the fully qualified source class name
59+ * @param toClassFqn the fully qualified target class name
60+ */
3561 @ Override
3662 public void addClassDependency (String fromClassFqn , String toClassFqn ) {
3763 if (fromClassFqn .equals (toClassFqn )) {
3864 return ;
3965 }
4066
41- classReferencesGraph .addVertex (fromClassFqn );
42- classReferencesGraph .addVertex (toClassFqn );
67+ VertexInfo fromVertex = getOrCreateVertex (fromClassFqn );
68+ VertexInfo toVertex = getOrCreateVertex (toClassFqn );
69+ classReferencesGraph .addVertex (fromVertex );
70+ classReferencesGraph .addVertex (toVertex );
4371
4472 DefaultWeightedEdge classRelationship ;
45- if (!classReferencesGraph .containsEdge (fromClassFqn , toClassFqn )) {
46- classRelationship = classReferencesGraph .addEdge (fromClassFqn , toClassFqn );
73+ if (!classReferencesGraph .containsEdge (fromVertex , toVertex )) {
74+ classRelationship = classReferencesGraph .addEdge (fromVertex , toVertex );
4775 } else {
48- classRelationship = classReferencesGraph .getEdge (fromClassFqn , toClassFqn );
76+ classRelationship = classReferencesGraph .getEdge (fromVertex , toVertex );
4977 classReferencesGraph .setEdgeWeight (
5078 classRelationship , classReferencesGraph .getEdgeWeight (classRelationship ) + 1 );
5179 }
@@ -63,6 +91,13 @@ public void addClassDependency(String fromClassFqn, String toClassFqn) {
6391 }
6492 }
6593
94+ /**
95+ * Adds or increments the package dependency implied by two class names.
96+ *
97+ * @param fromClassFqn the fully qualified source class name
98+ * @param toClassFqn the fully qualified target class name
99+ * @return the package edge, or {@code null} for equal or empty endpoint packages
100+ */
66101 @ Override
67102 public DefaultWeightedEdge addPackageDependency (String fromClassFqn , String toClassFqn ) {
68103 String fromPackageName = getPackageFromFqn (fromClassFqn );
@@ -80,14 +115,16 @@ public DefaultWeightedEdge addPackageDependency(String fromClassFqn, String toCl
80115 return null ;
81116 }
82117
83- packageReferencesGraph .addVertex (fromPackageName );
84- packageReferencesGraph .addVertex (toPackageName );
118+ VertexInfo fromPackageVertex = getOrCreateVertex (fromPackageName );
119+ VertexInfo toPackageVertex = getOrCreateVertex (toPackageName );
120+ packageReferencesGraph .addVertex (fromPackageVertex );
121+ packageReferencesGraph .addVertex (toPackageVertex );
85122
86123 DefaultWeightedEdge packageEdge ;
87- if (!packageReferencesGraph .containsEdge (fromPackageName , toPackageName )) {
88- packageEdge = packageReferencesGraph .addEdge (fromPackageName , toPackageName );
124+ if (!packageReferencesGraph .containsEdge (fromPackageVertex , toPackageVertex )) {
125+ packageEdge = packageReferencesGraph .addEdge (fromPackageVertex , toPackageVertex );
89126 } else {
90- packageEdge = packageReferencesGraph .getEdge (fromPackageName , toPackageName );
127+ packageEdge = packageReferencesGraph .getEdge (fromPackageVertex , toPackageVertex );
91128 packageReferencesGraph .setEdgeWeight (packageEdge , packageReferencesGraph .getEdgeWeight (packageEdge ) + 1 );
92129 }
93130
@@ -102,18 +139,40 @@ protected String getPackageFromFqn(String fqn) {
102139 return fqn .substring (0 , lastIndex );
103140 }
104141
142+ /**
143+ * Returns the registered {@link VertexInfo} for the given class or package
144+ * name, creating and registering one (with the parent namespace derived
145+ * from the name) when absent.
146+ */
147+ private VertexInfo getOrCreateVertex (String name ) {
148+ return vertexRegistry .computeIfAbsent (name , VertexInfo ::forName );
149+ }
150+
151+ /**
152+ * Records the raw source location reported by a visitor for later inspection.
153+ *
154+ * @param classFqn the fully qualified class name
155+ * @param sourceFilePath the visitor-reported source location
156+ */
105157 @ Override
106158 public void recordClassLocation (String classFqn , String sourceFilePath ) {
107- // This will be handled by JavaVisitor which maintains the mapping
159+ // Capture the location reported during collection; repository-relative
160+ // hydration of the VertexInfo vertices happens after the walk.
161+ recordedClassLocations .put (classFqn , sourceFilePath );
108162 }
109163
110164 @ Override
111165 public void registerPackage (String packageName ) {
112166 packagesInCodebase .add (packageName );
113167 }
114168
169+ /**
170+ * Registers a class vertex with its derived parent namespace, even if it has no dependencies.
171+ *
172+ * @param classFqn the fully qualified class name
173+ */
115174 @ Override
116175 public void registerClassVertex (String classFqn ) {
117- classReferencesGraph .addVertex (classFqn );
176+ classReferencesGraph .addVertex (getOrCreateVertex ( classFqn ) );
118177 }
119178}
0 commit comments