<commit_before>/*
 * 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));
    }
  }
}
<commit_msg>IDEA-CR-19952 GenericDomValue#getValue replaced with getStringValue in MavenPropertyInParentInspection<commit_after>/*
 * 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.getStringValue();
      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));
    }
  }
}

{'fraction_non_alphanumeric': 0.09969788519637462, 'fraction_numerical': 0.0037462235649546828, 'mean_word_length': 4.175734834271419, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533414', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2248, 'n_words': 508}