/*
 * Copyright 2000-2017 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;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.ide.presentation.Presentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.uast.UAnnotation;
import org.jetbrains.uast.UExpression;
import org.jetbrains.uast.UastContextKt;

public class PresentationAnnotationInspection extends DevKitUastInspectionBase {
  private static final Logger LOG = Logger.getInstance(PresentationAnnotationInspection.class);

  @NotNull
  @Override
  protected PsiElementVisitor buildInternalVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {
      @Override
      public void visitElement(PsiElement element) {
        UAnnotation annotation = UastContextKt.toUElement(element, UAnnotation.class);
        if (annotation == null) {
          return;
        }
        if (!Presentation.class.getCanonicalName().equals(annotation.getQualifiedName())) {
          return;
        }

        UExpression iconExpression = annotation.findDeclaredAttributeValue("icon");
        if (iconExpression == null) {
          return;
        }

        PsiElement iconExpressionPsi = iconExpression.getPsi();
        if (iconExpressionPsi == null) {
          LOG.error("Unexpected null value of @Presentation#icon expression PSI: " + element);
          return;
        }

        PsiReference[] references = iconExpressionPsi.getReferences();
        for (PsiReference reference : references) {
          if (reference.resolve() == null) {
            holder.registerProblem(reference);
          }
        }
      }
    };
  }
}

@@ -642,16 +642,124 @@
 tions;%0A%0A
+import com.intellij.codeInspection.InspectionManager;%0Aimport com.intellij.codeInspection.ProblemDescriptor;%0A
 import c
@@ -939,51 +939,8 @@
 nt;%0A
-import com.intellij.psi.PsiElementVisitor;%0A
 impo
@@ -1040,15 +1040,9 @@
 ins.
-uast.UA
+a
 nnot
@@ -1050,47 +1050,18 @@
 tion
-;%0Aimport org.jetbrains.uast.UExpression
+s.Nullable
 ;%0Aim
@@ -1084,29 +1084,17 @@
 ns.uast.
-UastContextKt
+*
 ;%0A%0Apubli
@@ -1270,22 +1270,23 @@
 );%0A%0A  @N
-otN
 ull
+able
 %0A  @Over
@@ -1297,378 +1297,184 @@
 %0A  p
-rotected PsiElementVisitor buildInternalVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) %7B%0A    return new PsiElementVisitor() %7B%0A      @Override%0A      public void visitElement(PsiElement element) %7B%0A        UAnnotation annotation = UastContextKt.toUElement(element, UAnnotation.class);%0A        if (annotation == null) %7B%0A          return;%0A        %7D%0A        if (!
+ublic ProblemDescriptor%5B%5D checkClass(@NotNull UClass aClass, @NotNull InspectionManager manager, boolean isOnTheFly) %7B%0A    final UAnnotation annotation = aClass.findAnnotation(
 Pres
@@ -1510,15 +1510,18 @@
 me()
-.equals
+);%0A    if 
 (ann
@@ -1531,28 +1531,16 @@
 tion
-.getQualifiedName())
+ == null
 ) %7B%0A
@@ -1549,35 +1549,28 @@
     
-  
-  
 return
+ null
 ;%0A    
- 
-   %7D%0A%0A    
+%7D%0A%0A
     
@@ -1645,27 +1645,25 @@
 icon%22);%0A
-    
     if 
+(!
 (iconExp
@@ -1666,31 +1666,54 @@
 nExpression 
-== null
+instanceof ULiteralExpression)
 ) %7B%0A        
@@ -1714,35 +1714,28 @@
     
-    
 return
+ null
 ;%0A    
-    %7D%0A%0A    
+%7D%0A%0A
     
@@ -1769,37 +1769,90 @@
 i = 
-iconExpression.getPsi();%0A    
+UastLiteralUtils.getPsiLanguageInjectionHost((ULiteralExpression)iconExpression);%0A
     
@@ -1880,28 +1880,24 @@
  == null) %7B%0A
-    
       LOG.er
@@ -1969,22 +1969,38 @@
 %22 + 
-element);%0A    
+annotation.asRenderString());%0A
     
@@ -2011,25 +2011,130 @@
 turn
+ null
 ;%0A    
+%7D%0A%0A
     
-%7D%0A%0A    
+ProblemsHolder holder = new ProblemsHolder(manager, iconExpressionPsi.getContainingFile(), isOnTheFly);%0A
     
@@ -2200,20 +2200,16 @@
 ();%0A    
-  
-  
 for (Psi
@@ -2240,28 +2240,24 @@
 ferences) %7B%0A
-    
       if (re
@@ -2293,20 +2293,16 @@
         
-    
 holder.r
@@ -2334,26 +2334,18 @@
 ;%0A      
- 
-   %7D%0A    
+%7D%0A
     %7D%0A  
@@ -2346,25 +2346,47 @@
   %7D%0A    
-  %7D%0A    %7D
+return holder.getResultsArray()
 ;%0A  %7D%0A%7D%0A

use UAST, check only on class-level