PsiElementVisitor (@NotNull final ProblemsHolder holder, boolean isOnTheFly) { return UastHintedVisitorAdapter.create(holder.getFile().getLanguage(), new AbstractUastNonRecursiveVisitor() { @Override public boolean visitCallExpression(@NotNull UCallExpression expression) { if (isAwtColorConstructor(expression) && isJBColorClassAccessible(holder) && !isUsedAsJBColorConstructorParameter(expression)) { PsiElement sourcePsi = expression.getSourcePsi(); if (sourcePsi != null) { LocalQuickFix[] fixes = sourcePsi.getLanguage().is(JavaLanguage.INSTANCE) ? new LocalQuickFix[]{new ConvertToJBColorQuickFix()} : LocalQuickFix.EMPTY_ARRAY; holder.registerProblem(sourcePsi, DevKitBundle.message("inspections.awt.color.used"), fixes); } } return super.visitCallExpression(expression); } private static boolean isAwtColorConstructor(@NotNull UCallExpression constructorCall) { return isColorTypeConstructor(constructorCall, AWT_COLOR_CLASS_NAME); } private static boolean isColorTypeConstructor(@NotNull UCallExpression call, @NotNull String colorClassName) { PsiMethod constructor = call.resolve(); if (constructor == null || !constructor.isConstructor()) return false; PsiClass constructorClass = constructor.getContainingClass(); if (constructorClass == null) return false; return colorClassName.equals(constructorClass.getQualifiedName()); } private static boolean isJBColorClassAccessible(@NotNull ProblemsHolder holder) { return DevKitInspectionUtil.isClassAvailable(holder, JB_COLOR_CLASS_NAME); } private static boolean isUsedAsJBColorConstructorParameter(@NotNull UExpression expression) { UCallExpression containingCall = UastContextKt.getUastParentOfType(expression.getSourcePsi(), UCallExpression.class, true); return containingCall != null && isJBColorConstructor(containingCall); } private static boolean isJBColorConstructor(@NotNull UCallExpression call) { return isColorTypeConstructor(call, JB_COLOR_CLASS_NAME); } @Override public boolean visitQualifiedReferenceExpression(@NotNull UQualifiedReferenceExpression node) { if (!(node.getUastParent() instanceof UImportStatement)) { inspectExpression(node); } return super.visitQualifiedReferenceExpression(node); } @Override public boolean visitSimpleNameReferenceExpression(@NotNull USimpleNameReferenceExpression node) { inspectExpression(node); return super.visitSimpleNameReferenceExpression(node); } private void inspectExpression(@NotNull UReferenceExpression expression) { if (isAwtColorConstantReference(expression) && isJBColorClassAccessible(holder) && !isUsedAsJBColorConstructorParameter(expression)) { PsiElement sourcePsi = expression.getSourcePsi(); if (sourcePsi != null) { holder.registerProblem(sourcePsi, DevKitBundle.message("inspections.awt.color.used"), new ConvertToJBColorConstantQuickFix()); } } } private static boolean isAwtColorConstantReference(@NotNull UReferenceExpression expression) { // avoid double warning for qualified and simple reference in Kotlin: if (expression.getUastParent() instanceof UQualifiedReferenceExpression) return false; PsiElement colorField = expression.resolve(); if (colorField instanceof PsiField && ((PsiField)colorField).hasModifierProperty(PsiModifier.STATIC)) { PsiClass colorClass = ((PsiField)colorField).getContainingClass(); return colorClass != null && AWT_COLOR_CLASS_NAME.equals(colorClass.getQualifiedName()); } return false; } }, HINTS); }