mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
maven: 'increase language level' quickfix modifies pom.xml
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.core.JavaCoreBundle;
|
||||
import com.intellij.openapi.roots.LanguageLevelModuleExtension;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -29,24 +30,26 @@ import org.jetbrains.annotations.Nullable;
|
||||
* @see LanguageLevelModuleExtension
|
||||
*/
|
||||
public enum LanguageLevel {
|
||||
JDK_1_3("Java 1.3", JavaCoreBundle.message("jdk.1.3.language.level.description")),
|
||||
JDK_1_4("Java 1.4", JavaCoreBundle.message("jdk.1.4.language.level.description")),
|
||||
JDK_1_5("Java 5.0", JavaCoreBundle.message("jdk.1.5.language.level.description")),
|
||||
JDK_1_6("Java 6", JavaCoreBundle.message("jdk.1.6.language.level.description")),
|
||||
JDK_1_7("Java 7", JavaCoreBundle.message("jdk.1.7.language.level.description")),
|
||||
JDK_1_8("Java 8", JavaCoreBundle.message("jdk.1.8.language.level.description")),
|
||||
JDK_1_9("Java 9", JavaCoreBundle.message("jdk.1.9.language.level.description")),
|
||||
JDK_X("Java X", JavaCoreBundle.message("jdk.X.language.level.description"));
|
||||
JDK_1_3("Java 1.3", JavaCoreBundle.message("jdk.1.3.language.level.description"), "1.3"),
|
||||
JDK_1_4("Java 1.4", JavaCoreBundle.message("jdk.1.4.language.level.description"), "1.4"),
|
||||
JDK_1_5("Java 5.0", JavaCoreBundle.message("jdk.1.5.language.level.description"), "1.5"),
|
||||
JDK_1_6("Java 6", JavaCoreBundle.message("jdk.1.6.language.level.description"), "1.6"),
|
||||
JDK_1_7("Java 7", JavaCoreBundle.message("jdk.1.7.language.level.description"), "1.7"),
|
||||
JDK_1_8("Java 8", JavaCoreBundle.message("jdk.1.8.language.level.description"), "1.8"),
|
||||
JDK_1_9("Java 9", JavaCoreBundle.message("jdk.1.9.language.level.description"), "1.9"),
|
||||
JDK_X("Java X", JavaCoreBundle.message("jdk.X.language.level.description"), "");
|
||||
|
||||
public static final LanguageLevel HIGHEST = JDK_1_8; // TODO! when language level 9 is really supported, update this field
|
||||
public static final Key<LanguageLevel> KEY = Key.create("LANGUAGE_LEVEL");
|
||||
|
||||
private final String myName;
|
||||
private final String myPresentableText;
|
||||
private final String myCompilerComplianceOption;
|
||||
|
||||
LanguageLevel(@NotNull String name, @NotNull @Nls String presentableText) {
|
||||
LanguageLevel(@NotNull String name, @NotNull @Nls String presentableText, @NotNull String compilerComplianceOption) {
|
||||
myName = name;
|
||||
myPresentableText = presentableText;
|
||||
myCompilerComplianceOption = compilerComplianceOption;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -68,16 +71,22 @@ public enum LanguageLevel {
|
||||
return compareTo(level) < 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static LanguageLevel parse(@Nullable String value) {
|
||||
if ("1.3".equals(value)) return JDK_1_3;
|
||||
if ("1.4".equals(value)) return JDK_1_4;
|
||||
if ("1.5".equals(value)) return JDK_1_5;
|
||||
if ("1.6".equals(value)) return JDK_1_6;
|
||||
if ("1.7".equals(value)) return JDK_1_7;
|
||||
if ("1.8".equals(value)) return JDK_1_8;
|
||||
if ("1.9".equals(value)) return JDK_1_9;
|
||||
/**
|
||||
* String representation of the level, suitable to pass as a value of compiler's "-source" and "-target" options
|
||||
*/
|
||||
public String getCompilerComplianceOption() {
|
||||
return myCompilerComplianceOption;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static LanguageLevel parse(@Nullable String compilerComplianceOption) {
|
||||
if (StringUtil.isEmpty(compilerComplianceOption)) return null;
|
||||
|
||||
for (LanguageLevel level : values()) {
|
||||
if (level.getCompilerComplianceOption().equals(compilerComplianceOption)) {
|
||||
return level;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+50
-4
@@ -30,14 +30,14 @@ import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.psi.xml.XmlFile;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.text.VersionComparatorUtil;
|
||||
import com.intellij.util.xml.DomUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.concurrency.Promise;
|
||||
import org.jetbrains.idea.maven.dom.MavenDomUtil;
|
||||
import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
|
||||
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
|
||||
import org.jetbrains.idea.maven.dom.model.*;
|
||||
import org.jetbrains.idea.maven.indices.MavenProjectIndicesManager;
|
||||
import org.jetbrains.idea.maven.model.MavenArtifact;
|
||||
import org.jetbrains.idea.maven.model.MavenConstants;
|
||||
@@ -154,8 +154,54 @@ public class MavenProjectModelModifier extends JavaProjectModelModifier {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull LanguageLevel level) {
|
||||
return null;
|
||||
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull final LanguageLevel level) {
|
||||
if (!myProjectsManager.isMavenizedModule(module)) return null;
|
||||
|
||||
MavenProject mavenProject = myProjectsManager.findProject(module);
|
||||
if (mavenProject == null) return null;
|
||||
|
||||
final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
|
||||
if (model == null) return null;
|
||||
|
||||
new WriteCommandAction(myProject, "Add Maven Dependency", DomUtil.getFile(model)) {
|
||||
@Override
|
||||
protected void run(@NotNull Result result) throws Throwable {
|
||||
MavenDomConfiguration configuration = getCompilerPlugin(model).getConfiguration();
|
||||
XmlTag tag = configuration.ensureTagExists();
|
||||
setChildTagValue(tag, "source", level.getCompilerComplianceOption());
|
||||
setChildTagValue(tag, "target", level.getCompilerComplianceOption());
|
||||
Document document = PsiDocumentManager.getInstance(myProject).getDocument(DomUtil.getFile(model));
|
||||
if (document != null) {
|
||||
FileDocumentManager.getInstance().saveDocument(document);
|
||||
}
|
||||
}
|
||||
}.execute();
|
||||
return myProjectsManager.forceUpdateProjects(Collections.singleton(mavenProject));
|
||||
}
|
||||
|
||||
private static void setChildTagValue(@NotNull XmlTag tag, @NotNull String subTagName, @NotNull String value) {
|
||||
XmlTag subTag = tag.findFirstSubTag(subTagName);
|
||||
if (subTag != null) {
|
||||
subTag.getValue().setText(value);
|
||||
}
|
||||
else {
|
||||
tag.addSubTag(tag.createChildTag(subTagName, tag.getNamespace(), value, false), false);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static MavenDomPlugin getCompilerPlugin(MavenDomProjectModel model) {
|
||||
MavenDomPlugins plugins = model.getBuild().getPlugins();
|
||||
for (MavenDomPlugin plugin : plugins.getPlugins()) {
|
||||
if ("org.apache.maven.plugins".equals(plugin.getGroupId().getValue()) &&
|
||||
"maven-compiler-plugin".equals(plugin.getArtifactId().getValue())) {
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
MavenDomPlugin plugin = plugins.addPlugin();
|
||||
plugin.getGroupId().setValue("org.apache.maven.plugins");
|
||||
plugin.getArtifactId().setValue("maven-compiler-plugin");
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
+25
@@ -15,13 +15,17 @@
|
||||
*/
|
||||
package org.jetbrains.idea.maven.actions;
|
||||
|
||||
import com.intellij.openapi.module.EffectiveLanguageLevelUtil;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.roots.DependencyScope;
|
||||
import com.intellij.openapi.roots.ExternalLibraryDescriptor;
|
||||
import com.intellij.openapi.roots.JavaProjectModelModifier;
|
||||
import com.intellij.openapi.roots.libraries.Library;
|
||||
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.concurrency.Promise;
|
||||
@@ -98,6 +102,27 @@ public class MavenProjectModelModifierTest extends MavenDomWithIndicesTestCase {
|
||||
assertModuleLibDep("m1", libName);
|
||||
}
|
||||
|
||||
public void testChangeLanguageLevel() throws IOException {
|
||||
importProject("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
"<version>1</version>");
|
||||
|
||||
Module module = getModule("project");
|
||||
assertEquals(LanguageLevel.JDK_1_5, EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(module));
|
||||
Promise<Void> result = getExtension().changeLanguageLevel(module, LanguageLevel.JDK_1_8);
|
||||
assertNotNull(result);
|
||||
XmlTag tag = findTag("project.build.plugins.plugin");
|
||||
assertNotNull(tag);
|
||||
assertEquals("maven-compiler-plugin", tag.getSubTagText("artifactId"));
|
||||
XmlTag configuration = tag.findFirstSubTag("configuration");
|
||||
assertNotNull(configuration);
|
||||
assertEquals("1.8", configuration.getSubTagText("source"));
|
||||
assertEquals("1.8", configuration.getSubTagText("target"));
|
||||
|
||||
waitUntilImported(result);
|
||||
assertEquals(LanguageLevel.JDK_1_8, EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(module));
|
||||
}
|
||||
|
||||
private void createTwoModulesPom(final String m1, final String m2) throws IOException {
|
||||
createProjectPom("<groupId>test</groupId>" +
|
||||
"<artifactId>project</artifactId>" +
|
||||
|
||||
Reference in New Issue
Block a user