diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt index 7fa4440f88..141d1df7a3 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt @@ -113,6 +113,15 @@ class ScopeManager : ScopeProvider { /** The current function, according to the scope that is currently active. */ val currentFunction: FunctionDeclaration? get() = this.firstScopeIsInstanceOrNull()?.astNode as? FunctionDeclaration + + /** + * The current method in the active scope tree, this ensures that 'this' keywords are mapped + * correctly if a method contains a lambda or other types of function declarations + */ + val currentMethod: MethodDeclaration? + get() = + this.firstScopeOrNull { scope: Scope? -> scope?.astNode is MethodDeclaration }?.astNode + as? MethodDeclaration /** The current record, according to the scope that is currently active. */ val currentRecord: RecordDeclaration? get() = this.firstScopeIsInstanceOrNull()?.astNode as? RecordDeclaration diff --git a/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/ExpressionHandler.kt b/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/ExpressionHandler.kt index 6a5f9924d4..18d6fb511d 100644 --- a/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/ExpressionHandler.kt +++ b/cpg-language-java/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/java/ExpressionHandler.kt @@ -36,7 +36,6 @@ import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration import de.fraunhofer.aisec.cpg.frontends.Handler import de.fraunhofer.aisec.cpg.frontends.HandlerInterface import de.fraunhofer.aisec.cpg.graph.* -import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement @@ -771,9 +770,7 @@ class ExpressionHandler(lang: JavaLanguageFrontend) : private fun createImplicitThis(): de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression { val base: de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression - val thisType = - (frontend.scopeManager.currentFunction as? MethodDeclaration?)?.receiver?.type - ?: unknownType() + val thisType = frontend.scopeManager.currentRecord?.toType() ?: unknownType() base = newReference("this", thisType).implicit("this") return base }