diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenPropertyInParentInspection.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenPropertyInParentInspection.java new file mode 100644 index 000000000000..dcc460cd793d --- /dev/null +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/inspections/MavenPropertyInParentInspection.java @@ -0,0 +1,102 @@ +/* + * 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.maven.dom.inspections; + +import com.intellij.codeHighlighting.HighlightDisplayLevel; +import com.intellij.codeInspection.*; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.intellij.psi.xml.XmlFile; +import com.intellij.psi.xml.XmlTag; +import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.xml.DomFileElement; +import com.intellij.util.xml.DomManager; +import com.intellij.util.xml.GenericDomValue; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.idea.maven.dom.MavenDomBundle; +import org.jetbrains.idea.maven.dom.model.MavenDomParent; +import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel; + +import java.util.List; + +/** + * @author ibessonov + */ +public class MavenPropertyInParentInspection extends XmlSuppressableInspectionTool { + + @NotNull + public String getGroupDisplayName() { + return MavenDomBundle.message("inspection.group"); + } + + @NotNull + public String getDisplayName() { + return MavenDomBundle.message("inspection.property.in.parent.name"); + } + + @NotNull + public String getShortName() { + return "MavenPropertyInParent"; + } + + @NotNull + public HighlightDisplayLevel getDefaultLevel() { + return HighlightDisplayLevel.WARNING; + } + + @Nullable + public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { + if (file instanceof XmlFile && (file.isPhysical() || ApplicationManager.getApplication().isUnitTestMode())) { + DomManager domManager = DomManager.getDomManager(file.getProject()); + DomFileElement model = domManager.getFileElement((XmlFile)file, MavenDomProjectModel.class); + + if (model != null) { + List problems = ContainerUtil.newArrayListWithCapacity(3); + + MavenDomParent mavenParent = model.getRootElement().getMavenParent(); + validate(manager, isOnTheFly, problems, mavenParent.getGroupId()); + validate(manager, isOnTheFly, problems, mavenParent.getArtifactId()); + validate(manager, isOnTheFly, problems, mavenParent.getVersion()); + + if (problems.isEmpty()) return ProblemDescriptor.EMPTY_ARRAY; + return problems.toArray(new ProblemDescriptor[problems.size()]); + } + } + + return null; + } + + private static void validate(@NotNull InspectionManager manager, boolean isOnTheFly, + @NotNull List problems, @NotNull GenericDomValue domValue) { + String unresolvedValue = domValue.getRawText(); + if (unresolvedValue != null && unresolvedValue.contains("${")) { + LocalQuickFix fix = null; + String resolvedValue = domValue.getValue(); + if (!unresolvedValue.equals(resolvedValue)) { + fix = new LocalQuickFixBase(MavenDomBundle.message("refactoring.inline.property")) { + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + ((XmlTag)descriptor.getPsiElement()).getValue().setText(resolvedValue); + } + }; + } + problems.add(manager.createProblemDescriptor(domValue.getXmlTag(), MavenDomBundle.message("inspection.property.in.parent.description"), + fix, ProblemHighlightType.GENERIC_ERROR, isOnTheFly)); + } + } +} diff --git a/plugins/maven/src/main/resources/META-INF/plugin.xml b/plugins/maven/src/main/resources/META-INF/plugin.xml index 7a6e63702fd7..bd1911b8887f 100644 --- a/plugins/maven/src/main/resources/META-INF/plugin.xml +++ b/plugins/maven/src/main/resources/META-INF/plugin.xml @@ -113,6 +113,9 @@ + diff --git a/plugins/maven/src/main/resources/MavenDomBundle.properties b/plugins/maven/src/main/resources/MavenDomBundle.properties index d840ea4d1ee7..5eb036a947e0 100644 --- a/plugins/maven/src/main/resources/MavenDomBundle.properties +++ b/plugins/maven/src/main/resources/MavenDomBundle.properties @@ -33,8 +33,11 @@ all.dependencies=All Dependencies chooser.show.project.files=Project Files refactoring.introduce.property=Introduce Property +refactoring.inline.property=Inline Property choose.project=Choose project inspection.duplicate.dependencies.name=Duplicate Dependencies inspection.duplicate.plugin.declaration=Duplicate plugin declaration MavenDuplicateDependenciesInspection.has.duplicates=Dependency is duplicated in file(s): {0} -inspection.redundant.groupId.name=Redundant groupId \ No newline at end of file +inspection.redundant.groupId.name=Redundant groupId +inspection.property.in.parent.name=Usage of properties in parent description +inspection.property.in.parent.description=Properties in parent definition are prohibited \ No newline at end of file diff --git a/plugins/maven/src/main/resources/inspectionDescriptions/MavenPropertyInParent.html b/plugins/maven/src/main/resources/inspectionDescriptions/MavenPropertyInParent.html new file mode 100644 index 000000000000..bcc197571688 --- /dev/null +++ b/plugins/maven/src/main/resources/inspectionDescriptions/MavenPropertyInParent.html @@ -0,0 +1,5 @@ + + +Reports that usage of properties in modules parent definition is prohibited + + \ No newline at end of file