intellij-community icon indicating copy to clipboard operation
intellij-community copied to clipboard

[kotlin] K2 J2K: Port JavaMapForEachInspection to BuiltinMembersConversion

Open ermattt opened this issue 1 year ago • 1 comments
trafficstars

NB: The test testJava8MapForEachWithFullJdk still fails, but I thought I'd put up a PR to get feedback before I dig deeper and/or start moving tests around.

I was able to switch the expression to use the more Kotlin-y curly braces (i.e. from map.forEach((key, value) -> foo(key, value)) to map.forEach { key: String?, value: String? -> foo(key, value) }), but not to use the destructuring (i.e. key: String?, value: String? vs. (key: String?, value: String?)).

If I understand correctly, I think that change might be impossible without 2 refactorings to JKTree classes:

  1. Add a JKTree class that's analogous to KtDestructuringDeclaration
  2. Modify JKParameter to also have a JKDestructuringDeclaration property (or something similar)

Is there another way to do this that I'm missing?

internal class Test {
    fun test(map: HashMap<String?, String?>) {
        map.forEach { key: String?, value: String? -> foo(key, value) }
    }

    fun foo(key: String?, value: String?) {
    }
}

@darthorimar @abelkov @jocelynluizzi13

ermattt avatar Jan 25 '24 22:01 ermattt

Thank you.

AFAIU your current withArgumentsProvider block doesn't really do anything: it just creates another lambda expression with the same parameters. It looks like we indeed need a new JK tree element to represent a (restricted form of) KtDestructuringDeclaration specifically to support this case.

You may be able to inherit this JKDestructuringParameter from JKParameter to not change the constructor of JKLambdaExpression.

I was able to switch the expression to use the more Kotlin-y curly braces

Well, the curly braces are printed as usual in JKCodeBuilder.Visitor#visitLambdaExpressionRaw. If you only remove the post-processing, without adding any other code, the test fails in exactly the same way.

After you add a new JK tree node, you will be able to print it specially (inside parentheses) in visitLambdaExpressionRaw.

abelkov avatar Jan 26 '24 12:01 abelkov

Replaced by another PR

ermattt avatar Mar 27 '24 19:03 ermattt