Inspections - pass onTheFly into ProblemDescriptors & use it to create LAZY refs...
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInspection / wrongPackageStatement / WrongPackageStatementInspection.java
blob50ae74ec551cd99e2fcb023537bb8ae74f5dc460
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInspection.wrongPackageStatement;
18 import com.intellij.codeHighlighting.HighlightDisplayLevel;
19 import com.intellij.codeInsight.daemon.JavaErrorMessages;
20 import com.intellij.codeInspection.*;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.util.Comparing;
23 import com.intellij.psi.*;
24 import org.jetbrains.annotations.NonNls;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
28 import java.util.ArrayList;
29 import java.util.List;
31 /**
32 * User: anna
33 * Date: 14-Nov-2005
35 public class WrongPackageStatementInspection extends BaseJavaLocalInspectionTool {
36 @Nullable
37 public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
38 // does not work in tests since CodeInsightTestCase copies file into temporary location
39 if (ApplicationManager.getApplication().isUnitTestMode()) return null;
40 if (file instanceof PsiJavaFile) {
41 if (JspPsiUtil.isInJspFile(file)) return null;
42 PsiJavaFile javaFile = (PsiJavaFile)file;
43 // highlight the first class in the file only
44 PsiClass[] classes = javaFile.getClasses();
45 if (classes.length == 0) return null;
46 PsiDirectory directory = javaFile.getContainingDirectory();
47 if (directory == null) return null;
48 PsiPackage dirPackage = JavaDirectoryService.getInstance().getPackage(directory);
49 if (dirPackage == null) return null;
50 PsiPackageStatement packageStatement = javaFile.getPackageStatement();
52 String packageName = dirPackage.getQualifiedName();
53 if (!Comparing.strEqual(packageName, "", true) && packageStatement == null) {
54 String description = JavaErrorMessages.message("missing.package.statement", packageName);
56 return new ProblemDescriptor[]{manager.createProblemDescriptor(classes[0].getNameIdentifier(), description,
57 new AdjustPackageNameFix(javaFile, null, dirPackage),
58 ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly)};
60 if (packageStatement != null) {
61 final PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference();
62 PsiPackage classPackage = (PsiPackage)packageReference.resolve();
63 List<LocalQuickFix> availableFixes = new ArrayList<LocalQuickFix>();
64 if (classPackage == null) {
65 availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage));
67 else if (!Comparing.equal(dirPackage.getQualifiedName(), packageReference.getText(), true)) {
68 availableFixes.add(new AdjustPackageNameFix(javaFile, packageStatement, dirPackage));
69 MoveToPackageFix moveToPackageFix = new MoveToPackageFix(file, classPackage);
70 if (moveToPackageFix.isAvailable()) {
71 availableFixes.add(moveToPackageFix);
74 if (!availableFixes.isEmpty()){
75 String description = JavaErrorMessages.message("package.name.file.path.mismatch",
76 packageReference.getText(),
77 dirPackage.getQualifiedName());
78 return new ProblemDescriptor[]{manager.createProblemDescriptor(packageStatement, description, availableFixes.toArray(new LocalQuickFix[availableFixes.size()]), ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
79 isOnTheFly)};
84 return null;
87 @NotNull
88 public String getGroupDisplayName() {
89 return "";
92 @NotNull
93 public HighlightDisplayLevel getDefaultLevel() {
94 return HighlightDisplayLevel.ERROR;
97 @NotNull
98 public String getDisplayName() {
99 return InspectionsBundle.message("wrong.package.statement");
102 @NotNull
103 @NonNls
104 public String getShortName() {
105 return "WrongPackageStatement";
108 public boolean isEnabledByDefault() {
109 return true;