summaryrefslogtreecommitdiff
path: root/plugins/devkit/src/inspections/internal/UndesirableClassUsageInspection.java
blob: 8348b0e838c3d97afd74679ad50505346ce84e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.devkit.inspections.internal;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.application.QueryExecutorBase;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.psi.*;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTabbedPane;
import com.intellij.ui.table.JBTable;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.QueryExecutor;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.Map;

public class UndesirableClassUsageInspection extends InternalInspection {
  private static final Map<String, String> CLASSES = new THashMap<String, String>();
  static {
    CLASSES.put(JList.class.getName(), JBList.class.getName());
    CLASSES.put(JTable.class.getName(), JBTable.class.getName());
    CLASSES.put(JTree.class.getName(), Tree.class.getName());
    CLASSES.put(JScrollPane.class.getName(), JBScrollPane.class.getName());
    CLASSES.put(JTabbedPane.class.getName(), JBTabbedPane.class.getName());
    CLASSES.put(JComboBox.class.getName(), ComboBox.class.getName());
    CLASSES.put(QueryExecutor.class.getName(), QueryExecutorBase.class.getName());
    CLASSES.put(BufferedImage.class.getName(), "UIUtil.createImage()");
  }

  @Override
  @NotNull
  public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitNewExpression(PsiNewExpression expression) {
        PsiJavaCodeReferenceElement ref = expression.getClassReference();
        if (ref == null) return;

        PsiElement res = ref.resolve();
        if (res == null) return;

        String name = ((PsiClass)res).getQualifiedName();
        if (name == null) return;

        String replacement = CLASSES.get(name);
        if (replacement == null) return;

        holder.registerProblem(expression, "Please use '" + replacement + "' instead", ProblemHighlightType.LIKE_DEPRECATED);
      }
    };
  }
}