Java8MapApiInspection: better name generation for lambda argument (IDEA-CR-16359)

This commit is contained in:
Tagir Valeev
2016-11-30 18:20:33 +07:00
parent 0780ecc65b
commit 97f6252f82
5 changed files with 114 additions and 8 deletions
@@ -29,7 +29,6 @@ import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ig.psiutils.*;
import one.util.streamex.IntStreamEx;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
@@ -383,13 +382,7 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
refs = StreamEx.of(PsiTreeUtil.collectElementsOfType(value, PsiReferenceExpression.class))
.filter(ref -> ref.getQualifierExpression() == null && ref.isReferenceTo(element)).toList();
if (!refs.isEmpty()) {
String name = ((PsiReferenceExpression)key).getReferenceName();
// like "myVariableName" => "mvn"
nameCandidate = IntStreamEx.ofChars(name).mapFirst(Character::toUpperCase).filter(Character::isUpperCase).charsToString()
.toLowerCase(Locale.ENGLISH);
if (nameCandidate.isEmpty()) {
nameCandidate = "k";
}
nameCandidate = getNameCandidate(((PsiReferenceExpression)key).getReferenceName());
}
}
}
@@ -418,6 +411,29 @@ public class Java8MapApiInspection extends BaseJavaBatchLocalInspectionTool {
}
CodeStyleManager.getInstance(project).reformat(result);
}
@NotNull
private static String getNameCandidate(String name) {
// Either last uppercase letter (if it's not the last letter) or the first letter, removing leading underscores
// token -> t
// myAccessToken -> t
// SQL -> s
// __name -> n
// __1 -> k
String nameCandidate;
name = name.replaceFirst("^[_\\d]+", "");
if (name.isEmpty()) return "k";
nameCandidate = name.substring(0, 1);
for (int pos = name.length() - 1; pos > 0; pos--) {
if (Character.isUpperCase(name.charAt(pos))) {
if (pos != name.length() - 1) {
nameCandidate = name.substring(pos, pos + 1);
}
break;
}
}
return nameCandidate.toLowerCase(Locale.ENGLISH);
}
}
class MapCheckCondition {
@@ -0,0 +1,21 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
static class MyItem {
String k;
int i;
MyItem(String k, int i) {
this.k = k;
this.i = i;
}
}
public MyItem testMap(Map<String, MyItem> map, String localUser) {
MyItem item = map.computeIfAbsent(localUser, u -> new MyItem(u, 1));
return item;
}
}
@@ -0,0 +1,21 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
static class MyItem {
String k;
int i;
MyItem(String k, int i) {
this.k = k;
this.i = i;
}
}
public MyItem testMap(Map<String, MyItem> map, String ___1) {
MyItem item = map.computeIfAbsent(___1, k -> new MyItem(k, 1));
return item;
}
}
@@ -0,0 +1,24 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
static class MyItem {
String k;
int i;
MyItem(String k, int i) {
this.k = k;
this.i = i;
}
}
public MyItem testMap(Map<String, MyItem> map, String localUser) {
MyItem item = map.get(localUser);
if(item == nul<caret>l) {
map.put(localUser, item = new MyItem(localUser, 1));
}
return item;
}
}
@@ -0,0 +1,24 @@
// "Replace with 'computeIfAbsent' method call" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
static class MyItem {
String k;
int i;
MyItem(String k, int i) {
this.k = k;
this.i = i;
}
}
public MyItem testMap(Map<String, MyItem> map, String ___1) {
MyItem item = map.get(___1);
if(item == nul<caret>l) {
map.put(___1, item = new MyItem(___1, 1));
}
return item;
}
}