1515import java .util .stream .Collectors ;
1616import java .util .stream .Stream ;
1717import org .jgrapht .Graph ;
18+ import org .jgrapht .graph .DefaultDirectedWeightedGraph ;
1819import org .jgrapht .graph .DefaultWeightedEdge ;
19- import org .jgrapht .graph .builder .GraphTypeBuilder ;
2020
2121public class JavaProjectParser {
2222
@@ -28,12 +28,7 @@ public class JavaProjectParser {
2828 */
2929 public Graph <String , DefaultWeightedEdge > getClassReferences (String srcDirectory ) throws IOException {
3030 Graph <String , DefaultWeightedEdge > classReferencesGraph =
31- GraphTypeBuilder .<String , DefaultWeightedEdge >directed ()
32- .allowingMultipleEdges (false )
33- .allowingSelfLoops (true )
34- .edgeClass (DefaultWeightedEdge .class )
35- .weighted (true )
36- .buildGraph ();
31+ new DefaultDirectedWeightedGraph <>(DefaultWeightedEdge .class );
3732 if (srcDirectory == null || srcDirectory .isEmpty ()) {
3833 throw new IllegalArgumentException ();
3934 } else {
@@ -42,20 +37,20 @@ public Graph<String, DefaultWeightedEdge> getClassReferences(String srcDirectory
4237 filesStream
4338 .filter (path -> path .getFileName ().toString ().endsWith (".java" ))
4439 .forEach (path -> {
45- Set <String > varTypes = getInstanceVarTypes (classNames , path .toFile ());
46- varTypes .addAll (getMethodTypes (classNames , path .toFile ()));
47- if (!varTypes .isEmpty ()) {
40+ List <String > types = getInstanceVarTypes (classNames , path .toFile ());
41+ types .addAll (getMethodArgumentTypes (classNames , path .toFile ()));
42+ if (!types .isEmpty ()) {
4843 String className =
4944 getClassName (path .getFileName ().toString ());
5045 classReferencesGraph .addVertex (className );
51- varTypes .forEach (classReferencesGraph ::addVertex );
52- varTypes .forEach (varType -> {
53- DefaultWeightedEdge weightedEdge = classReferencesGraph .addEdge (className , varType );
54-
55- // not sure why some edges are null, but let's ignore them for now
56- if ( null != weightedEdge ) {
46+ types .forEach (classReferencesGraph ::addVertex );
47+ types .forEach (type -> {
48+ if (! classReferencesGraph .containsEdge (className , type )) {
49+ classReferencesGraph . addEdge ( className , type );
50+ } else {
51+ DefaultWeightedEdge edge = classReferencesGraph . getEdge ( className , type );
5752 classReferencesGraph .setEdgeWeight (
58- weightedEdge , classReferencesGraph .getEdgeWeight (weightedEdge ) + 1 );
53+ edge , classReferencesGraph .getEdgeWeight (edge ) + 1 );
5954 }
6055 });
6156 }
@@ -65,10 +60,6 @@ public Graph<String, DefaultWeightedEdge> getClassReferences(String srcDirectory
6560 }
6661 }
6762
68- for (DefaultWeightedEdge weightedEdge : classReferencesGraph .edgeSet ()) {
69- System .out .println (weightedEdge .toString () + ":" + classReferencesGraph .getEdgeWeight (weightedEdge ));
70- }
71-
7263 return classReferencesGraph ;
7364 }
7465
@@ -78,7 +69,7 @@ public Graph<String, DefaultWeightedEdge> getClassReferences(String srcDirectory
7869 * @param file
7970 * @return
8071 */
81- private Set <String > getInstanceVarTypes (List <String > classNamesToFilterBy , File javaSrcFile ) {
72+ private List <String > getInstanceVarTypes (List <String > classNamesToFilterBy , File javaSrcFile ) {
8273 CompilationUnit compilationUnit ;
8374 try {
8475 compilationUnit = StaticJavaParser .parse (javaSrcFile );
@@ -87,11 +78,11 @@ private Set<String> getInstanceVarTypes(List<String> classNamesToFilterBy, File
8778 .filter (v -> !v .isPrimitiveType ())
8879 .map (Object ::toString )
8980 .filter (classNamesToFilterBy ::contains )
90- .collect (Collectors .toSet ());
81+ .collect (Collectors .toList ());
9182 } catch (FileNotFoundException e ) {
9283 e .printStackTrace ();
9384 }
94- return new HashSet <>();
85+ return new ArrayList <>();
9586 }
9687
9788 /**
@@ -100,23 +91,23 @@ private Set<String> getInstanceVarTypes(List<String> classNamesToFilterBy, File
10091 * @param file
10192 * @return
10293 */
103- private Set <String > getMethodTypes (List <String > classNamesToFilterBy , File javaSrcFile ) {
94+ private List <String > getMethodArgumentTypes (List <String > classNamesToFilterBy , File javaSrcFile ) {
10495 CompilationUnit compilationUnit ;
10596 try {
10697 compilationUnit = StaticJavaParser .parse (javaSrcFile );
10798 return compilationUnit .findAll (MethodDeclaration .class ).stream ()
10899 .flatMap (f -> f .getParameters ().stream ()
109100 .map (Parameter ::getType )
110101 .filter (type -> !type .isPrimitiveType ())
111- .collect (Collectors .toSet ())
102+ .collect (Collectors .toList ())
112103 .stream ())
113104 .map (Object ::toString )
114105 .filter (classNamesToFilterBy ::contains )
115- .collect (Collectors .toSet ());
106+ .collect (Collectors .toList ());
116107 } catch (FileNotFoundException e ) {
117108 e .printStackTrace ();
118109 }
119- return new HashSet <>();
110+ return new ArrayList <>();
120111 }
121112
122113 /**
0 commit comments