Was suggested I share this here but not sure what ...
# random
z
Was suggested I share this here but not sure what the best channel is If anyone wants a structural replace template for migrating to
Enum.entries
in Kotlin 1.9, here's one I wrote. You can make it an inspection too Template
Copy code
$Before$.$Method$()
For
$Before$
, add a script modifier with this script. This was the only way I could actually tell if a class was an enum or not, no combination of type hierarchy checks or regexes would work.
Copy code
import com.intellij.psi.*;
import org.jetbrains.kotlin.psi.*;

public boolean isReferencedClassEnum(KtDotQualifiedExpression expression) {
    KtExpression receiverExpression = expression.getReceiverExpression();
    if (receiverExpression instanceof KtNameReferenceExpression) {
        PsiElement resolved = ((KtNameReferenceExpression) receiverExpression).getReference().resolve();
        if (resolved instanceof KtClass) {
            return ((KtClass) resolved).isEnum();
        }
    }
    return false;
}

if (__context__ instanceof KtDotQualifiedExpression) {
    isReferencedClassEnum((KtDotQualifiedExpression) __context__)
} else {
    false
}
Here's a pasteable template you can import from clipboard
Copy code
<replaceConfiguration name="Replace Enum.values() with Enum.entries" text="$Before$.$Method$()" recursive="false" type="Kotlin" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="$Before$.entries" case_sensitive="true">
  <constraint name="__context__" within="" contains="" />
  <constraint name="Before" script="&quot;import com.intellij.psi.*;&#10;import org.jetbrains.kotlin.psi.*;&#10;&#10;public boolean isReferencedClassEnum(KtDotQualifiedExpression expression) {&#10;    KtExpression receiverExpression = expression.getReceiverExpression();&#10;    if (receiverExpression instanceof KtNameReferenceExpression) {&#10;        PsiElement resolved = ((KtNameReferenceExpression) receiverExpression).getReference().resolve();&#10;        if (resolved instanceof KtClass) {&#10;            return ((KtClass) resolved).isEnum();&#10;        }&#10;    }&#10;    return false;&#10;}&#10;&#10;if (__context__ instanceof KtDotQualifiedExpression) {&#10;    isReferencedClassEnum((KtDotQualifiedExpression) __context__)&#10;} else {&#10;    false&#10;}&quot;" within="" contains="" />
  <constraint name="Method" regexp="values" within="" contains="" />
</replaceConfiguration>
To make it go faster if you're trying to do one big migration, I suggest doing a simple string search for
.values()
repo-wide, then run the inspection with the scope of the "Files in last search result"
e
I was able to get away with replacing
values()
with
entries
through Replace in files but YMMV
e
I've used structural replace with "is subclass of Enum" before, I'm pretty sure… maybe it broke more recently
z
¯\_(ツ)_/¯ yeah I was unsuccessful. The script approach worked though
the values() replace definitely a YMMV thing. In a codebase with a lot of rxjava tests that was a no-go heh
😆 1
c
noiceee