-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
UseJBColorInspection.java
115 lines (108 loc) · 4.81 KB
/
UseJBColorInspection.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* 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.InspectionManager;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToJBColorConstantQuickFix;
import org.jetbrains.idea.devkit.inspections.quickfix.ConvertToJBColorQuickFix;
import java.awt.*;
/**
* @author Konstantin Bulenkov
*/
public class UseJBColorInspection extends InternalInspection {
@Override
public PsiElementVisitor buildInternalVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitNewExpression(PsiNewExpression expression) {
final ProblemDescriptor descriptor = checkNewExpression(expression, holder.getManager(), isOnTheFly);
if (descriptor != null) {
holder.registerProblem(descriptor);
}
super.visitNewExpression(expression);
}
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
final PsiElement colorField = expression.resolve();
if (colorField != null && colorField instanceof PsiField && ((PsiField)colorField).hasModifierProperty(PsiModifier.STATIC)) {
final PsiClass colorClass = ((PsiField)colorField).getContainingClass();
if (colorClass != null && Color.class.getName().equals(colorClass.getQualifiedName())) {
String text = expression.getText();
if (text.contains(".")) {
text = text.substring(text.lastIndexOf('.'));
}
if (text.startsWith(".")) {
text = text.substring(1);
}
if (text.equalsIgnoreCase("lightGray")) {
text = "LIGHT_GRAY";
} else if (text.equalsIgnoreCase("darkGray")) {
text = "DARK_GRAY";
}
final ProblemDescriptor descriptor = holder.getManager()
.createProblemDescriptor(expression, "Change to JBColor." + text.toUpperCase(), new ConvertToJBColorConstantQuickFix(text.toUpperCase()),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
holder.registerProblem(descriptor);
}
}
}
};
}
@Nullable
private static ProblemDescriptor checkNewExpression(PsiNewExpression expression, InspectionManager manager, boolean isOnTheFly) {
final Project project = manager.getProject();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final PsiClass jbColorClass = facade.findClass(JBColor.class.getName(), GlobalSearchScope.allScope(project));
final PsiType type = expression.getType();
if (type != null && jbColorClass != null) {
if (!facade.getResolveHelper().isAccessible(jbColorClass, expression, jbColorClass)) return null;
final PsiExpressionList arguments = expression.getArgumentList();
if (arguments != null) {
if ("java.awt.Color".equals(type.getCanonicalText())) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiNewExpression) {
final PsiType parentType = ((PsiNewExpression)parent.getParent()).getType();
if (parentType == null || JBColor.class.getName().equals(parentType.getCanonicalText())) return null;
}
return manager.createProblemDescriptor(expression, "Replace with JBColor", new ConvertToJBColorQuickFix(),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
}
}
}
return null;
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return "Use Darcula aware JBColor";
}
@NotNull
@Override
public String getShortName() {
return "UseJBColor";
}
}