alternative id provided (IDEA-52135)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / jdk15 / RawUseOfParameterizedTypeInspection.java
blobd57f24ffdb1133fec4f36191ed336f308bce3f8d
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.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;
29 import javax.swing.*;
31 public class RawUseOfParameterizedTypeInspection extends BaseInspection {
33 /** @noinspection PublicField*/
34 public boolean ignoreObjectConstruction = true;
36 /** @noinspection PublicField*/
37 public boolean ignoreTypeCasts = false;
40 @NotNull
41 public String getDisplayName() {
42 return InspectionGadgetsBundle.message(
43 "raw.use.of.parameterized.type.display.name");
46 @NotNull
47 protected String buildErrorString(Object... infos) {
48 return InspectionGadgetsBundle.message(
49 "raw.use.of.parameterized.type.problem.descriptor");
52 @Nullable
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"),
63 "ignoreTypeCasts");
64 return optionsPanel;
67 @Override
68 public String getAlternativeID() {
69 return "unchecked";
72 public BaseInspectionVisitor buildVisitor() {
73 return new RawUseOfParameterizedTypeVisitor();
76 private class RawUseOfParameterizedTypeVisitor
77 extends BaseInspectionVisitor {
79 @Override public void visitNewExpression(
80 @NotNull PsiNewExpression expression) {
81 if (!hasNeededLanguageLevel(expression)) {
82 return;
84 super.visitNewExpression(expression);
85 if (ignoreObjectConstruction) {
86 return;
88 if (expression.getArrayInitializer() != null ||
89 expression.getArrayDimensions().length > 0) {
90 // array creation cannot be generic
91 return;
93 final PsiJavaCodeReferenceElement classReference =
94 expression.getClassReference();
95 checkReferenceElement(classReference);
98 @Override public void visitTypeElement(@NotNull PsiTypeElement typeElement) {
99 if (!hasNeededLanguageLevel(typeElement)) {
100 return;
102 super.visitTypeElement(typeElement);
103 final PsiElement parent = typeElement.getParent();
104 if (parent instanceof PsiInstanceOfExpression ||
105 parent instanceof PsiClassObjectAccessExpression) {
106 return;
108 if (ignoreTypeCasts && parent instanceof PsiTypeCastExpression) {
109 return;
111 if (PsiTreeUtil.getParentOfType(typeElement, PsiComment.class)
112 != null) {
113 return;
115 final PsiJavaCodeReferenceElement referenceElement =
116 typeElement.getInnermostComponentReferenceElement();
117 checkReferenceElement(referenceElement);
120 @Override public void visitReferenceElement(
121 PsiJavaCodeReferenceElement reference) {
122 if (!hasNeededLanguageLevel(reference)) {
123 return;
125 super.visitReferenceElement(reference);
126 final PsiElement referenceParent = reference.getParent();
127 if (!(referenceParent instanceof PsiReferenceList)) {
128 return;
130 final PsiReferenceList referenceList =
131 (PsiReferenceList)referenceParent;
132 final PsiElement listParent = referenceList.getParent();
133 if (!(listParent instanceof PsiClass)) {
134 return;
136 checkReferenceElement(reference);
139 private void checkReferenceElement(
140 PsiJavaCodeReferenceElement reference) {
141 if (reference == null) {
142 return;
144 final PsiType[] typeParameters = reference.getTypeParameters();
145 if (typeParameters.length > 0) {
146 return;
148 final PsiElement element = reference.resolve();
149 if (!(element instanceof PsiClass)) {
150 return;
152 final PsiClass aClass = (PsiClass)element;
153 final PsiElement qualifier = reference.getQualifier();
154 if (qualifier instanceof PsiJavaCodeReferenceElement) {
155 final PsiJavaCodeReferenceElement qualifierReference =
156 (PsiJavaCodeReferenceElement)qualifier;
157 if (!aClass.hasModifierProperty(PsiModifier.STATIC) &&
158 !aClass.isInterface() && !aClass.isEnum()) {
159 checkReferenceElement(qualifierReference);
162 if (!aClass.hasTypeParameters()) {
163 return;
165 registerError(reference);
168 private boolean hasNeededLanguageLevel(PsiElement element) {
169 if (element.getLanguage() != StdLanguages.JAVA) {
170 return false;
172 return PsiUtil.isLanguageLevel5OrHigher(element);