Skip to content

Commit a468d5c

Browse files
Add method parameter types
1 parent bf56310 commit a468d5c

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

circular-reference-detector/src/main/java/com/ideacrest/parser/JavaProjectParser.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.github.javaparser.StaticJavaParser;
44
import com.github.javaparser.ast.CompilationUnit;
55
import com.github.javaparser.ast.body.FieldDeclaration;
6+
import com.github.javaparser.ast.body.MethodDeclaration;
7+
import com.github.javaparser.ast.body.Parameter;
68
import java.io.File;
79
import java.io.FileNotFoundException;
810
import java.io.IOException;
@@ -36,13 +38,14 @@ public Graph<String, DefaultEdge> getClassReferences(String srcDirectory) throws
3638
filesStream
3739
.filter(path -> path.getFileName().toString().endsWith(".java"))
3840
.forEach(path -> {
39-
Set<String> instanceVarTypes = getInstanceVarTypes(classNames, path.toFile());
40-
if (!instanceVarTypes.isEmpty()) {
41+
Set<String> varTypes = getInstanceVarTypes(classNames, path.toFile());
42+
varTypes.addAll(getMethodTypes(classNames, path.toFile()));
43+
if (!varTypes.isEmpty()) {
4144
String className =
4245
getClassName(path.getFileName().toString());
4346
classReferencesGraph.addVertex(className);
44-
instanceVarTypes.forEach(classReferencesGraph::addVertex);
45-
instanceVarTypes.forEach(var -> classReferencesGraph.addEdge(className, var));
47+
varTypes.forEach(classReferencesGraph::addVertex);
48+
varTypes.forEach(var -> classReferencesGraph.addEdge(className, var));
4649
}
4750
});
4851
} catch (FileNotFoundException e) {
@@ -54,7 +57,7 @@ public Graph<String, DefaultEdge> getClassReferences(String srcDirectory) throws
5457

5558
/**
5659
* Get instance variables types of a java source file using java parser
57-
* @param classNamesToFilterBy - only add instance variables which have these class names as type
60+
* @param classNamesToFilterBy - only add instance variable types which have these class names as type
5861
* @param file
5962
* @return
6063
*/
@@ -74,6 +77,31 @@ private Set<String> getInstanceVarTypes(List<String> classNamesToFilterBy, File
7477
return new HashSet<>();
7578
}
7679

80+
/**
81+
* Get parameter types of methods declared in a java source file using java parser
82+
* @param classNamesToFilterBy - only add types which have these class names as type
83+
* @param file
84+
* @return
85+
*/
86+
private Set<String> getMethodTypes(List<String> classNamesToFilterBy, File javaSrcFile) {
87+
CompilationUnit compilationUnit;
88+
try {
89+
compilationUnit = StaticJavaParser.parse(javaSrcFile);
90+
return compilationUnit.findAll(MethodDeclaration.class).stream()
91+
.flatMap(f -> f.getParameters().stream()
92+
.map(Parameter::getType)
93+
.filter(type -> !type.isPrimitiveType())
94+
.collect(Collectors.toSet())
95+
.stream())
96+
.map(Object::toString)
97+
.filter(classNamesToFilterBy::contains)
98+
.collect(Collectors.toSet());
99+
} catch (FileNotFoundException e) {
100+
e.printStackTrace();
101+
}
102+
return new HashSet<>();
103+
}
104+
77105
/**
78106
* Get all java classes in a source directory
79107
*

0 commit comments

Comments
 (0)