New "Return of instance of anonymous, local or inner class" inspection

This commit is contained in:
Bas Leijdekkers
2014-09-12 16:05:47 +02:00
parent 30f03aa7f6
commit 033e892e79
5 changed files with 169 additions and 0 deletions
@@ -1547,6 +1547,9 @@
key="inner.class.may.be.static.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.memory.issues" enabledByDefault="false" level="WARNING" runForWholeFile="true"
implementationClass="com.siyeh.ig.memory.InnerClassMayBeStaticInspection"/>
<localInspection language="JAVA" shortName="ReturnOfInnerClass" bundle="com.siyeh.InspectionGadgetsBundle"
key="return.of.inner.class.display.name" groupBundle="messages.InspectionsBundle"
implementationClass="com.siyeh.ig.memory.ReturnOfInnerClassInspection" enabledByDefault="false" level="WARNING"/>
<localInspection language="JAVA" shortName="StaticCollection" bundle="com.siyeh.InspectionGadgetsBundle" key="static.collection.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.memory.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.memory.StaticCollectionInspection"/>
@@ -2117,3 +2117,7 @@ junit3.method.naming.convention.problem.descriptor.regex.mismatch=JUnit 3 test m
introduce.holder.class.quickfix=Introduce holder class
double.brace.initialization.display.name=Double brace initialization
double.brace.initialization.quickfix=Replace with regular initialization
return.of.inner.class.display.name=Return of instance of anonymous, local or inner class
return.of.anonymous.class.problem.descriptor=Return of instance of anonymous class #loc
return.of.local.class.problem.descriptor=Return of instance of local class <code>{0}</code> #loc
return.of.inner.class.problem.descriptor=Return of instance of non-static inner class <code>{0}</code> #loc
@@ -0,0 +1,107 @@
/*
* 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.memory;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class ReturnOfInnerClassInspection extends BaseInspection {
private enum ClassType { ANONYMOUS_CLASS, LOCAL_CLASS, INNER_CLASS }
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("return.of.inner.class.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
switch ((ClassType)infos[0]) {
case ANONYMOUS_CLASS:
return InspectionGadgetsBundle.message("return.of.anonymous.class.problem.descriptor");
case LOCAL_CLASS: {
final PsiClass aClass = (PsiClass)infos[1];
return InspectionGadgetsBundle.message("return.of.local.class.problem.descriptor", aClass.getName());
}
case INNER_CLASS: {
final PsiClass aClass = (PsiClass)infos[1];
return InspectionGadgetsBundle.message("return.of.inner.class.problem.descriptor", aClass.getName());
}
default:
throw new UnsupportedOperationException();
}
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new ReturnOfInnerClassVisitor();
}
private static class ReturnOfInnerClassVisitor extends BaseInspectionVisitor {
@Override
public void visitReturnStatement(PsiReturnStatement statement) {
super.visitReturnStatement(statement);
final PsiExpression expression = ParenthesesUtils.stripParentheses(statement.getReturnValue());
if (expression == null) {
return;
}
final PsiMethod method = PsiTreeUtil.getParentOfType(statement, PsiMethod.class, true, PsiLambdaExpression.class);
if (method == null || method.hasModifierProperty(PsiModifier.PRIVATE)) {
return;
}
if (expression instanceof PsiNewExpression) {
final PsiNewExpression newExpression = (PsiNewExpression)expression;
final PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass();
if (anonymousClass != null) {
registerStatementError(statement, ClassType.ANONYMOUS_CLASS);
return;
}
}
final PsiType type = expression.getType();
if (!(type instanceof PsiClassType)) {
return;
}
final PsiClassType classType = (PsiClassType)type;
final PsiClass aClass = classType.resolve();
if (aClass == null) {
return;
}
if (PsiUtil.isLocalClass(aClass)) {
registerStatementError(statement, ClassType.LOCAL_CLASS, aClass);
return;
}
final PsiClass containingClass = aClass.getContainingClass();
if (containingClass == null || aClass.hasModifierProperty(PsiModifier.STATIC)) {
return;
}
registerStatementError(statement, ClassType.INNER_CLASS, aClass);
}
}
}
@@ -0,0 +1,20 @@
public class ReturnOfInnerClass {
public Object one() {
<warning descr="Return of instance of anonymous class">return</warning> new Object() {};
}
public Object two() {
class A {}
<warning descr="Return of instance of local class 'A'">return</warning> new A();
}
class B {}
public Object three() {
<warning descr="Return of instance of non-static inner class 'B'">return</warning> new B();
}
private Object four() {
return new B();
}
}
@@ -0,0 +1,35 @@
/*
* 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.memory;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import junit.framework.TestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class ReturnOfInnerClassInspectionTest extends LightInspectionTestCase {
public void testReturnOfInnerClass() { doTest(); }
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new ReturnOfInnerClassInspection();
}
}