Skip to content

Commit 3c84fe0

Browse files
committed
debug: Add some logger.
1 parent ef8fa50 commit 3c84fe0

1 file changed

Lines changed: 84 additions & 34 deletions

File tree

src/main/kotlin/org/skgroup/securityinspector/analysis/graphs/callgraph/CallGraphBuilder.kt

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.skgroup.securityinspector.analysis.graphs.callgraph
33
import com.intellij.openapi.application.ApplicationManager
44
import com.intellij.openapi.project.Project
55
import com.intellij.psi.*
6+
import com.intellij.psi.search.GlobalSearchScope
67
import com.intellij.psi.search.searches.ReferencesSearch
78
import com.intellij.psi.util.PsiTreeUtil
89
import org.skgroup.securityinspector.analysis.ast.nodes.MethodNode
@@ -44,7 +45,8 @@ class CallGraphBuilder : JavaRecursiveElementVisitor() {
4445

4546
// 查找对该方法的所有引用,建立反向调用关系
4647
// TODO 这里的逻辑我自己也混乱了,可能会有问题,后面再看
47-
ReferencesSearch.search(method, method.useScope).forEach { reference ->
48+
val scope = GlobalSearchScope.projectScope(method.project)
49+
ReferencesSearch.search(method, scope).forEach { reference ->
4850
val callerMethod = PsiTreeUtil.getParentOfType(reference.element, PsiMethod::class.java)
4951
?: return@forEach
5052

@@ -74,26 +76,50 @@ class CallGraphBuilder : JavaRecursiveElementVisitor() {
7476
* @param expression 方法调用表达式
7577
*/
7678
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
77-
super.visitMethodCallExpression(expression)
78-
if (currentMethodStack.isEmpty()) {
79-
return
80-
}
79+
if (currentMethodStack.isEmpty()) return
8180

8281
val caller = currentMethodStack.peek()
83-
8482
// 解析被调用的方法
85-
val resolvedMethod = expression.resolveMethod() ?: return
86-
val calleeMethodNode = GraphUtils.getMethodNode(resolvedMethod, expression)
87-
88-
// 将 callee 加入节点集合
89-
callGraph.nodes.add(calleeMethodNode)
90-
91-
// 在 callGraph 中记录调用关系 (caller -> callee)
92-
callGraph.edges
93-
.getOrPut(caller) { mutableSetOf() }
94-
.add(calleeMethodNode)
95-
96-
handleIoCContainerCall(expression, caller, calleeMethodNode)
83+
val resolvedMethod = expression.resolveMethod()
84+
// 调试日志
85+
if (resolvedMethod == null) {
86+
println("method call expression $expression can not be resolve.")
87+
return
88+
}
89+
val clazz: PsiClass? = resolvedMethod.containingClass
90+
expression.reference?.let {
91+
val calleeMethodNode = GraphUtils.getMethodNode(resolvedMethod, it)
92+
// 将 callee 加入节点集合
93+
callGraph.nodes.add(calleeMethodNode)
94+
// 在 callGraph 中记录调用关系 (caller -> callee)
95+
callGraph.edges
96+
.getOrPut(caller) { mutableSetOf() }
97+
.add(calleeMethodNode)
98+
handleIoCContainerCall(expression, caller, calleeMethodNode)
99+
} ?: run {
100+
// val calleeMethodNode = GraphUtils.getMethodNode(resolvedMethod)
101+
// callGraph.nodes.add(calleeMethodNode)
102+
// callGraph.edge
103+
// .getOrPut(caller) { mutableSetOf() }
104+
// .add(calleeMethodNode)
105+
// handleIoCContainerCall(expression, caller, calleeMethodNode)
106+
if (clazz != null) {
107+
if (clazz.isInterface) {
108+
println("$clazz is an interface")
109+
println("and the method call expression $expression can not be resolve.")
110+
}
111+
if (clazz.hasModifierProperty(PsiModifier.ABSTRACT)) {
112+
println("$clazz is an abstract class")
113+
println("and the method call expression $expression can not be resolve.")
114+
}
115+
}
116+
if (PsiTreeUtil.getParentOfType(expression, PsiLambdaExpression::class.java) != null) {
117+
println("in lambda method call expression $expression can not be resolve.")
118+
} else {
119+
println("unknown method call expression $expression can not be resolve.")
120+
}
121+
}
122+
super.visitMethodCallExpression(expression)
97123
}
98124

99125
/**
@@ -102,24 +128,48 @@ class CallGraphBuilder : JavaRecursiveElementVisitor() {
102128
* @param expression
103129
*/
104130
override fun visitNewExpression(expression: PsiNewExpression) {
105-
super.visitNewExpression(expression)
106-
if (currentMethodStack.isEmpty()) {
107-
return
108-
}
109-
val callerMethodNode = currentMethodStack.peek()
131+
if (currentMethodStack.isEmpty()) return
110132

133+
val callerMethodNode = currentMethodStack.peek()
111134
// 解析构造方法
112-
val constructor = expression.resolveConstructor() ?: return
113-
val calleeMethodNode = GraphUtils.getMethodNode(constructor, expression)
114-
115-
// 将 callee 加入节点集合
116-
callGraph.nodes.add(calleeMethodNode)
117-
118-
// 在 callGraph 中记录 (caller -> callee构造方法)
119-
callGraph
120-
.edges
121-
.getOrPut(callerMethodNode) { mutableSetOf() }
122-
.add(calleeMethodNode)
135+
val constructor = expression.resolveConstructor()
136+
// 调试日志
137+
if (constructor == null) {
138+
println("new expression $constructor can not be resolve.")
139+
return
140+
}
141+
val clazz: PsiClass? = constructor.containingClass
142+
expression.reference?.let {
143+
val calleeMethodNode = GraphUtils.getMethodNode(constructor, it)
144+
// 将 callee 加入节点集合
145+
callGraph.nodes.add(calleeMethodNode)
146+
// 在 callGraph 中记录 (caller -> callee构造方法)
147+
callGraph.edges
148+
.getOrPut(callerMethodNode) { mutableSetOf() }
149+
.add(calleeMethodNode)
150+
} ?: run {
151+
// val calleeMethodNode = GraphUtils.getMethodNode(constructor)
152+
// callGraph.nodes.add(calleeMethodNode)
153+
// callGraph.edges
154+
// .getOrPut(callerMethodNode) { mutableSetOf() }
155+
// .add(calleeMethodNode)
156+
if (clazz != null) {
157+
if (clazz.isInterface) {
158+
println("$clazz is an interface")
159+
println("and the method call expression $expression can not be resolve.")
160+
}
161+
if (clazz.hasModifierProperty(PsiModifier.ABSTRACT)) {
162+
println("$clazz is an abstract class")
163+
println("and the method call expression $expression can not be resolve.")
164+
}
165+
}
166+
if (PsiTreeUtil.getParentOfType(expression, PsiLambdaExpression::class.java) != null) {
167+
println("in lambda method call expression $expression can not be resolve.")
168+
} else {
169+
println("unknown new expression $constructor can not be resolve.")
170+
}
171+
}
172+
super.visitNewExpression(expression)
123173
}
124174

125175
/**

0 commit comments

Comments
 (0)