IDEA-161007 Add new inspection to make comparator lambdas use Comparator.comparing() combinators (Currently only simple comparators supported)

This commit is contained in:
Tagir Valeev
2016-09-12 17:50:47 +07:00
parent 34ed877b9d
commit dc37f19e2b
9 changed files with 252 additions and 34 deletions
@@ -0,0 +1,14 @@
// "Replace with Comparator.comparing" "true"
import java.util.Comparator;
import java.util.List;
public class Main {
interface Person {
String getName();
}
void sort(List<Person> persons) {
persons.sort(Comparator.comparing(Person::getName));
}
}
@@ -0,0 +1,13 @@
// "Replace with Comparator.comparing" "false"
import java.util.List;
public class Main {
interface Person {
String getName();
}
void sort(List<Person> persons) {
persons.sort((p1, p2) -> p2.getNam<caret>e().compareTo(p1.getName()));
}
}
@@ -0,0 +1,13 @@
// "Replace with Comparator.comparing" "true"
import java.util.List;
public class Main {
interface Person {
String getName();
}
void sort(List<Person> persons) {
persons.sort((p1, p2) -> p1.getNam<caret>e().compareTo(p2.getName()));
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.ComparatorCombinatorsInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import org.jetbrains.annotations.NotNull;
public class ComparatorCombinatorsInspectionTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new ComparatorCombinatorsInspection()
};
}
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCombinators";
}
}