IDEA-171854 Suggest replacing boxed compareTo with convenience methods from Java 7

This commit is contained in:
Tagir Valeev
2017-04-24 17:41:21 +07:00
parent 7bd221e166
commit 54eb5b2d2b
8 changed files with 257 additions and 0 deletions
@@ -0,0 +1,14 @@
// "Fix all 'Unnecessary boxing to compare primitives' problems in file" "true"
public class Test {
public void test(int a, int b) {
if(Integer.compare(a, b) > 0) {
System.out.println(1);
}
if(Long.compare(a, b) > 0) {
System.out.println(1);
}
if(Double.compare(a, b) > 0) {
System.out.println(1);
}
}
}
@@ -0,0 +1,11 @@
// "Fix all 'Unnecessary boxing to compare primitives' problems in file" "true"
public class Test {
public int test(String s1, String s2) {
int res = Integer.compare(s1.length(), s2.length());
if(res == 0) {
/*reverse order!*/
res = Character.compare(s2.charAt(0), s1.charAt(0));
}
return res;
}
}
@@ -0,0 +1,14 @@
// "Fix all 'Unnecessary boxing to compare primitives' problems in file" "true"
public class Test {
public void test(int a, int b) {
if(((Integer)a).compa<caret>reTo(b) > 0) {
System.out.println(1);
}
if(Long.valueOf(a).compareTo(Long.valueOf(b)) > 0) {
System.out.println(1);
}
if(new Double(a).compareTo(new Double(b)) > 0) {
System.out.println(1);
}
}
}
@@ -0,0 +1,10 @@
// "Fix all 'Unnecessary boxing to compare primitives' problems in file" "true"
public class Test {
public int test(String s1, String s2) {
int res = new Integer(s1.length()).co<caret>mpareTo(s2.length());
if(res == 0) {
res = new Character(/*reverse order!*/s2.charAt(0)).compareTo(s1.charAt(0));
}
return res;
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2000-2017 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.BoxForComparisonInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import org.jetbrains.annotations.NotNull;
public class BoxForComparisonInspectionTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new BoxForComparisonInspection(),
};
}
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/boxForComparison";
}
}