public static void setModuleLanguageLevel(Module module, final LanguageLevel level) { final LanguageLevelModuleExtension modifiable = (LanguageLevelModuleExtension) LanguageLevelModuleExtension.getInstance(module).getModifiableModel(true); modifiable.setLanguageLevel(level); modifiable.commit(); }
@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(); } }
@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); } }); }
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 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); } }