"Logger initialized with foreign class" inspection should also warn in anonymous classes

This commit is contained in:
Bas Leijdekkers
2014-08-13 21:55:42 +02:00
parent 74a06f6305
commit 03f0231eff
4 changed files with 73 additions and 4 deletions
@@ -1700,7 +1700,7 @@ this.class=this class
assertequals.may.be.assertsame.display.name='assertEquals()' may be 'assertSame()'
assertequals.may.be.assertsame.problem.descriptor=<code>#ref()</code> may be 'assertSame()' #loc
logger.initialized.with.foreign.class.quickfix=Replace with ''{0}.class''
logger.initialized.with.foreign.class.problem.descriptor=Logger initializer with foreign class <code>#ref</code> #loc
logger.initialized.with.foreign.class.problem.descriptor=Logger initialized with foreign class <code>#ref</code> #loc
logger.initialized.with.foreign.class.display.name=Logger initialized with foreign class
logger.factory.method.name=Logger factory method name
logger.factory.class.name=Logger factory class name
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -20,13 +20,13 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ClassUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -46,6 +46,11 @@ public class LoggerInitializedWithForeignClassInspectionBase extends BaseInspect
@SuppressWarnings({"PublicField"})
public String loggerFactoryMethodName = DEFAULT_FACTORY_METHOD_NAMES;
public LoggerInitializedWithForeignClassInspectionBase() {
parseString(DEFAULT_LOGGER_CLASS_NAMES, loggerFactoryClassNames);
parseString(DEFAULT_FACTORY_METHOD_NAMES, loggerFactoryMethodNames);
}
@Override
@NotNull
public String getDisplayName() {
@@ -157,7 +162,10 @@ public class LoggerInitializedWithForeignClassInspectionBase extends BaseInspect
if (expressions.length != 1) {
return;
}
final PsiClass containingClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
PsiClass containingClass = ClassUtils.getContainingClass(expression);
while (containingClass instanceof PsiAnonymousClass) {
containingClass = ClassUtils.getContainingClass(containingClass);
}
if (containingClass == null) {
return;
}
@@ -0,0 +1,13 @@
import java.util.logging.*;
class LoggerInitializedWithForeignClass {
void foo() {
new Object() {
void bar() {
Logger.getLogger(LoggerInitializedWithForeignClass.class.getName());
Logger.getLogger(<warning descr="Logger initialized with foreign class 'String.class'">String.class</warning>.getName());
}
};
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2014 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.logging;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class LoggerInitializedWithForeignClassInspectionTest extends LightInspectionTestCase {
public void testLoggerInitializedWithForeignClass() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new LoggerInitializedWithForeignClassInspection();
}
@Override
protected String[] getEnvironmentClasses() {
return new String[] {
"package java.util.logging;" +
"public class Logger {" +
" public static Logger getLogger(String name) {" +
" return null;" +
" }" +
"}"
};
}
}