You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "jlahoda (via GitHub)" <gi...@apache.org> on 2023/04/09 10:21:42 UTC

[GitHub] [netbeans] jlahoda opened a new pull request, #5802: Improving enable-preview Java fix:

jlahoda opened a new pull request, #5802:
URL: https://github.com/apache/netbeans/pull/5802

    - use an SPI, and a single fix provider, to:
    - support the fix in the langauge server
    - allow the fix to also change the source level, as needed
    - properly force refresh of diagnostics inside the language server after indexing finishes
   
   
   
   
   
   ---
   **^Add meaningful description above**
   
   By opening a pull request you confirm that, unless explicitly stated otherwise, the changes -
   
    - are all your own work, and you have the right to contribute them.
    - are contributed solely under the terms and conditions of the Apache License 2.0 (see section 5 of the license for more information).
   
   Please make sure (eg. `git log`) that all commits have a valid name and email address for you in the Author field.
   
   If you're a first time contributor, see the Contributing guidelines for more information.
   
   If you're a committer, please label the PR before pressing "Create pull request" so that the right test jobs can run.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1182155204


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -49,148 +43,146 @@
 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.modules.maven.model.pom.PluginContainer;
+import org.netbeans.modules.maven.model.pom.Properties;
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
-import org.openide.filesystems.FileSystem;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",           // NOI18N
-            "compiler.err.preview.feature.disabled.plural",    // NOI18N
-            "compiler.err.is.preview")));                      // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
 
-    @Override
-    public Set<String> getCodes() {
-        return ERROR_CODES;
+    private final Project prj;
+
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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;
+    public void enablePreview(String newSourceLevel) throws Exception {
+        final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+        pom.getFileSystem().runAtomicAction(() -> {
+            List<ModelOperation<POMModel>> operations = new ArrayList<>();
+            operations.add(new AddMvnCompilerPluginForEnablePreview(newSourceLevel));
+            org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
         }
-        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() {
+    public boolean canChangeSourceLevel() {
+        CheckCanChangeSourceLevel canChange = new CheckCanChangeSourceLevel();
+        try {
+            final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+            pom.getFileSystem().runAtomicAction(() -> {
+                List<ModelOperation<POMModel>> operations = Collections.singletonList(canChange);
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+            });
+        } catch (IOException ex) {
+            LOG.log(Level.FINE, null, ex);
+        }
+        return canChange.canChangeSourceLevel;
     }
+    private static final Logger LOG = Logger.getLogger(EnablePreviewMavenProj.class.getName());
 
-    private static final class ResolveMvnFix implements Fix {
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
 
-        private final Project prj;
-
-        ResolveMvnFix(@NonNull final Project prj) {
-            Parameters.notNull("prj", prj); //NOI18N
-            this.prj = prj;
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-
-        @Override
-        public String getText() {
-            return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature");
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
         }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
+        }
+    }
 
-        @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);
+    private static class BaseMvnCompilerPluginForEnablePreview {
+
+        protected static final String MAVEN_COMPILER_GROUP_ID = "org.apache.maven.plugins"; // NOI18N
+        protected static final String MAVEN_COMPILER_ARTIFACT_ID = "maven-compiler-plugin"; // NOI18N
+        protected static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
+        protected static final String RELEASE = "release"; // NOI18N
+        protected static final String RELEASE_PROPERTY = "maven.compiler.release"; // NOI18N
+        protected static final String SOURCE = "source"; // NOI18N
+        protected static final String SOURCE_PROPERTY = "maven.compiler.source"; // NOI18N
+        protected static final String TARGET = "target"; // NOI18N
+        protected static final String TARGET_PROPERTY = "maven.compiler.target"; // NOI18N
+        protected static final String COMPILER_ARG = "compilerArgs"; // NOI18N
+        protected static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
+        protected static final String ARG = "arg";// NOI18N
+
+        protected 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;
                     }
-                });
-
-            } 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();
+            return null;
+        }

Review Comment:
   awesome, thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lbownik commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "lbownik (via GitHub)" <gi...@apache.org>.
lbownik commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167592051


##########
java/java.hints/src/org/netbeans/modules/java/hints/errors/EnablePreview.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.java.hints.errors;
+
+import com.sun.source.util.TreePath;
+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.platform.JavaPlatform;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.java.source.ClasspathInfo.PathKind;
+import org.netbeans.api.java.source.CompilationInfo;
+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.modules.java.hints.spi.preview.PreviewEnabler;
+import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler.Factory;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Single Source Java File.
+ *
+ * @author Arunava Sinha
+ */
+public class EnablePreview implements ErrorRule<Void> {
+
+    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
+            "compiler.err.preview.feature.disabled",           //NOI18N
+            "compiler.err.preview.feature.disabled.plural")); // NOI18N
+
+    @Override
+    public Set<String> getCodes() {
+        return Collections.unmodifiableSet(ERROR_CODES);

Review Comment:
   how about making ERROR_CODES and unmodifiable set and avoid allocation every time getCodes is called?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] matthiasblaesing commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "matthiasblaesing (via GitHub)" <gi...@apache.org>.
matthiasblaesing commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169166326


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   Sorry, I tried to grasp what the whole PR does and failed to get to it quickly. To be frank: If you confident, that it is the right approach, change it, merge, if you are unsure, wait for the comment from @jlahoda and maybe get it into NB19.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168066444


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   consider using this patch:
   ```diff
   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
   index bfdc4b1..ca3b2af 100644
   --- 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
   @@ -38,6 +38,7 @@
    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.modules.maven.model.pom.Properties;
    import org.netbeans.spi.project.ProjectConfiguration;
    import org.netbeans.spi.project.ProjectConfigurationProvider;
    import org.openide.filesystems.FileSystem;
   @@ -122,6 +123,7 @@
            private static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
            private static final String SOURCE = "source"; // NOI18N
            private static final String TARGET = "target"; // NOI18N
   +        private static final String RELEASE = "release"; // NOI18N
            private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
            private static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
            private static final String ARG = "arg";// NOI18N
   @@ -144,10 +146,10 @@
    
                Plugin oldPlugin = searchMavenCompilerPlugin(build);
                if (oldPlugin == null) {
   -                build.addPlugin(createMavenCompilerPlugin());
   +                build.addPlugin(createMavenCompilerPlugin(model));
                } else {
    
   -                Plugin newPlugin = updateMavenCompilerPlugin(oldPlugin);
   +                Plugin newPlugin = updateMavenCompilerPlugin(model, oldPlugin);
    
                    build.removePlugin(oldPlugin);
                    build.addPlugin(newPlugin);
   @@ -167,12 +169,12 @@
                return null;
            }
    
   -        private Plugin createMavenCompilerPlugin() {
   +        private Plugin createMavenCompilerPlugin(POMModel model) {
                Plugin plugin = factory.createPlugin();
                plugin.setGroupId(MAVEN_COMPILER_GROUP_ID);
                plugin.setArtifactId(MAVEN_COMPILER_ARTIFACT_ID);
                plugin.setVersion(MAVEN_COMPILER_VERSION);
   -            plugin.setConfiguration(updateMavenCompilerPluginConfiguration(createConfiguration()));
   +            plugin.setConfiguration(updateMavenCompilerPluginConfiguration(model, createConfiguration()));
                return plugin;
            }
    
   @@ -181,36 +183,65 @@
                return configuration;
            }
    
   -        private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
   +        private Plugin updateMavenCompilerPlugin(POMModel model, final Plugin oldPlugin) {
                Configuration currenConfig = oldPlugin.getConfiguration();
                Plugin newPlugin = factory.createPlugin();
                newPlugin.setGroupId(oldPlugin.getGroupId());
                newPlugin.setArtifactId(oldPlugin.getArtifactId());
                newPlugin.setVersion(oldPlugin.getVersion());
   -            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
   +            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(model, currenConfig));
                return newPlugin;
            }
    
   -        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
   +        private Configuration updateMavenCompilerPluginConfiguration(POMModel model, final Configuration currentConfig) {
                Configuration newConfiguration = createConfiguration();
    
   -            if (currenConfig != null) {
   +            if (currentConfig != null) {
   +
   +                Properties props = model.getProject().getProperties();
   +
   +                boolean useProps = false;
   +                if (props != null && ( props.getProperty("maven.compiler.release") != null // NOI18N
   +                                    || props.getProperty("maven.compiler.source") != null // NOI18N
   +                                    || props.getProperty("maven.compiler.target") != null)) { // NOI18N
   +                    if (newSourceLevel != null) {
   +                        if (props.getProperty("maven.compiler.release") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.release", newSourceLevel); // NOI18N
   +                        }
   +                        if (props.getProperty("maven.compiler.source") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.source", newSourceLevel); // NOI18N
   +                        }
   +                        if (props.getProperty("maven.compiler.target") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.target", newSourceLevel); // NOI18N
   +                        }
   +                    }
   +                    useProps = true;
   +                }
   +
                    boolean isCompilerArgsElementPresent = false;
                    boolean isSourceElementPresent = false;
                    boolean isTargetElementPresent = false;
   -                for (POMExtensibilityElement element : currenConfig.getConfigurationElements()) {
   +                boolean isReleaseElementPresent = false;
   +
   +                for (POMExtensibilityElement element : currentConfig.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(SOURCE) && newSourceLevel != null) {
   -                        newElement.setElementText(newSourceLevel);
   -                        isSourceElementPresent = true;
   -                    }
   -                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
   -                        newElement.setElementText(newSourceLevel);
   -                        isTargetElementPresent = true;
   +                    if (newSourceLevel != null) {
   +                        if (newElement.getQName().getLocalPart().equals(SOURCE)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isSourceElementPresent = true;
   +                        }
   +                        if (newElement.getQName().getLocalPart().equals(TARGET)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isTargetElementPresent = true;
   +                        }
   +                        if (newElement.getQName().getLocalPart().equals(RELEASE)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isReleaseElementPresent = true;
   +                        }
                        }
                        if (newElement.getQName().getLocalPart().equals(COMPILER_ARG)) {
                            isCompilerArgsElementPresent = true;
   @@ -228,15 +259,17 @@
                        newConfiguration.addExtensibilityElement(newElement);
    
                    }
   -                if (!isSourceElementPresent && newSourceLevel != null) {
   -                    POMExtensibilityElement source = factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
   -                    source.setElementText(newSourceLevel);
   -                    newConfiguration.addExtensibilityElement(source);
   -                }
   -                if (!isTargetElementPresent && newSourceLevel != null) {
   -                    POMExtensibilityElement target = factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
   -                    target.setElementText(newSourceLevel);
   -                    newConfiguration.addExtensibilityElement(target);
   +                if (!useProps && newSourceLevel != null && !isReleaseElementPresent) {
   +                    if (!isSourceElementPresent) {
   +                        POMExtensibilityElement source = factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
   +                        source.setElementText(newSourceLevel);
   +                        newConfiguration.addExtensibilityElement(source);
   +                    }
   +                    if (!isTargetElementPresent) {
   +                        POMExtensibilityElement target = factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
   +                        target.setElementText(newSourceLevel);
   +                        newConfiguration.addExtensibilityElement(target);
   +                    }
                    }
                    if (!isCompilerArgsElementPresent) {
                        POMExtensibilityElement compilerArgs = factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
   
   ```
   
   this mitigates some issues.
    - adds support for properties
    - will work for projects > JDK 8 which are encouraged to use `release`
   
   This hint will need some more work later since it doesn't support maven features like `<pluginManagement>` etc, but at least it won't break basic poms now (hopefully).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169197804


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   i agree lets not rush it. The main reason of me trying to get this in was because of the fixes which this includes. Will try to extract them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on pull request #5802: Improving enable-preview Java fix

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#issuecomment-1526341810

   > > Thanks for the changes. Not sure if it is allowed to squash commits from different authors, though.
   > 
   > @jlahoda this is no problem. Commits can have co-authors: https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors#creating-co-authored-commits-on-the-command-line
   > 
   > when I squash multiple authors i simply add: `Co-authored-by: NAME <NA...@EXAMPLE.COM>` to the last lines of the commit.
   
   Thanks, done.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1181426771


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -49,148 +43,146 @@
 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.modules.maven.model.pom.PluginContainer;
+import org.netbeans.modules.maven.model.pom.Properties;
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
-import org.openide.filesystems.FileSystem;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",           // NOI18N
-            "compiler.err.preview.feature.disabled.plural",    // NOI18N
-            "compiler.err.is.preview")));                      // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
 
-    @Override
-    public Set<String> getCodes() {
-        return ERROR_CODES;
+    private final Project prj;
+
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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;
+    public void enablePreview(String newSourceLevel) throws Exception {
+        final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+        pom.getFileSystem().runAtomicAction(() -> {
+            List<ModelOperation<POMModel>> operations = new ArrayList<>();
+            operations.add(new AddMvnCompilerPluginForEnablePreview(newSourceLevel));
+            org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
         }
-        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() {
+    public boolean canChangeSourceLevel() {
+        CheckCanChangeSourceLevel canChange = new CheckCanChangeSourceLevel();
+        try {
+            final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+            pom.getFileSystem().runAtomicAction(() -> {
+                List<ModelOperation<POMModel>> operations = Collections.singletonList(canChange);
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+            });
+        } catch (IOException ex) {
+            LOG.log(Level.FINE, null, ex);
+        }
+        return canChange.canChangeSourceLevel;
     }
+    private static final Logger LOG = Logger.getLogger(EnablePreviewMavenProj.class.getName());
 
-    private static final class ResolveMvnFix implements Fix {
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
 
-        private final Project prj;
-
-        ResolveMvnFix(@NonNull final Project prj) {
-            Parameters.notNull("prj", prj); //NOI18N
-            this.prj = prj;
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-
-        @Override
-        public String getText() {
-            return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature");
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
         }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
+        }
+    }
 
-        @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);
+    private static class BaseMvnCompilerPluginForEnablePreview {
+
+        protected static final String MAVEN_COMPILER_GROUP_ID = "org.apache.maven.plugins"; // NOI18N
+        protected static final String MAVEN_COMPILER_ARTIFACT_ID = "maven-compiler-plugin"; // NOI18N
+        protected static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
+        protected static final String RELEASE = "release"; // NOI18N
+        protected static final String RELEASE_PROPERTY = "maven.compiler.release"; // NOI18N
+        protected static final String SOURCE = "source"; // NOI18N
+        protected static final String SOURCE_PROPERTY = "maven.compiler.source"; // NOI18N
+        protected static final String TARGET = "target"; // NOI18N
+        protected static final String TARGET_PROPERTY = "maven.compiler.target"; // NOI18N
+        protected static final String COMPILER_ARG = "compilerArgs"; // NOI18N
+        protected static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
+        protected static final String ARG = "arg";// NOI18N
+
+        protected 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;
                     }
-                });
-
-            } 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();
+            return null;
+        }

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1173092279


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   fwiw, making some progress on improving this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] matthiasblaesing commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "matthiasblaesing (via GitHub)" <gi...@apache.org>.
matthiasblaesing commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169194633


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   Yes, I got that. But this PR goes through multiple modules in an area I did not yet touch. The suggested changes seem sane, but that is much as I'm willing to say tonight, what I don't get is why the rush now? Is there a reason to force it in now or could @jlahoda just finish it and it gets into NB19?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1174746339


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -246,47 +226,176 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig, oldPlugin.getVersion()));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(Configuration currenConfig, String version) {
             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 (currenConfig == null) {
+                currenConfig = createConfiguration();
+            }
+
+            boolean supportsRelease = false;
+
+            try {
+                supportsRelease = version == null ||
+                                  new SpecificationVersion(version).compareTo(new SpecificationVersion("3.6")) >= 0;

Review Comment:
   added a commit for this: https://github.com/mbien/netbeans/commits/better-enable-preview



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168007754


##########
java/java.hints/src/org/netbeans/modules/java/hints/errors/EnablePreview.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.java.hints.errors;
+
+import com.sun.source.util.TreePath;
+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.platform.JavaPlatform;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.java.source.ClasspathInfo.PathKind;
+import org.netbeans.api.java.source.CompilationInfo;
+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.modules.java.hints.spi.preview.PreviewEnabler;
+import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler.Factory;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Single Source Java File.
+ *
+ * @author Arunava Sinha
+ */
+public class EnablePreview implements ErrorRule<Void> {
+
+    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+            "compiler.err.preview.feature.disabled",           //NOI18N
+            "compiler.err.preview.feature.disabled.plural"))); // NOI18N

Review Comment:
   tested on JDK 20 and this doesn't trigger the hint atm:
   ```java
       Thread.Builder.OfVirtual ofVirtual = Thread.ofVirtual();
   ```
   the compiler error:
   ```
   COMPILATION ERROR : 
   -------------------------------------------------------------
   com/mycompany/mavenproject1/Mavenproject1.java:[15,15] java.lang.Thread.Builder is a preview API and is disabled by default.
     (use --enable-preview to enable preview APIs)
   com/mycompany/mavenproject1/Mavenproject1.java:[15,23] java.lang.Thread.Builder.OfVirtual is a preview API and is disabled by default.
     (use --enable-preview to enable preview APIs)
   com/mycompany/mavenproject1/Mavenproject1.java:[15,52] ofVirtual() is a preview API and is disabled by default.
     (use --enable-preview to enable preview APIs)
   3 errors 
   ```
   maybe there is a new error code now? Or does this only work for lang features?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168043195


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   this looks like it would break if the compiler plugin is using `release` already, since it isn't compatible with `source`+`target`.
   
   Should be an easy fix though, just another `if`
   
   actually, I am pretty sure there is a utility somewhere which does this already. Let me take a look.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168043195


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   this looks like it would break if the compiler plugin is using `release` already, since it isn't compatible with `source`+`target`.
   
   Should be an easy fix though, just another `if`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1174652553


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -246,47 +226,176 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig, oldPlugin.getVersion()));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(Configuration currenConfig, String version) {
             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 (currenConfig == null) {
+                currenConfig = createConfiguration();
+            }
+
+            boolean supportsRelease = false;
+
+            try {
+                supportsRelease = version == null ||
+                                  new SpecificationVersion(version).compareTo(new SpecificationVersion("3.6")) >= 0;

Review Comment:
   consider using `ComparableVersion`. 
   - this won't throw `NumberFormatException`s
   - and it will also support comparing versions like `3.10-SNAPSHOT`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on pull request #5802: Improving enable-preview Java fix

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#issuecomment-1524694522

   > changes look good.
   > 
   > I added commits here: https://github.com/mbien/netbeans/commits/better-enable-preview
   > 
   > this adds a fix for compiler plugins without groupID (groupIds are optional for standard maven plugins) and rudimentary support for `pluginManagement`. Tests included.
   > 
   > feel free to squash with your changes if you like it (I can do this too if you are ok with me me force pushing into your PR).
   
   Thanks for the changes. Not sure if it is allowed to squash commits from different authors, though. I believe it is not. I'll work on including your patches. I need to take a look at the action in VS Code, though, as it didn't work for me yesterday.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167981809


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -52,144 +41,97 @@
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
 import org.openide.filesystems.FileSystem;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",
-            "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
-    }
+    private final Project prj;
 
-    public String getDescription() {
-        return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        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(newSourceLevel));
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
             }
-            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);
-                }
-
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
-            return null;
         }
     }
 
-    private boolean isMavenProject(Project prj) {
-        if (prj == null) {
-            return false;
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
+
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-        FileObject prjDir = prj.getProjectDirectory();
-        if (prjDir == null) {
-            return false;
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
+        }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
         }
-
-        FileObject pom = prjDir.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 SOURCE = "source"; // NOI18N
+        private static final String TARGET = "target"; // NOI18N
         private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
         private static final String MAVEN_COMPILER_VERSION = "3.3"; // NOI18N

Review Comment:
   can you bump this to `3.11.0`?
   
   `3.11.0` still supports running on JDK 8 but it knows about the `release` options, modules and other features too. This will  allow NB to show `release` option hint right away in the pom.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#issuecomment-1540480879

   ok lets get this in. merging.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#issuecomment-1525995678

   > Thanks for the changes. Not sure if it is allowed to squash commits from different authors, though. 
   
   @jlahoda this is no problem. Commits can have co-authors:
   https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors#creating-co-authored-commits-on-the-command-line
   
   when I squash multiple authors i simply add:
   `Co-authored-by: NAME <NA...@EXAMPLE.COM>`
   to the last lines of the commit.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167981809


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -52,144 +41,97 @@
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
 import org.openide.filesystems.FileSystem;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",
-            "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
-    }
+    private final Project prj;
 
-    public String getDescription() {
-        return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        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(newSourceLevel));
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
             }
-            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);
-                }
-
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
-            return null;
         }
     }
 
-    private boolean isMavenProject(Project prj) {
-        if (prj == null) {
-            return false;
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
+
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-        FileObject prjDir = prj.getProjectDirectory();
-        if (prjDir == null) {
-            return false;
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
+        }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
         }
-
-        FileObject pom = prjDir.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 SOURCE = "source"; // NOI18N
+        private static final String TARGET = "target"; // NOI18N
         private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
         private static final String MAVEN_COMPILER_VERSION = "3.3"; // NOI18N

Review Comment:
   can you bump this to `3.11.0`?
   
   `3.11.0` still supports running on JDK 8 but it knows about the `release` options too. So the user will see the `release` option hint right away in the pom.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168043195


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   this looks like it would break if the compiler plugin is using `release` already, since it isn't compatible with `source`+`target`.
   
   Should be an easy fix though, just another `if`
   
   actually, ~I am pretty sure there is a utility somewhere which does this already.~ Let me take a look.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1174652553


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -246,47 +226,176 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig, oldPlugin.getVersion()));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(Configuration currenConfig, String version) {
             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 (currenConfig == null) {
+                currenConfig = createConfiguration();
+            }
+
+            boolean supportsRelease = false;
+
+            try {
+                supportsRelease = version == null ||
+                                  new SpecificationVersion(version).compareTo(new SpecificationVersion("3.6")) >= 0;

Review Comment:
   consider using `ComparableVersion`. 
   - this won't throw NumberFormatExceptions
   - and it will also support text like `3.10-SNAPSHOT`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lbownik commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "lbownik (via GitHub)" <gi...@apache.org>.
lbownik commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167590419


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -195,7 +166,7 @@ public EditableProperties run() throws IOException {
         }
     }
 
-    private boolean isAntProject(Project prj) {
+    private static boolean isAntProject(Project prj) {
         if (prj == null) {

Review Comment:
   this is a privete method? can't you guarantee thot no callsite passes null? then you could get rid of this if statment



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167617620


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -18,94 +18,66 @@
  */
 package org.netbeans.modules.ant.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.modules.java.hints.spi.preview.PreviewEnabler;
 import org.netbeans.spi.project.support.ant.AntProjectHelper;
 import org.openide.util.EditableProperties;
 import org.openide.util.Mutex;
 import org.openide.util.MutexException;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
  * the fix for Ant type project.
  *
  * @author arusinha
  */
-public class EnablePreviewAntProj implements ErrorRule<Void> {
+public class EnablePreviewAntProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",          // NOI18N
-            "compiler.err.preview.feature.disabled.plural")); // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
+    private static final String JAVAC_SOURCE = "javac.source"; // NOI18N
+    private static final String JAVAC_TARGET = "javac.target"; // 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);
+    private final Project prj;
+
+    public EnablePreviewAntProj(Project prj) {
+        this.prj = prj;
     }
 
     @Override
-    @NonNull
-    public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        new ResolveAntFix(prj).enablePreview(newSourceLevel);
+    }
 
-        if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
-            return Collections.<Fix>emptyList();
-        }
+    @ServiceProvider(service=Factory.class, position=1000)
+    public static final class FactoryImpl implements Factory {
 
-        final FileObject file = compilationInfo.getFileObject();
-        Fix fix = null;
-        if (file != null) {
+        @Override
+        public PreviewEnabler enablerFor(FileObject file) {
             final Project prj = FileOwnerQuery.getOwner(file);
 
             if (isAntProject(prj)) {
-                fix = new EnablePreviewAntProj.ResolveAntFix(prj);
+                return new EnablePreviewAntProj(prj);
             } else {
-                fix = null;
+                return null;

Review Comment:
   Well, there must a value saying "this provider is not interested". I could invent a special hardcoded value for that, but isn't there already a value that can carry the meaning? I.e. `null`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167617973


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -195,7 +166,7 @@ public EditableProperties run() throws IOException {
         }
     }
 
-    private boolean isAntProject(Project prj) {
+    private static boolean isAntProject(Project prj) {
         if (prj == null) {

Review Comment:
   Well, `FileOwnerQuery.getOwner` can return `null`. So, there must be a null check somewhere. I don't really see an advantage in doing that before calling the method? Is there some? (Frankly, even if the method was public, I'd probably make it accept nulls, to make its use as convenient as possible, so that callers would not have to additionally check for `null`. I am not sure what's the issue with a method checking for `null`.)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169191803


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   i am going to try to extract the fixes to another PR and get it in first, this is less risky than mixing it with the API refactoring.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1174652553


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -246,47 +226,176 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig, oldPlugin.getVersion()));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(Configuration currenConfig, String version) {
             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 (currenConfig == null) {
+                currenConfig = createConfiguration();
+            }
+
+            boolean supportsRelease = false;
+
+            try {
+                supportsRelease = version == null ||
+                                  new SpecificationVersion(version).compareTo(new SpecificationVersion("3.6")) >= 0;

Review Comment:
   consider using `ComparableVersion`. 
   - this won't throw `NumberFormatException`s which makes the try block redundant
   - and it will also support comparing versions like `3.10-SNAPSHOT`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168015528


##########
java/java.hints/src/org/netbeans/modules/java/hints/errors/EnablePreview.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.java.hints.errors;
+
+import com.sun.source.util.TreePath;
+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.platform.JavaPlatform;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.java.source.ClasspathInfo.PathKind;
+import org.netbeans.api.java.source.CompilationInfo;
+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.modules.java.hints.spi.preview.PreviewEnabler;
+import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler.Factory;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Single Source Java File.
+ *
+ * @author Arunava Sinha
+ */
+public class EnablePreview implements ErrorRule<Void> {
+
+    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+            "compiler.err.preview.feature.disabled",           //NOI18N
+            "compiler.err.preview.feature.disabled.plural"))); // NOI18N

Review Comment:
   Uh, good catch! Added a new key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lbownik commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "lbownik (via GitHub)" <gi...@apache.org>.
lbownik commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168260760


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -195,7 +166,7 @@ public EditableProperties run() throws IOException {
         }
     }
 
-    private boolean isAntProject(Project prj) {
+    private static boolean isAntProject(Project prj) {
         if (prj == null) {

Review Comment:
   wen methods become NOPs when given improper arguments it gets harder to reason about program flow
   just my 0.02$



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169268190


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   extracted the error code update to #5837



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169172115


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   what it does essentially:
   if you type something like `Thread.Builder.OfVirtual ofVirtual = Thread.ofVirtual();` on JDK 20. The compiler will tell the user to enable the preview feature since this API is not final. (this works for language features too, not only API)
   
   This is intercepted by the hint and when the user activates the "fix" it sets the preview flag where needed (e.g maven compiler plugin). This does now also bump the source/target/release versions as needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1180937058


##########
java/java.hints/src/org/netbeans/modules/java/hints/errors/EnablePreview.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.java.hints.errors;
+
+import com.sun.source.util.TreePath;
+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.platform.JavaPlatform;
+import org.netbeans.api.java.platform.JavaPlatformManager;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.java.source.ClasspathInfo.PathKind;
+import org.netbeans.api.java.source.CompilationInfo;
+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.modules.java.hints.spi.preview.PreviewEnabler;
+import org.netbeans.modules.java.hints.spi.preview.PreviewEnabler.Factory;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.Lookup;
+
+/**
+ * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
+ * the fix for Single Source Java File.
+ *
+ * @author Arunava Sinha
+ */
+public class EnablePreview implements ErrorRule<Void> {
+
+    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
+            "compiler.err.preview.feature.disabled",           //NOI18N
+            "compiler.err.preview.feature.disabled.plural",    // NOI18N
+            "compiler.err.is.preview")));                      // NOI18N
+
+    @Override
+    public Set<String> getCodes() {
+        return ERROR_CODES;
+    }
+
+    @Override
+    @NonNull
+    public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+        final FileObject file = compilationInfo.getFileObject();
+
+        if (file != null) {
+            SpecificationVersion platformVersion = null;
+
+            FileObject jlObject = compilationInfo.getClasspathInfo().getClassPath(PathKind.BOOT).findResource("java/lang/Object.class");
+            for (JavaPlatform platform : JavaPlatformManager.getDefault().getInstalledPlatforms()) {
+                if (jlObject == platform.getBootstrapLibraries().findResource("java/lang/Object.class")) {
+                    platformVersion = platform.getSpecification().getVersion();
+                    break;
+                }
+            }

Review Comment:
   i am going to steal this for a different hint, since I needed to know the JDK version before and couldn't find any utility or anything in the lookup which would help there.



##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -49,148 +43,146 @@
 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.modules.maven.model.pom.PluginContainer;
+import org.netbeans.modules.maven.model.pom.Properties;
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
-import org.openide.filesystems.FileSystem;
+import org.openide.modules.SpecificationVersion;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",           // NOI18N
-            "compiler.err.preview.feature.disabled.plural",    // NOI18N
-            "compiler.err.is.preview")));                      // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
 
-    @Override
-    public Set<String> getCodes() {
-        return ERROR_CODES;
+    private final Project prj;
+
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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;
+    public void enablePreview(String newSourceLevel) throws Exception {
+        final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+        pom.getFileSystem().runAtomicAction(() -> {
+            List<ModelOperation<POMModel>> operations = new ArrayList<>();
+            operations.add(new AddMvnCompilerPluginForEnablePreview(newSourceLevel));
+            org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
         }
-        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() {
+    public boolean canChangeSourceLevel() {
+        CheckCanChangeSourceLevel canChange = new CheckCanChangeSourceLevel();
+        try {
+            final FileObject pom = prj.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+            pom.getFileSystem().runAtomicAction(() -> {
+                List<ModelOperation<POMModel>> operations = Collections.singletonList(canChange);
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
+            });
+        } catch (IOException ex) {
+            LOG.log(Level.FINE, null, ex);
+        }
+        return canChange.canChangeSourceLevel;
     }
+    private static final Logger LOG = Logger.getLogger(EnablePreviewMavenProj.class.getName());
 
-    private static final class ResolveMvnFix implements Fix {
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
 
-        private final Project prj;
-
-        ResolveMvnFix(@NonNull final Project prj) {
-            Parameters.notNull("prj", prj); //NOI18N
-            this.prj = prj;
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-
-        @Override
-        public String getText() {
-            return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature");
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
         }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
+        }
+    }
 
-        @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);
+    private static class BaseMvnCompilerPluginForEnablePreview {
+
+        protected static final String MAVEN_COMPILER_GROUP_ID = "org.apache.maven.plugins"; // NOI18N
+        protected static final String MAVEN_COMPILER_ARTIFACT_ID = "maven-compiler-plugin"; // NOI18N
+        protected static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
+        protected static final String RELEASE = "release"; // NOI18N
+        protected static final String RELEASE_PROPERTY = "maven.compiler.release"; // NOI18N
+        protected static final String SOURCE = "source"; // NOI18N
+        protected static final String SOURCE_PROPERTY = "maven.compiler.source"; // NOI18N
+        protected static final String TARGET = "target"; // NOI18N
+        protected static final String TARGET_PROPERTY = "maven.compiler.target"; // NOI18N
+        protected static final String COMPILER_ARG = "compilerArgs"; // NOI18N
+        protected static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
+        protected static final String ARG = "arg";// NOI18N
+
+        protected 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;
                     }
-                });
-
-            } 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();
+            return null;
+        }

Review Comment:
   my mistake, I didn't see that this method was implemented in the super class.
   
   I think it would be better to move the method from `AddMvnCompilerPluginForEnablePreview` to `BaseMvnCompilerPluginForEnablePreview`, so that it is not implemented twice, just slightly differently



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien merged pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien merged PR #5802:
URL: https://github.com/apache/netbeans/pull/5802


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168852067


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   @matthiasblaesing what is your opinion? should I apply the changes to merge this PR last minute?
   
   It is actually fixing several bugs in the hint.
    - it listens to one error type more
    - compiler plugin version was bumped from the ancient 3.3 to something which knows `release`/modules/etc
    - it doesn't break when the `release` option, or properties are used



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] jlahoda commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "jlahoda (via GitHub)" <gi...@apache.org>.
jlahoda commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1169266423


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   Given the needed tweaks for the pom.xml provider, I guess we can wait with this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lbownik commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "lbownik (via GitHub)" <gi...@apache.org>.
lbownik commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167590223


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -18,94 +18,66 @@
  */
 package org.netbeans.modules.ant.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.modules.java.hints.spi.preview.PreviewEnabler;
 import org.netbeans.spi.project.support.ant.AntProjectHelper;
 import org.openide.util.EditableProperties;
 import org.openide.util.Mutex;
 import org.openide.util.MutexException;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
  * the fix for Ant type project.
  *
  * @author arusinha
  */
-public class EnablePreviewAntProj implements ErrorRule<Void> {
+public class EnablePreviewAntProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",          // NOI18N
-            "compiler.err.preview.feature.disabled.plural")); // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
+    private static final String JAVAC_SOURCE = "javac.source"; // NOI18N
+    private static final String JAVAC_TARGET = "javac.target"; // 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);
+    private final Project prj;
+
+    public EnablePreviewAntProj(Project prj) {
+        this.prj = prj;
     }
 
     @Override
-    @NonNull
-    public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        new ResolveAntFix(prj).enablePreview(newSourceLevel);
+    }
 
-        if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
-            return Collections.<Fix>emptyList();
-        }
+    @ServiceProvider(service=Factory.class, position=1000)
+    public static final class FactoryImpl implements Factory {
 
-        final FileObject file = compilationInfo.getFileObject();
-        Fix fix = null;
-        if (file != null) {
+        @Override
+        public PreviewEnabler enablerFor(FileObject file) {
             final Project prj = FileOwnerQuery.getOwner(file);
 
             if (isAntProject(prj)) {
-                fix = new EnablePreviewAntProj.ResolveAntFix(prj);
+                return new EnablePreviewAntProj(prj);
             } else {
-                fix = null;
+                return null;

Review Comment:
   does it make sense to return some NullObject here ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] lbownik commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "lbownik (via GitHub)" <gi...@apache.org>.
lbownik commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167617877


##########
java/ant.hints/src/org/netbeans/modules/ant/hints/errors/EnablePreviewAntProj.java:
##########
@@ -18,94 +18,66 @@
  */
 package org.netbeans.modules.ant.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.modules.java.hints.spi.preview.PreviewEnabler;
 import org.netbeans.spi.project.support.ant.AntProjectHelper;
 import org.openide.util.EditableProperties;
 import org.openide.util.Mutex;
 import org.openide.util.MutexException;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * Handle error rule "compiler.err.preview.feature.disabled.plural" and provide
  * the fix for Ant type project.
  *
  * @author arusinha
  */
-public class EnablePreviewAntProj implements ErrorRule<Void> {
+public class EnablePreviewAntProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",          // NOI18N
-            "compiler.err.preview.feature.disabled.plural")); // NOI18N
     private static final String ENABLE_PREVIEW_FLAG = "--enable-preview";   // NOI18N
+    private static final String JAVAC_SOURCE = "javac.source"; // NOI18N
+    private static final String JAVAC_TARGET = "javac.target"; // 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);
+    private final Project prj;
+
+    public EnablePreviewAntProj(Project prj) {
+        this.prj = prj;
     }
 
     @Override
-    @NonNull
-    public List<Fix> run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        new ResolveAntFix(prj).enablePreview(newSourceLevel);
+    }
 
-        if (SourceVersion.latest() != compilationInfo.getSourceVersion()) {
-            return Collections.<Fix>emptyList();
-        }
+    @ServiceProvider(service=Factory.class, position=1000)
+    public static final class FactoryImpl implements Factory {
 
-        final FileObject file = compilationInfo.getFileObject();
-        Fix fix = null;
-        if (file != null) {
+        @Override
+        public PreviewEnabler enablerFor(FileObject file) {
             final Project prj = FileOwnerQuery.getOwner(file);
 
             if (isAntProject(prj)) {
-                fix = new EnablePreviewAntProj.ResolveAntFix(prj);
+                return new EnablePreviewAntProj(prj);
             } else {
-                fix = null;
+                return null;

Review Comment:
   ok



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix:

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1167981809


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -52,144 +41,97 @@
 import org.netbeans.spi.project.ProjectConfiguration;
 import org.netbeans.spi.project.ProjectConfigurationProvider;
 import org.openide.filesystems.FileSystem;
+import org.openide.util.lookup.ServiceProvider;
 
 /**
  * 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> {
+public class EnablePreviewMavenProj implements PreviewEnabler {
 
-    private static final Set<String> ERROR_CODES = new HashSet<String>(Arrays.asList(
-            "compiler.err.preview.feature.disabled",
-            "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
-    }
+    private final Project prj;
 
-    public String getDescription() {
-        return NbBundle.getMessage(EnablePreviewMavenProj.class, "FIX_EnablePreviewFeature"); // NOI18N
+    private EnablePreviewMavenProj(@NonNull final Project prj) {
+        Parameters.notNull("prj", prj); //NOI18N
+        this.prj = prj;
     }
 
     @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) {
+    public void enablePreview(String newSourceLevel) throws Exception {
+        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(newSourceLevel));
+                org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);
             }
-            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);
-                }
-
+        });
+
+        ProjectConfiguration cfg = prj.getLookup().lookup(ProjectConfigurationProvider.class).getActiveConfiguration();
+
+        ActionConfig[] actions = new ActionConfig[] {
+            ActionConfig.runAction("run"), // NOI18N
+            ActionConfig.runAction("debug"), // NOI18N
+            ActionConfig.runAction("profile"), // NOI18N
+            ActionConfig.runAction("run.single.main"), // NOI18N
+            ActionConfig.runAction("debug.single.main"), // NOI18N
+            ActionConfig.runAction("profile.single.main"), // NOI18N
+            ActionConfig.testAction("test"), // NOI18N
+            ActionConfig.testAction("test.single"), // NOI18N
+            ActionConfig.testAction("debug.test.single"), // NOI18N
+            ActionConfig.testAction("profile.test.single"), // NOI18N
+        };
+        for (ActionConfig action : actions) {
+            NetbeansActionMapping mapp = ModelHandle2.getMapping(action.actionName, prj, cfg);
+            Map<String, String> properties = mapp.getProperties();
+            String existingValue = properties.getOrDefault(action.propertyName, "");
+
+            if (!existingValue.contains(ENABLE_PREVIEW_FLAG)) {
+                properties.put(action.propertyName, ENABLE_PREVIEW_FLAG + (existingValue .isEmpty() ? "" : " ") + existingValue);
+                ModelHandle2.putMapping(mapp, prj, cfg);
             }
 
-            return null;
         }
     }
 
-    private boolean isMavenProject(Project prj) {
-        if (prj == null) {
-            return false;
+    private static final class ActionConfig {
+        public final String actionName;
+        public final String propertyName;
+
+        public ActionConfig(String actionName, String propertyName) {
+            this.actionName = actionName;
+            this.propertyName = propertyName;
         }
-        FileObject prjDir = prj.getProjectDirectory();
-        if (prjDir == null) {
-            return false;
+        public static ActionConfig runAction(String actionName) {
+            return new ActionConfig(actionName, "exec.args");
+        }
+        public static ActionConfig testAction(String actionName) {
+            return new ActionConfig(actionName, "argLine");
         }
-
-        FileObject pom = prjDir.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 SOURCE = "source"; // NOI18N
+        private static final String TARGET = "target"; // NOI18N
         private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
         private static final String MAVEN_COMPILER_VERSION = "3.3"; // NOI18N

Review Comment:
   can you bump this to `3.11.0`?
   
   `3.11.0` still supports running on JDK 8 but it knows about the release options too. So the user will see the `release` option hint right away in the pom.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


[GitHub] [netbeans] mbien commented on a diff in pull request #5802: Improving enable-preview Java fix

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #5802:
URL: https://github.com/apache/netbeans/pull/5802#discussion_r1168066444


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/errors/EnablePreviewMavenProj.java:
##########
@@ -245,18 +182,36 @@ private Configuration createConfiguration() {
         }
 
         private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
-
             Configuration currenConfig = oldPlugin.getConfiguration();
+            Plugin newPlugin = factory.createPlugin();
+            newPlugin.setGroupId(oldPlugin.getGroupId());
+            newPlugin.setArtifactId(oldPlugin.getArtifactId());
+            newPlugin.setVersion(oldPlugin.getVersion());
+            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
+            return newPlugin;
+        }
+
+        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
             Configuration newConfiguration = createConfiguration();
 
-            boolean isCompilerArgsElementPresent = false;
             if (currenConfig != null) {
+                boolean isCompilerArgsElementPresent = false;
+                boolean isSourceElementPresent = false;
+                boolean isTargetElementPresent = false;
                 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(SOURCE) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isSourceElementPresent = true;
+                    }
+                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
+                        newElement.setElementText(newSourceLevel);
+                        isTargetElementPresent = true;
+                    }

Review Comment:
   consider using this patch:
   ```diff
   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
   index bfdc4b1..ca3b2af 100644
   --- 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
   @@ -38,6 +38,7 @@
    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.modules.maven.model.pom.Properties;
    import org.netbeans.spi.project.ProjectConfiguration;
    import org.netbeans.spi.project.ProjectConfigurationProvider;
    import org.openide.filesystems.FileSystem;
   @@ -122,6 +123,7 @@
            private static final String COMPILER_ID_PROPERTY = "compilerId"; // NOI18N
            private static final String SOURCE = "source"; // NOI18N
            private static final String TARGET = "target"; // NOI18N
   +        private static final String RELEASE = "release"; // NOI18N
            private static final String COMPILER_ARG = "compilerArgs"; // NOI18N
            private static final String MAVEN_COMPILER_VERSION = "3.11.0"; // NOI18N
            private static final String ARG = "arg";// NOI18N
   @@ -144,10 +146,10 @@
    
                Plugin oldPlugin = searchMavenCompilerPlugin(build);
                if (oldPlugin == null) {
   -                build.addPlugin(createMavenCompilerPlugin());
   +                build.addPlugin(createMavenCompilerPlugin(model));
                } else {
    
   -                Plugin newPlugin = updateMavenCompilerPlugin(oldPlugin);
   +                Plugin newPlugin = updateMavenCompilerPlugin(model, oldPlugin);
    
                    build.removePlugin(oldPlugin);
                    build.addPlugin(newPlugin);
   @@ -167,12 +169,12 @@
                return null;
            }
    
   -        private Plugin createMavenCompilerPlugin() {
   +        private Plugin createMavenCompilerPlugin(POMModel model) {
                Plugin plugin = factory.createPlugin();
                plugin.setGroupId(MAVEN_COMPILER_GROUP_ID);
                plugin.setArtifactId(MAVEN_COMPILER_ARTIFACT_ID);
                plugin.setVersion(MAVEN_COMPILER_VERSION);
   -            plugin.setConfiguration(updateMavenCompilerPluginConfiguration(createConfiguration()));
   +            plugin.setConfiguration(updateMavenCompilerPluginConfiguration(model, createConfiguration()));
                return plugin;
            }
    
   @@ -181,36 +183,65 @@
                return configuration;
            }
    
   -        private Plugin updateMavenCompilerPlugin(final Plugin oldPlugin) {
   +        private Plugin updateMavenCompilerPlugin(POMModel model, final Plugin oldPlugin) {
                Configuration currenConfig = oldPlugin.getConfiguration();
                Plugin newPlugin = factory.createPlugin();
                newPlugin.setGroupId(oldPlugin.getGroupId());
                newPlugin.setArtifactId(oldPlugin.getArtifactId());
                newPlugin.setVersion(oldPlugin.getVersion());
   -            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(currenConfig));
   +            newPlugin.setConfiguration(updateMavenCompilerPluginConfiguration(model, currenConfig));
                return newPlugin;
            }
    
   -        private Configuration updateMavenCompilerPluginConfiguration(final Configuration currenConfig) {
   +        private Configuration updateMavenCompilerPluginConfiguration(POMModel model, final Configuration currentConfig) {
                Configuration newConfiguration = createConfiguration();
    
   -            if (currenConfig != null) {
   +            if (currentConfig != null) {
   +
   +                Properties props = model.getProject().getProperties();
   +
   +                boolean useProps = false;
   +                if (props != null && ( props.getProperty("maven.compiler.release") != null // NOI18N
   +                                    || props.getProperty("maven.compiler.source") != null // NOI18N
   +                                    || props.getProperty("maven.compiler.target") != null)) { // NOI18N
   +                    if (newSourceLevel != null) {
   +                        if (props.getProperty("maven.compiler.release") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.release", newSourceLevel); // NOI18N
   +                        }
   +                        if (props.getProperty("maven.compiler.source") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.source", newSourceLevel); // NOI18N
   +                        }
   +                        if (props.getProperty("maven.compiler.target") != null) { // NOI18N
   +                            props.setProperty("maven.compiler.target", newSourceLevel); // NOI18N
   +                        }
   +                    }
   +                    useProps = true;
   +                }
   +
                    boolean isCompilerArgsElementPresent = false;
                    boolean isSourceElementPresent = false;
                    boolean isTargetElementPresent = false;
   -                for (POMExtensibilityElement element : currenConfig.getConfigurationElements()) {
   +                boolean isReleaseElementPresent = false;
   +
   +                for (POMExtensibilityElement element : currentConfig.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(SOURCE) && newSourceLevel != null) {
   -                        newElement.setElementText(newSourceLevel);
   -                        isSourceElementPresent = true;
   -                    }
   -                    if (newElement.getQName().getLocalPart().equals(TARGET) && newSourceLevel != null) {
   -                        newElement.setElementText(newSourceLevel);
   -                        isTargetElementPresent = true;
   +                    if (newSourceLevel != null) {
   +                        if (newElement.getQName().getLocalPart().equals(SOURCE)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isSourceElementPresent = true;
   +                        }
   +                        if (newElement.getQName().getLocalPart().equals(TARGET)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isTargetElementPresent = true;
   +                        }
   +                        if (newElement.getQName().getLocalPart().equals(RELEASE)) {
   +                            newElement.setElementText(newSourceLevel);
   +                            isReleaseElementPresent = true;
   +                        }
                        }
                        if (newElement.getQName().getLocalPart().equals(COMPILER_ARG)) {
                            isCompilerArgsElementPresent = true;
   @@ -228,15 +259,17 @@
                        newConfiguration.addExtensibilityElement(newElement);
    
                    }
   -                if (!isSourceElementPresent && newSourceLevel != null) {
   -                    POMExtensibilityElement source = factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
   -                    source.setElementText(newSourceLevel);
   -                    newConfiguration.addExtensibilityElement(source);
   -                }
   -                if (!isTargetElementPresent && newSourceLevel != null) {
   -                    POMExtensibilityElement target = factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
   -                    target.setElementText(newSourceLevel);
   -                    newConfiguration.addExtensibilityElement(target);
   +                if (!useProps && newSourceLevel != null && !isReleaseElementPresent) {
   +                    if (!isSourceElementPresent) {
   +                        POMExtensibilityElement source = factory.createPOMExtensibilityElement(POMQName.createQName(SOURCE));
   +                        source.setElementText(newSourceLevel);
   +                        newConfiguration.addExtensibilityElement(source);
   +                    }
   +                    if (!isTargetElementPresent) {
   +                        POMExtensibilityElement target = factory.createPOMExtensibilityElement(POMQName.createQName(TARGET));
   +                        target.setElementText(newSourceLevel);
   +                        newConfiguration.addExtensibilityElement(target);
   +                    }
                    }
                    if (!isCompilerArgsElementPresent) {
                        POMExtensibilityElement compilerArgs = factory.createPOMExtensibilityElement(POMQName.createQName(COMPILER_ARG));
   
   ```
   
   this mitigates some issues.
    - adds support for properties
    - will work for projects > JDK 8 which are encouraged to use `release`
   
   This hint will need some more work later since it doesn't support maven features like `<pluginManagement>` etc, but at least it won't break poms now (hopefully).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists