IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / bugs / InstanceofIncompatibleInterfaceInspection.java
blobe00169844487922fe360bc162f34c422e2720a46
1 /*
2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.siyeh.ig.bugs;
18 import com.intellij.psi.*;
19 import com.siyeh.InspectionGadgetsBundle;
20 import com.siyeh.ig.BaseInspection;
21 import com.siyeh.ig.BaseInspectionVisitor;
22 import com.siyeh.ig.psiutils.InheritanceUtil;
23 import org.jetbrains.annotations.NotNull;
25 public class InstanceofIncompatibleInterfaceInspection
26 extends BaseInspection {
28 @NotNull
29 public String getDisplayName(){
30 return InspectionGadgetsBundle.message(
31 "instanceof.with.incompatible.interface.display.name");
34 @NotNull
35 public String buildErrorString(Object... infos){
36 return InspectionGadgetsBundle.message(
37 "instanceof.with.incompatible.interface.problem.descriptor");
40 public BaseInspectionVisitor buildVisitor(){
41 return new InstanceofIncompatibleInterfaceVisitor();
44 private static class InstanceofIncompatibleInterfaceVisitor
45 extends BaseInspectionVisitor{
47 @Override public void visitInstanceOfExpression(
48 @NotNull PsiInstanceOfExpression expression){
49 super.visitInstanceOfExpression(expression);
50 final PsiTypeElement castTypeElement = expression.getCheckType();
51 if(castTypeElement == null){
52 return;
54 final PsiType castType = castTypeElement.getType();
55 if(!(castType instanceof PsiClassType)){
56 return;
58 final PsiClassType castClassType = (PsiClassType)castType;
59 final PsiExpression operand = expression.getOperand();
60 final PsiType operandType = operand.getType();
61 if(!(operandType instanceof PsiClassType)){
62 return;
64 final PsiClassType operandClassType = (PsiClassType)operandType;
65 final PsiClass castClass = castClassType.resolve();
66 if(castClass == null){
67 return;
69 if(!castClass.isInterface()){
70 return;
72 final PsiClass operandClass = operandClassType.resolve();
73 if(operandClass == null){
74 return;
76 if(operandClass.isInterface()){
77 return;
79 if(InheritanceUtil.existsMutualSubclass(operandClass, castClass)){
80 return;
82 registerError(castTypeElement);