com.intellij.openapi.roots.LanguageLevelProjectExtension Java Examples

The following examples show how to use com.intellij.openapi.roots.LanguageLevelProjectExtension. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlazeAndroidSyncPlugin.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static void setProjectSdkAndLanguageLevel(
    final Project project, final Sdk sdk, final LanguageLevel javaLanguageLevel) {
  UIUtil.invokeAndWaitIfNeeded(
      (Runnable)
          () ->
              ApplicationManager.getApplication()
                  .runWriteAction(
                      () -> {
                        ProjectRootManagerEx rootManager =
                            ProjectRootManagerEx.getInstanceEx(project);
                        rootManager.setProjectSdk(sdk);
                        LanguageLevelProjectExtension ext =
                            LanguageLevelProjectExtension.getInstance(project);
                        ext.setLanguageLevel(javaLanguageLevel);
                      }));
}
 
Example #2
Source File: BlazeAndroidSyncPluginTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateProjectSdkWithoutSyncData() {
  // Setup.
  BlazeProjectData blazeProjectData =
      MockBlazeProjectDataBuilder.builder(workspaceRoot)
          .setWorkspaceLanguageSettings(
              new WorkspaceLanguageSettings(WorkspaceType.ANDROID, ImmutableSet.of()))
          .build();

  // Perform.
  syncPlugin.updateProjectSdk(project, context, projectViewSet, null, blazeProjectData);

  // Verify.
  ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
  LanguageLevel languageLevel =
      LanguageLevelProjectExtension.getInstance(project).getLanguageLevel();

  // Even when sync data is null, the project sdk should still be available.
  assertThat(rootManager.getProjectSdk().getName()).isEqualTo("android-sdk-28");
  assertThat(languageLevel).isEqualTo(LanguageLevel.JDK_1_8);
}
 
Example #3
Source File: BlazeAndroidSyncPluginTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateProjectSdkWithoutSyncDataDoesNotOverrideSdkIfOneAlreadyExists() {
  // Setup.
  BlazeProjectData blazeProjectData =
      MockBlazeProjectDataBuilder.builder(workspaceRoot)
          .setWorkspaceLanguageSettings(
              new WorkspaceLanguageSettings(WorkspaceType.ANDROID, ImmutableSet.of()))
          .build();
  ProjectRootManagerEx.getInstanceEx(project)
      .setProjectSdk(BlazeSdkProvider.getInstance().findSdk(MOCK_ANDROID_SDK_TARGET_HASH_26));

  // Perform.
  syncPlugin.updateProjectSdk(project, context, projectViewSet, null, blazeProjectData);

  // Verify.
  ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
  LanguageLevel languageLevel =
      LanguageLevelProjectExtension.getInstance(project).getLanguageLevel();

  // Even when sync data is null, the project sdk should still be available.  In this case
  // an sdk is already available, so it's not reset from project view.
  assertThat(rootManager.getProjectSdk().getName()).isEqualTo("android-sdk-26");
  assertThat(languageLevel).isNull();
}
 
Example #4
Source File: IntellijPluginSyncPlugin.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public void updateProjectSdk(
    Project project,
    BlazeContext context,
    ProjectViewSet projectViewSet,
    BlazeVersionData blazeVersionData,
    BlazeProjectData blazeProjectData) {
  if (!blazeProjectData
      .getWorkspaceLanguageSettings()
      .isWorkspaceType(WorkspaceType.INTELLIJ_PLUGIN)) {
    return;
  }

  LanguageLevel javaLanguageLevel =
      JavaLanguageLevelHelper.getJavaLanguageLevel(
          projectViewSet, blazeProjectData, LanguageLevel.JDK_1_8);

  // Leave the SDK, but set the language level
  Transactions.submitWriteActionTransactionAndWait(
      () -> {
        LanguageLevelProjectExtension ext = LanguageLevelProjectExtension.getInstance(project);
        ext.setLanguageLevel(javaLanguageLevel);
      });
}
 
Example #5
Source File: MavenTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
protected void setUpFixtures() throws Exception {
  //myTestFixture = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName()).getFixture();
  //myTestFixture.setUp();
  myProjectBuilder = IdeaTestFixtureFactory.getFixtureFactory().createFixtureBuilder(getName());
  final JavaTestFixtureFactory factory = JavaTestFixtureFactory.getFixtureFactory();
  ModuleFixtureBuilder moduleBuilder = myProjectBuilder.addModule(JavaModuleFixtureBuilder.class);
  myTestFixture = factory.createCodeInsightFixture(myProjectBuilder.getFixture());
  myTestFixture.setUp();
  LanguageLevelProjectExtension.getInstance(myTestFixture.getProject()).setLanguageLevel(LanguageLevel.JDK_1_6);
}
 
Example #6
Source File: BlazeAndroidSyncPluginTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
/**
 * Initialized blaze context, project view set, and registers the following services:
 *
 * <p>A mock sdk provider with 2 registered SDKs: android-26 and android-28. 2 SDKs are registered
 * because the test will make use of both to verify the correct one is selected.
 *
 * <p>A {@link MockProjectRootManagerEx} service. Note that it's registered on the {@link
 * ProjectRootManager} component instead of {@link ProjectRootManagerEx}. This is due to the way
 * {@link ProjectRootManagerEx} obtains its own instance. See {@link
 * ProjectRootManagerEx#getInstance(Project)} for more details.
 *
 * <p>A {@link MockLanguageLevelProjectExtension} service. This is to stop {@link
 * BlazeAndroidSyncPlugin#setProjectSdkAndLanguageLevel(Project, Sdk, LanguageLevel)} from
 * throwing NPEs because it makes use of that service.
 */
@Override
protected void initTest(Container applicationServices, Container projectServices) {
  super.initTest(applicationServices, projectServices);
  MockBlazeSdkProvider mockBlazeSdkProvider = new MockBlazeSdkProvider();
  applicationServices.register(BlazeSdkProvider.class, mockBlazeSdkProvider);
  LightWeightMockSdkUtil.registerSdk(
      MOCK_ANDROID_SDK_TARGET_HASH_26, "android-sdk-26", mockBlazeSdkProvider);
  LightWeightMockSdkUtil.registerSdk(
      MOCK_ANDROID_SDK_TARGET_HASH_28, "android-sdk-28", mockBlazeSdkProvider);

  projectServices.register(ProjectRootManager.class, new MockProjectRootManagerEx());

  projectServices.register(
      LanguageLevelProjectExtension.class, new MockLanguageLevelProjectExtension());

  context = new BlazeContext();
  context.addOutputSink(IssueOutput.class, new ErrorCollector());

  projectViewSet =
      ProjectViewSet.builder()
          .add(
              ProjectView.builder()
                  .add(ScalarSection.builder(AndroidSdkPlatformSection.KEY).set("android-28"))
                  .build())
          .build();
}
 
Example #7
Source File: BlazeAndroidSyncPluginTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateProjectSdkWithSyncData() {
  // Setup.
  SyncState syncStateWithAndroidSdk26AndJava9 =
      new SyncState.Builder()
          .put(new BlazeAndroidSyncData(null, new AndroidSdkPlatform("android-26", 0)))
          .put(
              new BlazeJavaSyncData(
                  BlazeJavaImportResult.builder()
                      .setContentEntries(ImmutableList.of())
                      .setLibraries(ImmutableMap.of())
                      .setBuildOutputJars(ImmutableList.of())
                      .setJavaSourceFiles(ImmutableSet.of())
                      .setSourceVersion("9")
                      .setEmptyJarTracker(EmptyJarTracker.builder().build())
                      .build(),
                  null))
          .build();

  BlazeProjectData blazeProjectData =
      MockBlazeProjectDataBuilder.builder(workspaceRoot)
          .setWorkspaceLanguageSettings(
              new WorkspaceLanguageSettings(WorkspaceType.ANDROID, ImmutableSet.of()))
          .setSyncState(syncStateWithAndroidSdk26AndJava9)
          .build();

  // Perform.
  syncPlugin.updateProjectSdk(project, context, projectViewSet, null, blazeProjectData);

  // Verify.
  ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
  LanguageLevel languageLevel =
      LanguageLevelProjectExtension.getInstance(project).getLanguageLevel();

  // Should return android-26 even though project view says android-28 because data returned from
  // sync takes higher priority.
  assertThat(rootManager.getProjectSdk().getName()).isEqualTo("android-sdk-26");
  // Defaults to JDK 1.8, but sync result specifies 1.9, which takes higher priority.
  assertThat(languageLevel).isEqualTo(LanguageLevel.JDK_1_9);
}
 
Example #8
Source File: AndroidSyncTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void assertSyncSuccess(TargetMap targetMap, VirtualFile javaRoot) {
  errorCollector.assertNoIssues();
  BlazeProjectData blazeProjectData =
      BlazeProjectDataManager.getInstance(getProject()).getBlazeProjectData();
  assertThat(blazeProjectData).isNotNull();
  assertThat(blazeProjectData.getTargetMap()).isEqualTo(targetMap);
  assertThat(blazeProjectData.getWorkspaceLanguageSettings().getWorkspaceType())
      .isEqualTo(WorkspaceType.ANDROID);

  ImmutableList<ContentEntry> contentEntries = getWorkspaceContentEntries();
  assertThat(contentEntries).hasSize(1);
  assertThat(findContentEntry(javaRoot)).isNotNull();
  assertThat(findContentEntry(javaRoot).getSourceFolders()).hasLength(1);

  // Check that the workspace is set to android
  Module workspaceModule =
      ModuleFinder.getInstance(getProject())
          .findModuleByName(BlazeDataStorage.WORKSPACE_MODULE_NAME);
  assertThat(workspaceModule).isNotNull();
  assertThat(AndroidFacet.getInstance(workspaceModule)).isNotNull();

  // Check that a resource module was created
  Module resourceModule =
      ModuleFinder.getInstance(getProject()).findModuleByName("java.com.google.lib");
  assertThat(resourceModule).isNotNull();
  assertThat(AndroidFacet.getInstance(resourceModule)).isNotNull();

  // Check that a module was created for the run configuration
  Module appModule =
      ModuleFinder.getInstance(getProject()).findModuleByName("java.com.android.app");
  if (BlazeAndroidProjectStructureSyncer.deprecateRunConfigModuleExperiment.isEnabled()) {
    assertThat(appModule).isNull();
  } else {
    assertThat(appModule).isNotNull();
  }

  // The default language level should be whatever is specified in the toolchain info
  assertThat(LanguageLevelProjectExtension.getInstance(getProject()).getLanguageLevel())
      .isEqualTo(LanguageLevel.JDK_1_8);
}
 
Example #9
Source File: BlazeJavaSyncPlugin.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void setProjectSdkAndLanguageLevel(
    final Project project, final Sdk sdk, final LanguageLevel javaLanguageLevel) {
  Transactions.submitWriteActionTransactionAndWait(
      () -> {
        ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
        rootManager.setProjectSdk(sdk);
        LanguageLevelProjectExtension ext = LanguageLevelProjectExtension.getInstance(project);
        ext.setLanguageLevel(javaLanguageLevel);
      });
}
 
Example #10
Source File: OSSPantsIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Assert Project has the right JDK and language level (JVM project only).
 */
protected void assertProjectJdkAndLanguageLevel() {
  final String pantsExecutablePath = PantsUtil.findPantsExecutable(getProjectPath()).get().getPath();
  assertEquals(
    ProjectRootManager.getInstance(myProject).getProjectSdk().getHomePath(),
    getDefaultJavaSdk(pantsExecutablePath).get().getHomePath()
  );

  LanguageLevel projectLanguageLevel = LanguageLevelProjectExtension.getInstance(myProject).getLanguageLevel();
  LanguageLevel expectedLanguageLevel = LanguageLevel.JDK_1_8;
  assertTrue(
    String.format("Project Language Level should be %s, but is %s", expectedLanguageLevel, projectLanguageLevel),
    projectLanguageLevel.equals(LanguageLevel.JDK_1_8)
  );
}