/*
 * Copyright 2003-2019 Dave Griffith, Bas Leijdekkers
 *
 * 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 com.siyeh.ig.imports;

import com.intellij.codeInspection.CleanupLocalInspectionTool;
import com.intellij.psi.*;
import com.intellij.psi.util.FileTypeUtils;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.DeleteImportFix;
import com.siyeh.ig.psiutils.ImportUtils;
import org.jetbrains.annotations.NotNull;

public class JavaLangImportInspection extends BaseInspection implements CleanupLocalInspectionTool{

  @Override
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message(
      "java.lang.import.display.name");
  }

  @Override
  @NotNull
  public String buildErrorString(Object... infos) {
    return InspectionGadgetsBundle.message(
      "java.lang.import.problem.descriptor");
  }

  @Override
  public InspectionGadgetsFix buildFix(Object... infos) {
    return new DeleteImportFix();
  }

  @Override
  public boolean shouldInspect(PsiFile file) {
    return !FileTypeUtils.isInServerPageFile(file);
  }

  @Override
  public BaseInspectionVisitor buildVisitor() {
    return new JavaLangImportVisitor();
  }

  private static class JavaLangImportVisitor extends BaseInspectionVisitor {

    @Override
    public void visitJavaFile(PsiJavaFile file) {
      super.visitJavaFile(file);
      final PsiImportList importList = file.getImportList();
      if (importList == null) {
        return;
      }
      final PsiImportStatement[] importStatements = importList.getImportStatements();
      for (PsiImportStatement importStatement : importStatements) {
        checkImportStatement(importStatement, file);
      }
    }

    private void checkImportStatement(PsiImportStatement importStatement, PsiJavaFile file) {
      final PsiJavaCodeReferenceElement reference = importStatement.getImportReference();
      if (reference == null) {
        return;
      }
      final String text = importStatement.getQualifiedName();
      if (text == null) {
        return;
      }
      if (importStatement.isOnDemand()) {
        if (HardcodedMethodConstants.JAVA_LANG.equals(text)) {
          registerError(importStatement);
        }
      }
      else {
        final int classNameIndex = text.lastIndexOf((int)'.');
        if (classNameIndex < 0) {
          return;
        }
        final String parentName = text.substring(0, classNameIndex);
        if (!HardcodedMethodConstants.JAVA_LANG.equals(parentName)) {
          return;
        }
        if (ImportUtils.hasOnDemandImportConflict(text, file)) {
          return;
        }
        registerError(importStatement);
      }
    }
  }
}

@@ -1972,336 +1972,52 @@
 isit
-JavaFile(PsiJavaFile file) %7B%0A      super.visitJavaFile(file);%0A      final PsiImportList importList = file.getImportList();%0A      if (importList == null) %7B%0A        return;%0A      %7D%0A      final PsiImportStatement%5B%5D importStatements = importList.getImportStatements();%0A      for (PsiImportStatement importStatement : importS
+ImportStatement(PsiImportStatement s
 tatement
 s) %7B
@@ -2012,17 +2012,16 @@
 tatement
-s
 ) %7B%0A    
@@ -2026,163 +2026,46 @@
     
-  checkImportStatement(importStatement, file);%0A      %7D%0A    %7D%0A%0A    private void checkImportStatement(PsiImportStatement importStatement, PsiJavaFile file) %7B
+super.visitImportStatement(statement);
 %0A   
@@ -2113,23 +2113,17 @@
 rence = 
-importS
+s
 tatement
@@ -2226,23 +2226,17 @@
  text = 
-importS
+s
 tatement
@@ -2316,23 +2316,17 @@
     if (
-importS
+s
 tatement
@@ -2421,39 +2421,33 @@
   registerError(
-importS
+s
 tatement);%0A     
@@ -2820,20 +2820,25 @@
 t(text, 
-file
+statement
 )) %7B%0A   
@@ -2888,15 +2888,9 @@
 ror(
-importS
+s
 tate

simplify visitor