diff --git a/java/ql/lib/change-notes/2022-12-09-default-extension-methods.md b/java/ql/lib/change-notes/2022-12-09-default-extension-methods.md index f9335b2670d..df9d23503da 100644 --- a/java/ql/lib/change-notes/2022-12-09-default-extension-methods.md +++ b/java/ql/lib/change-notes/2022-12-09-default-extension-methods.md @@ -1,4 +1,4 @@ --- category: minorAnalysis --- -* The extraction of extension methods have been improved when default parameter values are present. The dispatch and extension receiver parameters are extracted in the correct order. The `ExtensionMethod::getExtensionParameterIndex` predicate has been introduced to facilitate getting the correct extension parameter index. +* The extraction of Kotlin extension methods has been improved when default parameter values are present. The dispatch and extension receiver parameters are extracted in the correct order. The `ExtensionMethod::getExtensionReceiverParameterIndex` predicate has been introduced to facilitate getting the correct extension parameter index. diff --git a/java/ql/lib/semmle/code/java/Member.qll b/java/ql/lib/semmle/code/java/Member.qll index d0e7de8a9ac..d55a0eefdec 100644 --- a/java/ql/lib/semmle/code/java/Member.qll +++ b/java/ql/lib/semmle/code/java/Member.qll @@ -820,7 +820,7 @@ class ExtensionMethod extends Method { * extension methods that are defined as members, the index is 1. Index 0 is the dispatch receiver of the `$default` * method. */ - int getExtensionParameterIndex() { + int getExtensionReceiverParameterIndex() { if exists(Method src | this = src.getKotlinParameterDefaultsProxy() and diff --git a/java/ql/lib/semmle/code/java/Variable.qll b/java/ql/lib/semmle/code/java/Variable.qll index 75c7501c8a7..82314824395 100644 --- a/java/ql/lib/semmle/code/java/Variable.qll +++ b/java/ql/lib/semmle/code/java/Variable.qll @@ -91,7 +91,7 @@ class Parameter extends Element, @param, LocalScopeVariable { /** Holds if this formal parameter is a parameter representing the dispatch receiver in an extension method. */ predicate isExtensionParameter() { - this.getPosition() = this.getCallable().(ExtensionMethod).getExtensionParameterIndex() + this.getPosition() = this.getCallable().(ExtensionMethod).getExtensionReceiverParameterIndex() } /** diff --git a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll index 775b3828a67..14445971b47 100644 --- a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll +++ b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll @@ -299,7 +299,8 @@ private class PathNormalizeSanitizer extends MethodAccess { */ private Expr getVisualQualifier(MethodAccess ma) { if ma.getMethod() instanceof ExtensionMethod - then result = ma.getArgument(ma.getMethod().(ExtensionMethod).getExtensionParameterIndex()) + then + result = ma.getArgument(ma.getMethod().(ExtensionMethod).getExtensionReceiverParameterIndex()) else result = ma.getQualifier() } @@ -313,7 +314,8 @@ private Argument getVisualArgument(MethodAccess ma, int argPos) { if ma.getMethod() instanceof ExtensionMethod then result = - ma.getArgument(argPos + ma.getMethod().(ExtensionMethod).getExtensionParameterIndex() + 1) + ma.getArgument(argPos + ma.getMethod().(ExtensionMethod).getExtensionReceiverParameterIndex() + + 1) else result = ma.getArgument(argPos) } diff --git a/java/ql/test/kotlin/library-tests/methods/methods.ql b/java/ql/test/kotlin/library-tests/methods/methods.ql index 7042cfd48cf..0f0b43f4e4b 100644 --- a/java/ql/test/kotlin/library-tests/methods/methods.ql +++ b/java/ql/test/kotlin/library-tests/methods/methods.ql @@ -29,7 +29,7 @@ query predicate extensionsMismatch(Method src, Method def) { query predicate extensionIndex(ExtensionMethod m, int i, Type t) { m.fromSource() and - m.getExtensionParameterIndex() = i and + m.getExtensionReceiverParameterIndex() = i and m.getExtendedType() = t and m.getParameter(i).getType() = t }