<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testFramework;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.PathUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.lang.JavaVersion;
import kotlin.Deprecated;
import org.jetbrains.annotations.*;
import org.junit.Assert;
import org.junit.Assume;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertTrue;

@TestOnly
public class IdeaTestUtil extends PlatformTestUtil {
  private static final String MOCK_JDK_DIR_NAME_PREFIX = "mockJDK-";

  @SuppressWarnings("UseOfSystemOutOrSystemErr")
  public static void printDetectedPerformanceTimings() {
    System.out.println(Timings.getStatistics());
  }

  public static void withLevel(@NotNull final Module module, @NotNull LanguageLevel level, @NotNull final Runnable r) {
    final LanguageLevelProjectExtension projectExt = LanguageLevelProjectExtension.getInstance(module.getProject());

    final LanguageLevel projectLevel = projectExt.getLanguageLevel();
    final LanguageLevel moduleLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
    try {
      projectExt.setLanguageLevel(level);
      setModuleLanguageLevel(module, level);
      r.run();
    }
    finally {
      setModuleLanguageLevel(module, moduleLevel);
      projectExt.setLanguageLevel(projectLevel);
    }
  }

  public static void setModuleLanguageLevel(@NotNull Module module, @Nullable LanguageLevel level) {
    ModuleRootModificationUtil.updateModel(module, (model) -> {
      model.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(level);
    });
  }

  public static void setModuleLanguageLevel(@NotNull Module module, @NotNull LanguageLevel level, @NotNull Disposable parentDisposable) {
    LanguageLevel prev = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
    setModuleLanguageLevel(module, level);
    Disposer.register(parentDisposable, () -> setModuleLanguageLevel(module, prev));
  }

  @NotNull
  public static Sdk getMockJdk(@NotNull JavaVersion version) {
    int mockJdk = version.feature >= 11 ? 11 :
                  version.feature >= 9 ? 9 :
                  version.feature >= 7 ? version.feature :
                  version.feature >= 5 ? 7 :
                  4;
    String path = getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + (mockJdk < 11 ? "1." : "") + mockJdk).getPath();
    return createMockJdk("java " + version, path);
  }

  @NotNull
  private static Sdk createMockJdk(@NotNull String name, @NotNull String path) {
    JavaSdk javaSdk = JavaSdk.getInstance();
    if (javaSdk == null) {
      throw new AssertionError("The test uses classes from Java plugin but Java plugin wasn't loaded; make sure that Java plugin " +
                               "classes are included into classpath and that the plugin isn't disabled by using 'idea.load.plugins', 'idea.load.plugins.id', 'idea.load.plugins.category' system properties");
    }
    return ((JavaSdkImpl)javaSdk).createMockJdk(name, path, false);
  }

  @NotNull
  public static Sdk getMockJdk14() {
    return getMockJdk(JavaVersion.compose(4));
  }

  @NotNull
  public static Sdk getMockJdk17() {
    return getMockJdk(JavaVersion.compose(7));
  }

  @NotNull
  public static Sdk getMockJdk17(@NotNull String name) {
    return createMockJdk(name, getMockJdk17Path().getPath());
  }

  @NotNull
  public static Sdk getMockJdk18() {
    return getMockJdk(JavaVersion.compose(8));
  }

  @NotNull
  public static Sdk getMockJdk9() {
    return getMockJdk(JavaVersion.compose(9));
  }

  @NotNull
  public static File getMockJdk14Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.4");
  }

  @NotNull
  public static File getMockJdk17Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.7");
  }

  @NotNull
  public static File getMockJdk18Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.8");
  }

  @NotNull
  public static File getMockJdk9Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.9");
  }

  public static String getMockJdkVersion(@NotNull String path) {
    String name = PathUtil.getFileName(path);
    if (name.startsWith(MOCK_JDK_DIR_NAME_PREFIX)) {
      return "java " + StringUtil.trimStart(name, MOCK_JDK_DIR_NAME_PREFIX);
    }
    return null;
  }

  @NotNull
  private static File getPathForJdkNamed(@NotNull String name) {
    return new File(PathManager.getCommunityHomePath(), "java/" + name);
  }

  @NotNull
  @Deprecated(message = "Use IdeaTestUtil.addWebJarsToModule instead")
  @ApiStatus.ScheduledForRemoval(inVersion = "2021.1")
  public static Sdk getWebMockJdk17() {
    Sdk jdk = getMockJdk17();
    jdk=addWebJarsTo(jdk);
    return jdk;
  }

  @NotNull
  @Contract(pure=true)
  @Deprecated(message = "Use IdeaTestUtil.addWebJarsToModule instead")
  @ApiStatus.ScheduledForRemoval(inVersion = "2021.1")<commit_msg>IdeaTestUtil: use java Deprecated instead of Kotlin's IDEA-CR-55323<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.testFramework;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.PathUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.lang.JavaVersion;
import org.jetbrains.annotations.*;
import org.junit.Assert;
import org.junit.Assume;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertTrue;

@TestOnly
public class IdeaTestUtil extends PlatformTestUtil {
  private static final String MOCK_JDK_DIR_NAME_PREFIX = "mockJDK-";

  @SuppressWarnings("UseOfSystemOutOrSystemErr")
  public static void printDetectedPerformanceTimings() {
    System.out.println(Timings.getStatistics());
  }

  public static void withLevel(@NotNull final Module module, @NotNull LanguageLevel level, @NotNull final Runnable r) {
    final LanguageLevelProjectExtension projectExt = LanguageLevelProjectExtension.getInstance(module.getProject());

    final LanguageLevel projectLevel = projectExt.getLanguageLevel();
    final LanguageLevel moduleLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
    try {
      projectExt.setLanguageLevel(level);
      setModuleLanguageLevel(module, level);
      r.run();
    }
    finally {
      setModuleLanguageLevel(module, moduleLevel);
      projectExt.setLanguageLevel(projectLevel);
    }
  }

  public static void setModuleLanguageLevel(@NotNull Module module, @Nullable LanguageLevel level) {
    ModuleRootModificationUtil.updateModel(module, (model) -> {
      model.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(level);
    });
  }

  public static void setModuleLanguageLevel(@NotNull Module module, @NotNull LanguageLevel level, @NotNull Disposable parentDisposable) {
    LanguageLevel prev = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
    setModuleLanguageLevel(module, level);
    Disposer.register(parentDisposable, () -> setModuleLanguageLevel(module, prev));
  }

  @NotNull
  public static Sdk getMockJdk(@NotNull JavaVersion version) {
    int mockJdk = version.feature >= 11 ? 11 :
                  version.feature >= 9 ? 9 :
                  version.feature >= 7 ? version.feature :
                  version.feature >= 5 ? 7 :
                  4;
    String path = getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + (mockJdk < 11 ? "1." : "") + mockJdk).getPath();
    return createMockJdk("java " + version, path);
  }

  @NotNull
  private static Sdk createMockJdk(@NotNull String name, @NotNull String path) {
    JavaSdk javaSdk = JavaSdk.getInstance();
    if (javaSdk == null) {
      throw new AssertionError("The test uses classes from Java plugin but Java plugin wasn't loaded; make sure that Java plugin " +
                               "classes are included into classpath and that the plugin isn't disabled by using 'idea.load.plugins', 'idea.load.plugins.id', 'idea.load.plugins.category' system properties");
    }
    return ((JavaSdkImpl)javaSdk).createMockJdk(name, path, false);
  }

  @NotNull
  public static Sdk getMockJdk14() {
    return getMockJdk(JavaVersion.compose(4));
  }

  @NotNull
  public static Sdk getMockJdk17() {
    return getMockJdk(JavaVersion.compose(7));
  }

  @NotNull
  public static Sdk getMockJdk17(@NotNull String name) {
    return createMockJdk(name, getMockJdk17Path().getPath());
  }

  @NotNull
  public static Sdk getMockJdk18() {
    return getMockJdk(JavaVersion.compose(8));
  }

  @NotNull
  public static Sdk getMockJdk9() {
    return getMockJdk(JavaVersion.compose(9));
  }

  @NotNull
  public static File getMockJdk14Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.4");
  }

  @NotNull
  public static File getMockJdk17Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.7");
  }

  @NotNull
  public static File getMockJdk18Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.8");
  }

  @NotNull
  public static File getMockJdk9Path() {
    return getPathForJdkNamed(MOCK_JDK_DIR_NAME_PREFIX + "1.9");
  }

  public static String getMockJdkVersion(@NotNull String path) {
    String name = PathUtil.getFileName(path);
    if (name.startsWith(MOCK_JDK_DIR_NAME_PREFIX)) {
      return "java " + StringUtil.trimStart(name, MOCK_JDK_DIR_NAME_PREFIX);
    }
    return null;
  }

  @NotNull
  private static File getPathForJdkNamed(@NotNull String name) {
    return new File(PathManager.getCommunityHomePath(), "java/" + name);
  }

  /**
   * @deprecated {@link IdeaTestUtil#addWebJarsToModule(Module)} instead
   */
  @Deprecated
  @ApiStatus.ScheduledForRemoval(inVersion = "2021.1")
  public static Sdk getWebMockJdk17() {
    Sdk jdk = getMockJdk17();
    jdk=addWebJarsTo(jdk);
    return jdk;
  }

  /**
   * @deprecated {@link IdeaTestUtil#addWebJarsToModule(Module)} instead
   */
  @NotNull
  @Contract(pure=true)
  @Deprecated
  @ApiStatus.ScheduledForRemoval(inVersion = "2021.1")
{'fraction_non_alphanumeric': 0.10114684832284211, 'fraction_numerical': 0.011985858411658187, 'mean_word_length': 4.3694444444444445, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6541510', 'n_tokens_mistral': 3724, 'n_tokens_neox': 3562, 'n_words': 763}