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
{
28 public String
getDisplayName(){
29 return InspectionGadgetsBundle
.message(
30 "casting.to.incompatible.interface.display.name");
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){
53 final PsiType castType
= castTypeElement
.getType();
54 if(!(castType
instanceof PsiClassType
)){
57 final PsiClassType castClassType
= (PsiClassType
)castType
;
58 final PsiExpression operand
= expression
.getOperand();
62 final PsiType operandType
= operand
.getType();
63 if(!(operandType
instanceof PsiClassType
)){
66 final PsiClassType operandClassType
= (PsiClassType
)operandType
;
67 final PsiClass castClass
= castClassType
.resolve();
68 if(castClass
== null){
71 if(!castClass
.isInterface()){
74 final PsiClass operandClass
= operandClassType
.resolve();
75 if(operandClass
== null){
78 if(operandClass
.isInterface()){
81 if(InheritanceUtil
.existsMutualSubclass(operandClass
, castClass
)){
84 registerError(castTypeElement
);