IDEA-100038 (slf4j paramerization quickfix bug)

This commit is contained in:
Bas Leijdekkers
2013-03-01 16:01:36 +01:00
parent f67c3e8f20
commit 11696cad24
6 changed files with 175 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -28,6 +28,7 @@ import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -40,6 +41,7 @@ import java.util.Set;
*/
public class StringConcatenationArgumentToLogCallInspection extends BaseInspection {
@NonNls
private static final Set<String> logNames = new THashSet<String>();
static {
logNames.add("trace");
@@ -95,8 +97,8 @@ public class StringConcatenationArgumentToLogCallInspection extends BaseInspecti
if (arguments.length == 0) {
return;
}
final StringBuilder newMethodCall = new StringBuilder(methodCallExpression.getMethodExpression().getText());
newMethodCall.append("(");
@NonNls final StringBuilder newMethodCall = new StringBuilder(methodCallExpression.getMethodExpression().getText());
newMethodCall.append('(');
PsiExpression argument = arguments[0];
int usedArguments;
if (!(argument instanceof PsiPolyadicExpression)) {
@@ -137,7 +139,7 @@ public class StringConcatenationArgumentToLogCallInspection extends BaseInspecti
boolean inStringLiteral = false;
for (PsiExpression operand : operands) {
if (ExpressionUtils.isEvaluatedAtCompileTime(operand)) {
if (ExpressionUtils.hasStringType(operand)) {
if (ExpressionUtils.hasStringType(operand) && operand instanceof PsiLiteralExpression) {
final String text = operand.getText();
final int count = StringUtil.getOccurrenceCount(text, "{}");
for (int i = 0; i < count && usedArguments + i < arguments.length; i++) {

View File

@@ -0,0 +1,11 @@
import org.slf4j.*;
class UseOfConstant {
void foo() {
Logger logger = LoggerFactory.getLogger(UseOfConstant.class);
final String CONST = "const";
String var = "var";
logger.info("string {}" + CONST, var);
}
}

View File

@@ -0,0 +1,11 @@
import org.slf4j.*;
class UseOfConstant {
void foo() {
Logger logger = LoggerFactory.getLogger(UseOfConstant.class);
final String CONST = "const";
String var = "var";
logger.in<caret>fo("string " + var + CONST);
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2000-2013 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.siyeh.ig;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
/**
* @author Bas Leijdekkers
*/
public abstract class LightInspectionTestCase extends LightCodeInsightFixtureTestCase {
@Override
protected void setUp() throws Exception {
System.out.println("LightInspectionTestCase.setUp()");
super.setUp();
for (String environmentClass : getEnvironmentClasses()) {
System.out.println("environmentClass = " + environmentClass);
myFixture.addClass(environmentClass);
}
myFixture.enableInspections(getInspection());
}
protected abstract LocalInspectionTool getInspection();
@NonNls
protected String[] getEnvironmentClasses() {
return new String[]{};
}
protected final void doTest(@Language("JAVA") @NotNull @NonNls String classText) {
@NonNls final StringBuilder newText = new StringBuilder();
int start = 0;
int end = classText.indexOf("/*");
while (end >= 0) {
newText.append(classText, start, end);
start = end + 2;
end = classText.indexOf("*/", end);
if (end < 0) {
throw new IllegalArgumentException("invalid class text");
}
final String warning = classText.substring(start, end);
if (warning.isEmpty()) {
newText.append("</warning>");
} else {
newText.append("<warning descr=\"").append(warning).append("\">");
}
start = end + 2;
end = classText.indexOf("/*", end + 1);
}
newText.append(classText, start, classText.length());
myFixture.configureByText("X.java", newText.toString());
myFixture.testHighlighting(true, false, false);
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2000-2013 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.siyeh.ig.fixes.logging;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.logging.StringConcatenationArgumentToLogCallInspection;
public class StringConcatenationArgumentToLogCallFixTest extends IGQuickFixesTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
myDefaultHint = InspectionGadgetsBundle.message("string.concatenation.in.format.call.quickfix");
myFixture.addClass("package org.slf4j; public interface Logger { void info(String format); }");
myFixture.addClass("package org.slf4j; public class LoggerFactory { public static Logger getLogger(Class clazz) { return null; }}");
myFixture.enableInspections(new StringConcatenationArgumentToLogCallInspection());
}
public void testUseOfConstant() {
doTest();
}
@Override
protected String getRelativePath() {
return "logging/string_concatenation_argument_to_log_call";
}
}

View File

@@ -0,0 +1,31 @@
package com.siyeh.ig.logging;
import com.intellij.codeInspection.LocalInspectionTool;
import com.siyeh.ig.LightInspectionTestCase;
public class StringConcatenationArgumentToLogCallInspectionTest extends LightInspectionTestCase {
@Override
protected LocalInspectionTool getInspection() {
return new StringConcatenationArgumentToLogCallInspection();
}
@Override
protected String[] getEnvironmentClasses() {
return new String[]{
"package org.slf4j; public interface Logger { void debug(String format); }",
"package org.slf4j; public class LoggerFactory { public static Logger getLogger(Class clazz) { return null; }}"};
}
public void testBasic() {
doTest("import org.slf4j.*;\n" +
"class X {\n" +
" void foo() {\n" +
" Logger logger = LoggerFactory.getLogger(X.class);\n" +
" final String CONST = \"const\";\n" +
" String var = \"var\";\n" +
" logger./*Non-constant string concatenation as argument to 'debug()' logging call*/debug/**/(\"string \" + var + CONST);\n" +
" }\n" +
"}"
);
}
}