2 * Copyright 2003-2009 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
.psiutils
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.psi
.*;
20 import org
.jetbrains
.annotations
.NonNls
;
21 import org
.jetbrains
.annotations
.NotNull
;
22 import org
.jetbrains
.annotations
.Nullable
;
24 public class TypeUtils
{
26 private TypeUtils() {}
28 public static boolean expressionHasType(
29 @NonNls @NotNull String typeName
,
30 @Nullable PsiExpression expression
) {
31 if (expression
== null) {
34 final PsiType type
= expression
.getType();
35 return typeEquals(typeName
, type
);
38 public static boolean typeEquals(@NonNls @NotNull String typeName
,
39 @Nullable PsiType targetType
) {
40 return targetType
!= null && targetType
.equalsToText(typeName
);
43 public static PsiType
getJavaLangObjectType(@NotNull PsiElement context
) {
44 final Project project
= context
.getProject();
45 final PsiElementFactory factory
=
46 JavaPsiFacade
.getInstance(project
).getElementFactory();
47 return factory
.createTypeFromText("java.lang.Object", context
);
50 public static boolean isJavaLangObject(@Nullable PsiType targetType
) {
51 return typeEquals("java.lang.Object", targetType
);
54 public static boolean isJavaLangString(@Nullable PsiType targetType
) {
55 return typeEquals("java.lang.String", targetType
);
58 public static boolean expressionHasTypeOrSubtype(
59 @Nullable PsiExpression expression
,
60 @NonNls @NotNull String typeName
) {
61 if (expression
== null) {
64 final PsiType type
= expression
.getType();
68 if (!(type
instanceof PsiClassType
)) {
71 final PsiClassType classType
= (PsiClassType
) type
;
72 final PsiClass aClass
= classType
.resolve();
73 return aClass
!= null && ClassUtils
.isSubclass(aClass
, typeName
);
76 public static String
expressionHasTypeOrSubtype(
77 @Nullable PsiExpression expression
,
78 @NonNls @NotNull String
... typeNames
) {
79 if (expression
== null) {
82 final PsiType type
= expression
.getType();
86 if (!(type
instanceof PsiClassType
)) {
89 final PsiClassType classType
= (PsiClassType
) type
;
90 final PsiClass aClass
= classType
.resolve();
94 for (String typeName
: typeNames
) {
95 if (ClassUtils
.isSubclass(aClass
, typeName
)) {