diff --git a/java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/AdjustPackageNameFix.java b/java/java-analysis-impl/src/com/intellij/codeInspection/wrongPackageStatement/AdjustPackageNameFix.java similarity index 100% rename from java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/AdjustPackageNameFix.java rename to java/java-analysis-impl/src/com/intellij/codeInspection/wrongPackageStatement/AdjustPackageNameFix.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspectionBase.java new file mode 100644 index 000000000000..8efcc458c2d3 --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspectionBase.java @@ -0,0 +1,125 @@ +/* + * Copyright 2000-2009 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 com.intellij.codeInspection.wrongPackageStatement; + +import com.intellij.codeHighlighting.HighlightDisplayLevel; +import com.intellij.codeInsight.daemon.JavaErrorMessages; +import com.intellij.codeInspection.*; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.util.Comparing; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtilCore; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * User: anna + * Date: 14-Nov-2005 + */ +public class WrongPackageStatementInspectionBase extends BaseJavaBatchLocalInspectionTool { + @Override + @Nullable + public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { + // does not work in tests since CodeInsightTestCase copies file into temporary location + if (ApplicationManager.getApplication().isUnitTestMode()) return null; + if (file instanceof PsiJavaFile) { + if (isInJsp(file)) return null; + PsiJavaFile javaFile = (PsiJavaFile)file; + + PsiDirectory directory = javaFile.getContainingDirectory(); + if (directory == null) return null; + PsiPackage dirPackage = JavaDirectoryService.getInstance().getPackage(directory); + if (dirPackage == null) return null; + PsiPackageStatement packageStatement = javaFile.getPackageStatement(); + + // highlight the first class in the file only + PsiClass[] classes = javaFile.getClasses(); + if (classes.length == 0 && packageStatement == null) return null; + + String packageName = dirPackage.getQualifiedName(); + if (!Comparing.strEqual(packageName, "", true) && packageStatement == null) { + String description = JavaErrorMessages.message("missing.package.statement", packageName); + + return new ProblemDescriptor[]{manager.createProblemDescriptor(classes[0].getNameIdentifier(), description, + new AdjustPackageNameFix(packageName), + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly)}; + } + if (packageStatement != null) { + final PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference(); + PsiPackage classPackage = (PsiPackage)packageReference.resolve(); + List availableFixes = new ArrayList(); + if (classPackage == null || !Comparing.equal(dirPackage.getQualifiedName(), packageReference.getQualifiedName(), true)) { + availableFixes.add(new AdjustPackageNameFix(packageName)); + String packName = classPackage != null ? classPackage.getQualifiedName() : packageReference.getQualifiedName(); + addMoveToPackageFix(file, packName, availableFixes); + } + if (!availableFixes.isEmpty()){ + String description = JavaErrorMessages.message("package.name.file.path.mismatch", + packageReference.getQualifiedName(), + dirPackage.getQualifiedName()); + LocalQuickFix[] fixes = availableFixes.toArray(new LocalQuickFix[availableFixes.size()]); + ProblemDescriptor descriptor = + manager.createProblemDescriptor(packageStatement.getPackageReference(), description, isOnTheFly, + fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); + return new ProblemDescriptor[]{descriptor}; + + } + } + } + return null; + } + + private static boolean isInJsp(PsiFile file) { + return PsiUtilCore.getTemplateLanguageFile(file) instanceof ServerPageFile; + } + + protected void addMoveToPackageFix(PsiFile file, String packName, List availableFixes) { + } + + @Override + @NotNull + public String getGroupDisplayName() { + return ""; + } + + @Override + @NotNull + public HighlightDisplayLevel getDefaultLevel() { + return HighlightDisplayLevel.ERROR; + } + + @Override + @NotNull + public String getDisplayName() { + return InspectionsBundle.message("wrong.package.statement"); + } + + @Override + @NotNull + @NonNls + public String getShortName() { + return "WrongPackageStatement"; + } + + @Override + public boolean isEnabledByDefault() { + return true; + } +} diff --git a/java/java-impl/src/com/intellij/codeInspection/MoveToPackageFix.java b/java/java-impl/src/com/intellij/codeInspection/MoveToPackageFix.java index 983c4fa6f55b..3fd32c34212b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/MoveToPackageFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/MoveToPackageFix.java @@ -99,10 +99,4 @@ public class MoveToPackageFix implements LocalQuickFix { LOG.error(e); } } - - public boolean startInWriteAction() { - return false; - } - - } diff --git a/java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspection.java b/java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspection.java index e6fc02673bb9..3f4bd058a1fc 100644 --- a/java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/wrongPackageStatement/WrongPackageStatementInspection.java @@ -15,105 +15,22 @@ */ package com.intellij.codeInspection.wrongPackageStatement; -import com.intellij.codeHighlighting.HighlightDisplayLevel; -import com.intellij.codeInsight.daemon.JavaErrorMessages; -import com.intellij.codeInspection.*; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.util.Comparing; -import com.intellij.psi.*; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.MoveToPackageFix; +import com.intellij.psi.PsiFile; -import java.util.ArrayList; import java.util.List; /** * User: anna * Date: 14-Nov-2005 */ -public class WrongPackageStatementInspection extends BaseJavaLocalInspectionTool { +public class WrongPackageStatementInspection extends WrongPackageStatementInspectionBase { @Override - @Nullable - public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { - // does not work in tests since CodeInsightTestCase copies file into temporary location - if (ApplicationManager.getApplication().isUnitTestMode()) return null; - if (file instanceof PsiJavaFile) { - if (JspPsiUtil.isInJspFile(file)) return null; - PsiJavaFile javaFile = (PsiJavaFile)file; - - PsiDirectory directory = javaFile.getContainingDirectory(); - if (directory == null) return null; - PsiPackage dirPackage = JavaDirectoryService.getInstance().getPackage(directory); - if (dirPackage == null) return null; - PsiPackageStatement packageStatement = javaFile.getPackageStatement(); - - // highlight the first class in the file only - PsiClass[] classes = javaFile.getClasses(); - if (classes.length == 0 && packageStatement == null) return null; - - String packageName = dirPackage.getQualifiedName(); - if (!Comparing.strEqual(packageName, "", true) && packageStatement == null) { - String description = JavaErrorMessages.message("missing.package.statement", packageName); - - return new ProblemDescriptor[]{manager.createProblemDescriptor(classes[0].getNameIdentifier(), description, - new AdjustPackageNameFix(packageName), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly)}; - } - if (packageStatement != null) { - final PsiJavaCodeReferenceElement packageReference = packageStatement.getPackageReference(); - PsiPackage classPackage = (PsiPackage)packageReference.resolve(); - List availableFixes = new ArrayList(); - if (classPackage == null || !Comparing.equal(dirPackage.getQualifiedName(), packageReference.getQualifiedName(), true)) { - availableFixes.add(new AdjustPackageNameFix(packageName)); - MoveToPackageFix moveToPackageFix = new MoveToPackageFix(classPackage != null ? classPackage.getQualifiedName() : packageReference.getQualifiedName()); - if (moveToPackageFix.isAvailable(file)) { - availableFixes.add(moveToPackageFix); - } - } - if (!availableFixes.isEmpty()){ - String description = JavaErrorMessages.message("package.name.file.path.mismatch", - packageReference.getQualifiedName(), - dirPackage.getQualifiedName()); - LocalQuickFix[] fixes = availableFixes.toArray(new LocalQuickFix[availableFixes.size()]); - ProblemDescriptor descriptor = - manager.createProblemDescriptor(packageStatement.getPackageReference(), description, isOnTheFly, - fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); - return new ProblemDescriptor[]{descriptor}; - - } - } + protected void addMoveToPackageFix(PsiFile file, String packName, List availableFixes) { + MoveToPackageFix moveToPackageFix = new MoveToPackageFix(packName); + if (moveToPackageFix.isAvailable(file)) { + availableFixes.add(moveToPackageFix); } - return null; - } - - @Override - @NotNull - public String getGroupDisplayName() { - return ""; - } - - @Override - @NotNull - public HighlightDisplayLevel getDefaultLevel() { - return HighlightDisplayLevel.ERROR; - } - - @Override - @NotNull - public String getDisplayName() { - return InspectionsBundle.message("wrong.package.statement"); - } - - @Override - @NotNull - @NonNls - public String getShortName() { - return "WrongPackageStatement"; - } - - @Override - public boolean isEnabledByDefault() { - return true; } }