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
.jdk15
;
18 import com
.intellij
.lang
.StdLanguages
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.util
.PsiTreeUtil
;
21 import com
.intellij
.psi
.util
.PsiUtil
;
22 import com
.siyeh
.InspectionGadgetsBundle
;
23 import com
.siyeh
.ig
.BaseInspection
;
24 import com
.siyeh
.ig
.BaseInspectionVisitor
;
25 import com
.siyeh
.ig
.ui
.MultipleCheckboxOptionsPanel
;
26 import org
.jetbrains
.annotations
.NotNull
;
27 import org
.jetbrains
.annotations
.Nullable
;
31 public class RawUseOfParameterizedTypeInspection
extends BaseInspection
{
33 /** @noinspection PublicField*/
34 public boolean ignoreObjectConstruction
= true;
36 /** @noinspection PublicField*/
37 public boolean ignoreTypeCasts
= false;
41 public String
getDisplayName() {
42 return InspectionGadgetsBundle
.message(
43 "raw.use.of.parameterized.type.display.name");
47 protected String
buildErrorString(Object
... infos
) {
48 return InspectionGadgetsBundle
.message(
49 "raw.use.of.parameterized.type.problem.descriptor");
53 public JComponent
createOptionsPanel() {
54 final MultipleCheckboxOptionsPanel optionsPanel
=
55 new MultipleCheckboxOptionsPanel(this);
56 optionsPanel
.addCheckbox(
57 InspectionGadgetsBundle
.message(
58 "raw.use.of.parameterized.type.ignore.new.objects.option"),
59 "ignoreObjectConstruction");
60 optionsPanel
.addCheckbox(
61 InspectionGadgetsBundle
.message(
62 "raw.use.of.parameterized.type.ignore.type.casts.option"),
67 public BaseInspectionVisitor
buildVisitor() {
68 return new RawUseOfParameterizedTypeVisitor();
71 private class RawUseOfParameterizedTypeVisitor
72 extends BaseInspectionVisitor
{
74 @Override public void visitNewExpression(
75 @NotNull PsiNewExpression expression
) {
76 if (!hasNeededLanguageLevel(expression
)) {
79 super.visitNewExpression(expression
);
80 if (ignoreObjectConstruction
) {
83 if (expression
.getArrayInitializer() != null ||
84 expression
.getArrayDimensions().length
> 0) {
85 // array creation cannot be generic
88 final PsiJavaCodeReferenceElement classReference
=
89 expression
.getClassReference();
90 checkReferenceElement(classReference
);
93 @Override public void visitTypeElement(@NotNull PsiTypeElement typeElement
) {
94 if (!hasNeededLanguageLevel(typeElement
)) {
97 super.visitTypeElement(typeElement
);
98 final PsiElement parent
= typeElement
.getParent();
99 if (parent
instanceof PsiInstanceOfExpression
||
100 parent
instanceof PsiClassObjectAccessExpression
) {
103 if (ignoreTypeCasts
&& parent
instanceof PsiTypeCastExpression
) {
106 if (PsiTreeUtil
.getParentOfType(typeElement
, PsiComment
.class)
110 final PsiJavaCodeReferenceElement referenceElement
=
111 typeElement
.getInnermostComponentReferenceElement();
112 checkReferenceElement(referenceElement
);
115 @Override public void visitReferenceElement(
116 PsiJavaCodeReferenceElement reference
) {
117 if (!hasNeededLanguageLevel(reference
)) {
120 super.visitReferenceElement(reference
);
121 final PsiElement referenceParent
= reference
.getParent();
122 if (!(referenceParent
instanceof PsiReferenceList
)) {
125 final PsiReferenceList referenceList
=
126 (PsiReferenceList
)referenceParent
;
127 final PsiElement listParent
= referenceList
.getParent();
128 if (!(listParent
instanceof PsiClass
)) {
131 checkReferenceElement(reference
);
134 private void checkReferenceElement(
135 PsiJavaCodeReferenceElement reference
) {
136 if (reference
== null) {
139 final PsiType
[] typeParameters
= reference
.getTypeParameters();
140 if (typeParameters
.length
> 0) {
143 final PsiElement element
= reference
.resolve();
144 if (!(element
instanceof PsiClass
)) {
147 final PsiClass aClass
= (PsiClass
)element
;
148 final PsiElement qualifier
= reference
.getQualifier();
149 if (qualifier
instanceof PsiJavaCodeReferenceElement
) {
150 final PsiJavaCodeReferenceElement qualifierReference
=
151 (PsiJavaCodeReferenceElement
)qualifier
;
152 if (!aClass
.hasModifierProperty(PsiModifier
.STATIC
) &&
153 !aClass
.isInterface() && !aClass
.isEnum()) {
154 checkReferenceElement(qualifierReference
);
157 if (!aClass
.hasTypeParameters()) {
160 registerError(reference
);
163 private boolean hasNeededLanguageLevel(PsiElement element
) {
164 if (element
.getLanguage() != StdLanguages
.JAVA
) {
167 return PsiUtil
.isLanguageLevel5OrHigher(element
);