IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / bugs / CastToIncompatibleInterfaceInspection.java
blobf52a14b837c7dd0913efc86823abff9eec030e92
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 CastToIncompatibleInterfaceInspection extends BaseInspection {
27 @NotNull
28 public String getDisplayName(){
29 return InspectionGadgetsBundle.message(
30 "casting.to.incompatible.interface.display.name");
33 @NotNull
34 public String buildErrorString(Object... infos){
35 return InspectionGadgetsBundle.message(
36 "casting.to.incompatible.interface.problem.descriptor");
39 public BaseInspectionVisitor buildVisitor(){
40 return new CastToIncompatibleInterfaceVisitor();
43 private static class CastToIncompatibleInterfaceVisitor
44 extends BaseInspectionVisitor{
46 @Override public void visitTypeCastExpression(
47 @NotNull PsiTypeCastExpression expression){
48 super.visitTypeCastExpression(expression);
49 final PsiTypeElement castTypeElement = expression.getCastType();
50 if(castTypeElement == null){
51 return;
53 final PsiType castType = castTypeElement.getType();
54 if(!(castType instanceof PsiClassType)){
55 return;
57 final PsiClassType castClassType = (PsiClassType)castType;
58 final PsiExpression operand = expression.getOperand();
59 if(operand == null){
60 return;
62 final PsiType operandType = operand.getType();
63 if(!(operandType instanceof PsiClassType)){
64 return;
66 final PsiClassType operandClassType = (PsiClassType)operandType;
67 final PsiClass castClass = castClassType.resolve();
68 if(castClass == null){
69 return;
71 if(!castClass.isInterface()){
72 return;
74 final PsiClass operandClass = operandClassType.resolve();
75 if(operandClass == null){
76 return;
78 if(operandClass.isInterface()){
79 return;
81 if(InheritanceUtil.existsMutualSubclass(operandClass, castClass)){
82 return;
84 registerError(castTypeElement);