Maven inspection that reports properties usage in module parent definition

This commit is contained in:
Ivan Bessonov
2017-04-03 12:36:32 +03:00
parent dc2bb52910
commit d3c302f7f5
4 changed files with 114 additions and 1 deletions
@@ -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<MavenDomProjectModel> model = domManager.getFileElement((XmlFile)file, MavenDomProjectModel.class);
if (model != null) {
List<ProblemDescriptor> 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<ProblemDescriptor> problems, @NotNull GenericDomValue<String> 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));
}
}
}
@@ -113,6 +113,9 @@
<localInspection language="XML" shortName="MavenRedundantGroupId" bundle="MavenDomBundle" key="inspection.redundant.groupId.name"
groupKey="inspection.group" enabledByDefault="true" level="WARNING"
implementationClass="org.jetbrains.idea.maven.dom.inspections.MavenRedundantGroupIdInspection"/>
<localInspection language="XML" shortName="MavenPropertyInParent" bundle="MavenDomBundle" key="inspection.property.in.parent.name"
groupKey="inspection.group" enabledByDefault="true" level="WARNING"
implementationClass="org.jetbrains.idea.maven.dom.inspections.MavenPropertyInParentInspection"/>
<dom.extender domClass="org.jetbrains.idea.maven.dom.model.MavenDomConfiguration"
extenderClass="org.jetbrains.idea.maven.dom.MavenPluginConfigurationDomExtender"/>
@@ -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=<html><body>Dependency is duplicated in file(s): {0} </body></html>
inspection.redundant.groupId.name=Redundant groupId
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
@@ -0,0 +1,5 @@
<html>
<body>
Reports that usage of properties in modules parent definition is prohibited
</body>
</html>