Example #1
0
 public CommentFormatter(@NotNull Project project) {
   mySettings = CodeStyleSettingsManager.getSettings(project);
   myParser =
       new JDParser(
           mySettings, LanguageLevelProjectExtension.getInstance(project).getLanguageLevel());
   myProject = project;
 }
  public static void withLevel(final Module module, final LanguageLevel level, final Runnable r) {
    final LanguageLevelProjectExtension projectExt =
        LanguageLevelProjectExtension.getInstance(module.getProject());

    final LanguageLevel projectLevel = projectExt.getLanguageLevel();
    final LanguageLevel moduleLevel =
        LanguageLevelModuleExtension.getInstance(module).getLanguageLevel();
    try {
      projectExt.setLanguageLevel(level);
      setModuleLanguageLevel(module, level);
      r.run();
    } finally {
      setModuleLanguageLevel(module, moduleLevel);
      projectExt.setLanguageLevel(projectLevel);
    }
  }
 @Override
 public void setUp() throws Exception {
   super.setUp();
   doTest(
       "@interface A { int i() default 42; }\n class C { void m(int p) throws E { } }",
       null); // warm up
   LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.HIGHEST);
 }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    LanguageLevelProjectExtension.getInstance(myProject).setLanguageLevel(LanguageLevel.JDK_1_5);
    String root = PathManagerEx.getTestDataPath() + "/psi/repositoryUse/src15";
    PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17("mock 1.5"));
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
  private static LanguageLevel getLanguageLevelInner(@NotNull PsiDirectory dir) {
    final VirtualFile virtualFile = dir.getVirtualFile();
    final Project project = dir.getProject();
    final Module module =
        ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile);
    if (module != null) {
      return LanguageLevelUtil.getEffectiveLanguageLevel(module);
    }

    return LanguageLevelProjectExtension.getInstance(project).getLanguageLevel();
  }
  @Test
  @IdeGuiTest
  public void testLanguageLevelForApi21() {
    // Verifies that creating a project with L will set the language level correctly
    // both in the generated Gradle model as well as in the synced project and modules

    // "20+" here should change to 21 as soon as L goes out of preview state
    IdeFrameFixture projectFrame =
        newProject("Test Application")
            .withMinSdk("20+")
            // just to speed up the test: type as little as possible
            .withActivity("A")
            .withCompanyDomain("C")
            .withName("P")
            .withPackageName("a.b")
            .create();

    IdeaAndroidProject appAndroidProject = projectFrame.getAndroidProjectForModule("app");
    AndroidProject model = appAndroidProject.getDelegate();
    ApiVersion minSdkVersion = model.getDefaultConfig().getProductFlavor().getMinSdkVersion();
    assertNotNull("minSdkVersion", minSdkVersion);

    // If this test fails, verify that
    //   (1) you have the L preview installed in the SDK on the test machine
    //   (2) the associated JDK is JDK 7 or higher
    assertThat(minSdkVersion.getApiString()).as("minSdkVersion API").isEqualTo("L");
    assertThat(appAndroidProject.getJavaLanguageLevel())
        .as("Gradle Java language level")
        .isSameAs(LanguageLevel.JDK_1_7);
    LanguageLevelProjectExtension projectExt =
        LanguageLevelProjectExtension.getInstance(projectFrame.getProject());
    assertThat(projectExt.getLanguageLevel())
        .as("Project Java language level")
        .isSameAs(LanguageLevel.JDK_1_7);
    for (Module module : ModuleManager.getInstance(projectFrame.getProject()).getModules()) {
      LanguageLevelModuleExtension moduleExt = LanguageLevelModuleExtension.getInstance(module);
      assertThat(moduleExt.getLanguageLevel())
          .as("Gradle Java language level in module " + module.getName())
          .isNull();
    }
  }
  @Test
  @IdeGuiTest
  public void testCreateNewMobileProject() {
    IdeFrameFixture projectFrame = newProject("Test Application").create();
    FileFixture layoutFile =
        projectFrame.findExistingFileByRelativePath("app/src/main/res/layout/activity_main.xml");
    layoutFile.requireOpenAndSelected();

    // Verify state of project
    projectFrame.requireModuleCount(2);
    IdeaAndroidProject appAndroidProject = projectFrame.getAndroidProjectForModule("app");
    assertThat(appAndroidProject.getVariantNames()).as("variants").containsOnly("debug", "release");
    assertThat(appAndroidProject.getSelectedVariant().getName())
        .as("selected variant")
        .isEqualTo("debug");

    AndroidProject model = appAndroidProject.getDelegate();
    ApiVersion minSdkVersion = model.getDefaultConfig().getProductFlavor().getMinSdkVersion();
    assertNotNull("minSdkVersion", minSdkVersion);
    assertThat(minSdkVersion.getApiString()).as("minSdkVersion API").isEqualTo("19");

    // Creating a project with minSdkVersion 19 should leave the Java language level as Java 6
    // For L and higher we use Java 7 language level; that is tested separately in
    // testLanguageLevelForApi21
    assertThat(appAndroidProject.getJavaLanguageLevel())
        .as("Gradle Java language level")
        .isSameAs(LanguageLevel.JDK_1_6);
    LanguageLevelProjectExtension projectExt =
        LanguageLevelProjectExtension.getInstance(projectFrame.getProject());
    assertThat(projectExt.getLanguageLevel())
        .as("Project Java language level")
        .isSameAs(LanguageLevel.JDK_1_6);
    for (Module module : ModuleManager.getInstance(projectFrame.getProject()).getModules()) {
      LanguageLevelModuleExtension moduleExt = LanguageLevelModuleExtension.getInstance(module);
      assertThat(moduleExt.getLanguageLevel())
          .as("Gradle Java language level in module " + module.getName())
          .isNull();
    }
  }
 @Override
 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file)
     throws IncorrectOperationException {
   final VirtualFile virtualFile = file.getVirtualFile();
   LOG.assertTrue(virtualFile != null);
   final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
   final LanguageLevel moduleLevel =
       module == null
           ? null
           : LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
   if (moduleLevel != null && isLanguageLevelAcceptable(project, module, myLevel)) {
     final ModifiableRootModel rootModel =
         ModuleRootManager.getInstance(module).getModifiableModel();
     rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLevel);
     rootModel.commit();
   } else {
     LanguageLevelProjectExtension.getInstance(project).setLanguageLevel(myLevel);
     ProjectRootManagerEx.getInstanceEx(project)
         .makeRootsChange(EmptyRunnable.INSTANCE, false, true);
   }
 }
 @Override
 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file)
     throws IncorrectOperationException {
   final VirtualFile virtualFile = file.getVirtualFile();
   LOG.assertTrue(virtualFile != null);
   final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
   final LanguageLevel moduleLevel =
       module == null ? null : LanguageLevelModuleExtension.getInstance(module).getLanguageLevel();
   if (moduleLevel != null && isLanguageLevelAcceptable(project, module, myLevel)) {
     final ModifiableRootModel rootModel =
         ModuleRootManager.getInstance(module).getModifiableModel();
     rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLevel);
     ApplicationManager.getApplication()
         .runWriteAction(
             new Runnable() {
               @Override
               public void run() {
                 rootModel.commit();
               }
             });
   } else {
     LanguageLevelProjectExtension.getInstance(project).setLanguageLevel(myLevel);
   }
 }
  @NotNull
  public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) {

    final Set<String> languageLevels = new HashSet<String>();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
      final LanguageLevelModuleExtension instance =
          LanguageLevelModuleExtension.getInstance(module);
      final LanguageLevel languageLevel = instance.getLanguageLevel();
      if (languageLevel != null) {
        languageLevels.add(languageLevel.getPresentableText());
      }
    }
    languageLevels.add(
        LanguageLevelProjectExtension.getInstance(project).getLanguageLevel().getPresentableText());

    return ContainerUtil.map2Set(
        languageLevels,
        new Function<String, UsageDescriptor>() {
          @Override
          public UsageDescriptor fun(String languageLevel) {
            return new UsageDescriptor(languageLevel, 1);
          }
        });
  }
 @Override
 protected void tearDown() throws Exception {
   LanguageLevelProjectExtension.getInstance(myProject).setLanguageLevel(LanguageLevel.JDK_1_5);
   super.tearDown();
 }
 public void testSCR36110() throws Exception {
   JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
   LanguageLevel old =
       LanguageLevelProjectExtension.getInstance(facade.getProject()).getLanguageLevel();
   doTest();
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.HIGHEST);
 }
 public void testTypeParamFromDiamond() throws Exception {
   LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
   doSingleFileTest();
 }
 public void testLastResourceVariableWithFinallyBlock() throws Exception {
   LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
   doSingleFileTest();
 }
 protected Sdk getTestProjectSdk() {
   Sdk sdk = JavaSdkImpl.getMockJdk17();
   LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
   return sdk;
 }