IDEA-120226 ("Class explicitly extends a Collection" inspection should be suppressed when overriding removeEldestEntry() from class LinkedHashMap)

This commit is contained in:
Bas Leijdekkers
2014-02-06 10:42:30 +01:00
parent 6200195a07
commit ccfd1f8de0
3 changed files with 85 additions and 3 deletions
@@ -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.
@@ -15,12 +15,12 @@
*/
package com.siyeh.ig.inheritance;
import com.intellij.psi.PsiAnonymousClass;
import com.intellij.psi.PsiClass;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.CollectionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
public class ExtendsConcreteCollectionInspectionBase extends BaseInspection {
@@ -68,6 +68,24 @@ public class ExtendsConcreteCollectionInspectionBase extends BaseInspection {
if (!CollectionUtils.isCollectionClass(superClass)) {
return;
}
final String qualifiedName = superClass.getQualifiedName();
if ("java.util.LinkedHashMap".equals(qualifiedName)) {
final PsiMethod[] methods = aClass.findMethodsByName("removeEldestEntry", false);
final PsiClassType entryType = TypeUtils.getType("java.util.Map.Entry", aClass);
for (PsiMethod method : methods) {
if (!PsiType.BOOLEAN.equals(method.getReturnType())) {
continue;
}
final PsiParameterList parameterList = method.getParameterList();
if ( parameterList.getParametersCount() != 1) {
continue;
}
final PsiParameter parameter = parameterList.getParameters()[0];
if (entryType.isAssignableFrom(parameter.getType())) {
return;
}
}
}
registerClassError(aClass, superClass, aClass);
}
}
@@ -0,0 +1,15 @@
package com.siyeh.igtest.inheritance.extends_concrete_collection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
class <warning descr="Class 'ExtendsConcreteCollection' explicitly extends 'java.util.ArrayList'">ExtendsConcreteCollection</warning> extends ArrayList {
}
class MyMap extends LinkedHashMap<String, String> {
@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
return true;
}
}
@@ -0,0 +1,49 @@
/*
* 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.inheritance;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
/**
* @author Bas Leijdekkers
*/
public class ExtendsConcreteCollectionInspectionTest extends LightInspectionTestCase {
@Override
protected InspectionProfileEntry getInspection() {
return new ExtendsConcreteCollectionInspection();
}
@Override
protected String[] getEnvironmentClasses() {
return new String[] {
"package java.util;" +
"public class LinkedHashMap<K, V> {" +
" protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {\n" +
" return false;\n" +
" }" +
"}",
"package java.util;" +
"public class ArrayDeque<E> extends AbstractCollection<E> {" +
" public Iterator<E> iterator() { return null; }" +
" public int size() { return 0; }" +
"}"
};
}
public void testExtendsConcreteCollection() { doTest(); }
}