You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ne...@apache.org on 2019/07/03 10:53:42 UTC
[netbeans] branch master updated: [NETBEANS-2457]: Provided Error
Hint to turn on Preview Features in ant/maven Java projects (#1285)
This is an automated email from the ASF dual-hosted git repository.
neilcsmith pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new f98af58 [NETBEANS-2457]: Provided Error Hint to turn on Preview Features in ant/maven Java projects (#1285)
f98af58 is described below
commit f98af58ebcf87fed38823e62a5696819f8595f29
Author: Arunava Sinha <ar...@oracle.com>
AuthorDate: Wed Jul 3 16:23:38 2019 +0530
[NETBEANS-2457]: Provided Error Hint to turn on Preview Features in ant/maven Java projects (#1285)
* [NETBEANS-2457]: Provided Error Hint to turn on Preview Features in ant/maven project
* [NETBEANS-2457]: Refactored Error Hint to turn on Preview Features in ant/maven project
---
.../java/hints/errors/EnablePreviewAntProj.java | 217 ++++++++++++++++
.../modules/apisupport/project/resources/layer.xml | 1 +
.../modules/maven/hints/errors/Bundle.properties | 1 +
.../maven/hints/errors/EnablePreviewMavenProj.java | 286 +++++++++++++++++++++
.../src/org/netbeans/modules/maven/hints/layer.xml | 1 +
5 files changed, 506 insertions(+)
diff --git a/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/java/hints/errors/EnablePreviewAntProj.java b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/java/hints/errors/EnablePreviewAntProj.java
new file mode 100644
index 0000000..7b53fb5
--- /dev/null
+++ b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/java/hints/errors/EnablePreviewAntProj.java
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.apisupport.project.java.hints.errors;
+
+import com.sun.source.util.TreePath;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.lang.model.SourceVersion;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.modules.java.hints.spi.ErrorRule;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.Fix;
+import org.openide.filesystems.FileObject;
+import org.openide.util.NbBundle;
+import org.openide.util.Parameters;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectManager;
+import org.netbeans.spi.project.support.ant.AntProjectHelper;
+import org.openide.util.EditableProperties;
+import org.openide.util.Mutex;
+import org.openide.util.MutexException;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Ant type project.
+ *
+ * @author arusinha
+ */
+
+@NbBundle.Messages({
+ "FIX_EnablePreviewFeature=Enable Preview Feature" // NOI18N
+})
+public class EnablePreviewAntProj implements ErrorRule<Void> {
+
+ private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
+ "compiler.err.preview.feature.disabled.plural")); // NOI18N
+ private static final String ENABLE_PREVIEW_FLAG = "--enable-preview"; // NOI18N
+ private static final String JAVAC_COMPILER_ARGS = "javac.compilerargs"; // NOI18N
+ private static final String RUN_JVMARGS = "run.jvmargs"; // NOI18N
+
+ @Override
+ public Set<String> getCodes() {
+ return Collections.unmodifiableSet(ERROR_CODES);
+ }
+
+ @Override
+ @NonNull
+ public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+
+ System.out.println("ANT projectSourceVersion.latest() =="+ SourceVersion.latest() );
+ System.out.println("ANT compilationInfo.getSourceVersion() =="+ compilationInfo.getSourceVersion());
+ if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
+ return Collections.<Fix>emptyList();
+ }
+
+ final FileObject file = compilationInfo.getFileObject();
+ Fix fix = null;
+ if (file != null) {
+ final Project prj = FileOwnerQuery.getOwner(file);
+
+ if (isAntProject(prj)) {
+ fix = new EnablePreviewAntProj.ResolveAntFix(prj);
+ } else {
+ fix = null;
+ }
+ }
+ return (fix != null) ? Collections.<Fix>singletonList(fix) : Collections.<Fix>emptyList();
+ }
+
+ @Override
+ public String getId() {
+ return EnablePreviewAntProj.class.getName();
+ }
+
+ @Override
+ public String getDisplayName() {
+ return NbBundle.getMessage(EnablePreviewAntProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+ }
+
+ public String getDescription() {
+ return NbBundle.getMessage(EnablePreviewAntProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+ }
+
+ @Override
+ public void cancel() {
+ }
+
+ private static final class ResolveAntFix implements Fix {
+
+ private final Project prj;
+
+ ResolveAntFix(@NonNull final Project prj) {
+ Parameters.notNull("prj", prj); //NOI18N
+ this.prj = prj;
+ }
+
+ @Override
+ public String getText() {
+ return NbBundle.getMessage(EnablePreviewAntProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+ }
+
+ @Override
+ public ChangeInfo implement() throws Exception {
+
+ EditableProperties ep = getEditableProperties(prj, AntProjectHelper.PROJECT_PROPERTIES_PATH);
+
+ String compilerArgs = ep.getProperty(JAVAC_COMPILER_ARGS);
+ compilerArgs = compilerArgs != null ? compilerArgs + " " + ENABLE_PREVIEW_FLAG : ENABLE_PREVIEW_FLAG;
+
+ String runJVMArgs = ep.getProperty(RUN_JVMARGS);
+ runJVMArgs = runJVMArgs != null ? runJVMArgs + " " + ENABLE_PREVIEW_FLAG : ENABLE_PREVIEW_FLAG;
+
+ ep.setProperty(JAVAC_COMPILER_ARGS, compilerArgs);
+ ep.setProperty(RUN_JVMARGS, runJVMArgs);
+ storeEditableProperties(prj, AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
+ return null;
+ }
+
+ }
+
+ private static void storeEditableProperties(final Project prj, final String propertiesPath, final EditableProperties ep)
+ throws IOException {
+ try {
+ ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
+ @Override
+ public Void run() throws IOException {
+ FileObject propertiesFo = prj.getProjectDirectory().getFileObject(propertiesPath);
+ if (propertiesFo != null) {
+ OutputStream os = null;
+ try {
+ os = propertiesFo.getOutputStream();
+ ep.store(os);
+ } finally {
+ if (os != null) {
+ os.close();
+ }
+ }
+ }
+ return null;
+ }
+ });
+ } catch (MutexException ex) {
+ }
+ }
+
+ private static EditableProperties getEditableProperties(final Project prj, final String propertiesPath)
+ throws IOException {
+ try {
+ return ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<EditableProperties>() {
+ @Override
+ public EditableProperties run() throws IOException {
+ FileObject propertiesFo = prj.getProjectDirectory().getFileObject(propertiesPath);
+ EditableProperties ep = null;
+ if (propertiesFo != null) {
+ InputStream is = null;
+ ep = new EditableProperties(false);
+ try {
+ is = propertiesFo.getInputStream();
+ ep.load(is);
+ } finally {
+ if (is != null) {
+ is.close();
+ }
+ }
+ }
+ return ep;
+ }
+ });
+ } catch (MutexException ex) {
+ return null;
+ }
+ }
+
+ private boolean isAntProject(Project prj) {
+
+ List<FileObject> antProjectFiles = new ArrayList();
+ antProjectFiles.add(prj.getProjectDirectory().getFileObject("build.xml")); // NOI18N
+ antProjectFiles.add(prj.getProjectDirectory().getFileObject("nbproject/project.properties")); // NOI18N
+ antProjectFiles.add(prj.getProjectDirectory().getFileObject("nbproject/project.xml")); // NOI18N
+ boolean isAntProject = true;
+ for (FileObject file : antProjectFiles) {
+ if (!(file != null && file.isValid())) {
+ isAntProject = false;
+ break;
+ }
+ }
+
+ return isAntProject;
+
+ }
+
+}
diff --git a/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/resources/layer.xml b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/resources/layer.xml
index 0557608..3f0acc8 100644
--- a/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/resources/layer.xml
+++ b/apisupport/apisupport.ant/src/org/netbeans/modules/apisupport/project/resources/layer.xml
@@ -275,6 +275,7 @@
<folder name="rules">
<folder name="errors">
<file name="org-netbeans-modules-apisupport-project-java-hints-errors-SearchModuleDependency.instance"/>
+ <file name="org-netbeans-modules-apisupport-project-java-hints-errors-EnablePreviewAntProj.instance"/>
</folder>
</folder>
</folder>
diff --git a/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/Bundle.properties b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/Bundle.properties
index 76c8a53..e3541d5 100644
--- a/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/Bundle.properties
+++ b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/Bundle.properties
@@ -18,3 +18,4 @@
LBL_Missing_Class=Resolve Missing Class Dependency
LBL_Missing_Class_Dec=Resolve missing class dependency and add to project POM
+FIX_EnablePreviewFeature=Enable Preview Feature
diff --git a/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
new file mode 100644
index 0000000..37f29fc
--- /dev/null
+++ b/java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.errors;
+
+import com.sun.source.util.TreePath;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import javax.lang.model.SourceVersion;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.modules.java.hints.spi.ErrorRule;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.Fix;
+import org.openide.filesystems.FileObject;
+import org.openide.util.NbBundle;
+import org.openide.util.Parameters;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.maven.api.customizer.ModelHandle2;
+import org.netbeans.modules.maven.execute.model.NetbeansActionMapping;
+import org.netbeans.modules.maven.model.ModelOperation;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Configuration;
+import org.netbeans.modules.maven.model.pom.POMComponentFactory;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.POMQName;
+import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.filesystems.FileSystem;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Maven type project.
+ *
+ * @author arusinha
+ */
+public class EnablePreviewMavenProj implements ErrorRule<Void> {
+
+ private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
+ "compiler.err.preview.feature.disabled.plural")); // NOI18N
+ private static final String ENABLE_PREVIEW_FLAG = "--enable-preview"; // NOI18N
+
+ @Override
+ public Set<String> getCodes() {
+ return Collections.unmodifiableSet(ERROR_CODES);
+ }
+
+ @Override
+ @NonNull
+ public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+
+ if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
+ return Collections.<Fix>emptyList();
+ }
+
+ Fix fix = null;
+ final FileObject file = compilationInfo.getFileObject();
+ if (file != null) {
+ final Project prj = FileOwnerQuery.getOwner(file);
+ if (isMavenProject(prj)) {
+ fix = new EnablePreviewMavenProj.ResolveMvnFix(prj);
+ } else {
+ fix = null;
+ }
+
+ }
+ return (fix != null) ? Collections.<Fix>singletonList(fix) : Collections.<Fix>emptyList();
+ }
+
+ @Override
+ public String getId() {
+ return EnablePreviewMavenProj.class.getName();
+ }
+
+ @Override
+ public String getDisplayName() {
+ return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+ }
+
+ public String getDescription() {
+ return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+ }
+
+ @Override
+ public void cancel() {
+ }
+
+ private static final class ResolveMvnFix implements Fix {
+
+ private final Project prj;
+
+ ResolveMvnFix(@NonNull final Project prj) {
+ Parameters.notNull("prj", prj); //NOI18N
+ this.prj = prj;
+ }
+
+ @Override
+ public String getText() {
+ return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature");
+ }
+
+ @Override
+ public ChangeInfo implement() throws Exception {
+
+ try {
+
+ final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+ pom.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
+ @Override
+ public void run() throws IOException {
+ List<ModelOperation<POMModel>> operations = new ArrayList<ModelOperation<POMModel>>();
+ operations.add(new AddMvnCompilerPluginForEnablePreview());
+ org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+ }
+ });
+
+ } catch (IOException ex) {
+ }
+ ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+ for (String action : new String[]{"run", "debug", "profile"}) { // NOI18N
+
+ NetbeansActionMapping mapp = ModelHandle2.getMapping(action, prj, cfg);
+ Map<String, String> properties = mapp.getProperties();
+
+ for (Entry<String, String> entry : properties.entrySet()) {
+ if (entry.getKey().equals("exec.args")) { // NOI18Nl
+ if (!entry.getValue().contains(ENABLE_PREVIEW_FLAG + " ")) {
+ properties.put(entry.getKey(), ENABLE_PREVIEW_FLAG + " " + entry.getValue());
+ }
+ }
+ }
+ if (mapp != null) {
+ ModelHandle2.putMapping(mapp, prj, cfg);
+ }
+
+ }
+
+ return null;
+ }
+ }
+
+ private boolean isMavenProject(Project prj) {
+
+ FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml");
+ return (pom != null) && pom.isValid();
+
+ }
+
+ private static class AddMvnCompilerPluginForEnablePreview implements ModelOperation<POMModel> {
+
+ private static final String MAVEN_COMPILER_GROUP_ID = "org.apache.maven.plugins"; // NOI18N
+ private static final String MAVEN_COMPILER_ARTIFACT_ID = "maven-compiler-plugin"; // NOI18N
+ private static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
+ private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
+ private static final String MAVEN_COMPILER_VERSION = "3.3"; // NOI18N
+ private static final String ARG = "arg";// NOI18N
+ private POMComponentFactory factory;
+
+ @Override
+ public void performOperation(final POMModel model) {
+ factory = model.getFactory();
+ org.netbeans.modules.maven.model.pom.Project proj = model.getProject();
+ Build build = model.getProject().getBuild();
+ if (build == null) {
+ build = factory.createBuild();
+ proj.setBuild(build);
+ }
+
+ Plugin oldPlugin = searchMavenCompilerPlugin(build);
+ if (oldPlugin == null) {
+ build.addPlugin(createMavenCompilerPlugin());
+ } else {
+
+ Plugin newPlugin = updateMavenCompilerPlugin(oldPlugin);
+
+ build.removePlugin(oldPlugin);
+ build.addPlugin(newPlugin);
+ }
+ }
+
+ private Plugin searchMavenCompilerPlugin(final Build build) {
+ List<Plugin> plugins = build.getPlugins();
+ if (plugins != null) {
+ for (Plugin plugin : plugins) {
+ if (MAVEN_COMPILER_GROUP_ID.equals(plugin.getGroupId())
+ && MAVEN_COMPILER_ARTIFACT_ID.equals(plugin.getArtifactId())) {
+ return plugin;
+ }
+ }
+ }
+ return null;
+ }
+
+ private Plugin createMavenCompilerPlugin() {
+ Plugin plugin = factory.createPlugin();
+ plugin.setGroupId(MAVEN_COMPILER_GROUP_ID);
+ plugin.setArtifactId(MAVEN_COMPILER_ARTIFACT_ID);
+ plugin.setVersion(MAVEN_COMPILER_VERSION);
+ plugin.setConfiguration(createConfiguration());
+ Configuration config = factory.createConfiguration();
+ POMExtensibilityElement compilerArgs = factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
+ compilerArgs.setChildElementText(COMPILER_ID_PROPERTY, ENABLE_PREVIEW_FLAG, POMQName.createQName(ARG));
+ config.addExtensibilityElement(compilerArgs);
+ plugin.setConfiguration(config);
+ return plugin;
+ }
+
+ private Configuration createConfiguration() {
+ Configuration configuration = factory.createConfiguration();
+ return configuration;
+ }
+
+ private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
+
+ Configuration currenConfig = oldPlugin.getConfiguration();
+ Configuration newConfiguration = createConfiguration();
+
+ boolean isCompilerArgsElementPresent = false;
+ if (currenConfig != null) {
+ for (POMExtensibilityElement element : currenConfig.getConfigurationElements()) {
+ POMExtensibilityElement newElement = factory.createPOMExtensibilityElement(element.getQName());
+ String elementText = element.getElementText();
+ if (elementText.trim().length() > 0) {
+ newElement.setElementText(element.getElementText());
+ }
+ if (newElement.getQName().getLocalPart().equals(COMPILER_ARG)) {
+ isCompilerArgsElementPresent = true;
+ POMExtensibilityElement compilerArgs = factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
+ newElement.setChildElementText(COMPILER_ID_PROPERTY, ENABLE_PREVIEW_FLAG, POMQName.createQName(ARG));
+ }
+ for (POMExtensibilityElement childElement : element.getAnyElements()) {
+
+ POMExtensibilityElement newChildElement = factory.createPOMExtensibilityElement(childElement.getQName());
+
+ newChildElement.setElementText(childElement.getElementText());
+ newElement.addExtensibilityElement(newChildElement);
+ }
+
+ newConfiguration.addExtensibilityElement(newElement);
+
+ }
+ if (!isCompilerArgsElementPresent) {
+ POMExtensibilityElement compilerArgs = factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
+ compilerArgs.setChildElementText(COMPILER_ID_PROPERTY, ENABLE_PREVIEW_FLAG, POMQName.createQName(ARG));
+ newConfiguration.addExtensibilityElement(compilerArgs);
+ }
+ }
+
+ Plugin newPlugin = factory.createPlugin();
+ newPlugin.setGroupId(oldPlugin.getGroupId());
+ newPlugin.setArtifactId(oldPlugin.getArtifactId());
+ newPlugin.setVersion(oldPlugin.getVersion());
+ newPlugin.setConfiguration(newConfiguration);
+ return newPlugin;
+
+ }
+
+ }
+
+}
diff --git a/java/maven.hints/src/org/netbeans/modules/maven/hints/layer.xml b/java/maven.hints/src/org/netbeans/modules/maven/hints/layer.xml
index eb4eeb7..765d9a7 100644
--- a/java/maven.hints/src/org/netbeans/modules/maven/hints/layer.xml
+++ b/java/maven.hints/src/org/netbeans/modules/maven/hints/layer.xml
@@ -25,6 +25,7 @@
<folder name="rules">
<folder name="errors">
<file name="org-netbeans-modules-maven-hints-errors-SearchClassDependencyInRepo.instance"/>
+ <file name="org-netbeans-modules-maven-hints-errors-EnablePreviewMavenProj.instance"/>
</folder>
<folder name="hints">
<folder name="maven">
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists