You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/11/26 03:52:36 UTC

[GitHub] [netbeans] mbien opened a new pull request, #5009: adds a basic maven dependency updater hint.

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

    - marks artifacts which have a newer version available
    - provides a fix to bump the version
    - supports maven properties
    - ignores non-numerical versions
   
   I wasn't sure whether or not to enable this by default but I decided to enable it mostly because:
    - many new-project-poms are outdated, this mitigates the issue a little bit (this is also the original motivation for the hint)
    - poms don't have a lot of hints right now, so adding a little bit of noise probably doesn't hurt atm
    - users can turn it off / we can turn it off based on feedback later
   
   I leave this as draft until I got more testing in. Performance isn't bad so far. It checks large poms in about a second. Although I noticed that the cancel method is never called on maven hints?


-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#issuecomment-1362252143

   only rebased to latest master


-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#issuecomment-1367662423

   @matthiasblaesing sorry for changing the PR after approval. I fixed #5165 too. See first 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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058027005


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   The maven hints option panel seems to be fairly broken atm. Cancel doesn't work for most hint options etc. The implementation is also a bit... unusual. It polls the `getSavedValue` method twice per second and it forgets polling again when cancelled and reopened. The `cancel` method is also called on ok - this isn't mentioned in the doc.
   
   
   It also expects the UI to remember the old ("saved") value for the options? I don't think it is right now possible to make the ok, cancel and apply button work at the same time. I chose 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 pull request #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#issuecomment-1367612301

   awesome, squashed PR to:
    - first commit will be the maven option panel polling bugfix
    - second commit is the feature
   
   this should hopefully reduce risks and allow to test things independently if something happens. (I try to avoid fixing bugs in feature commits)


-- 
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 pull request #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
matthiasblaesing commented on PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#issuecomment-1367915635

   Could reproduce the problem and found it fixed with the update. Thank you.


-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058027005


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   The maven hints option panel seems to be fairly broken atm. Cancel doesn't work for most hint options etc. The implementation is also a bit... unusual. It polls the `getSavedValue` method twice per second and it forgets polling again when cancelled and reopened. The `cancel` method is also called on ok (after reopening) - this isn't mentioned in the doc.
   
   
   It also expects the UI to remember the old ("saved") value for the options? I don't think it is right now possible to make the ok, cancel and apply button work at the same time. I chose 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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058027005


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   The maven hints option panel seems to be fairly broken atm. Cancel doesn't work for most hint options etc. The implementation is also a bit... unusual. It polls the `getSavedValue` method twice per second and it forgets polling again when cancelled and reopened.
   
   It also expects the UI to remember the old ("saved") value for the options? I don't think it is right now possible to make the ok, cancel and apply button work at the same time. I chose 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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058027005


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   The maven hints option panel seems to be fairly broken atm. Cancel doesn't work for most hint options etc. The implementation is also a bit... unusual. It polls the `getSavedValue` method twice per second and it forgets polling again when reopened. The `cancel` method is also called on ok (after reopening) - this isn't mentioned in the doc.
   
   
   It also expects the UI to remember the old ("saved") value of the options? I don't think it is right now possible to make the ok, cancel and apply button work at the same time. I chose ok.



##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   i believe @neilcsmith-net mentioned this once, now I know what you meant :D



-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058082785


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   I think I have fixed the polling issue. This appears to be the root cause of options not cancelling properly and also the apply button not getting activated. Last commit contains the fix.



-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#issuecomment-1366329439

   > Implementation looks sane. Two thoughts:
   > 
   >     1. (Out of scope for this changeset): Maven hints are missing "Show as warning on current line". With more hints the noise goes up and the hepfulness goes down. This is a candiate for to much noise. @work this would flag nearly all dependencies and thus I would deactivate it.
   
   yeah would be nice to have feature parity with the java hints where it makes sense. There is also currently no way to run maven hints as inspections although this is probably only rarely needed. Projects can define project specific hints but this is also only possible for java hints I believe (I don't see anything else in the combo box).
   
   > 
   >     2. Would it make sense to provide an update to latest and "latest in same major version"? I associate bumping major version with incompatible change or at least "no stability guarentee".
   
   Done. I added it as option in the hint settings. I actually thought about adding this too but dismissed it since I initially wanted to keep this really simple (since there are maven plugins for this). But I believe a simple "don't suggest major version upgrades" probably covers 95% of all typical cases, so why not.
   
   The initial motivation for this was because many project pom templates are outdated, this gives the user a quick upgrade path even on first use once the IDE downloaded the maven index. The release version hint (and other IDE features too) require certain compiler plugin versions. This hint can basically unlock other hints by bumping this version.


-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1059174701


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   filed #5165 for the remaining issues



-- 
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 #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien commented on code in PR #5009:
URL: https://github.com/apache/netbeans/pull/5009#discussion_r1058027005


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UpdateDependencyHint.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.hints.pom;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.hints.pom.spi.Configuration;
+import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
+import org.netbeans.modules.maven.indexer.api.NBVersionInfo;
+import org.netbeans.modules.maven.indexer.api.RepositoryQueries;
+import org.netbeans.modules.maven.model.pom.Build;
+import org.netbeans.modules.maven.model.pom.Dependency;
+import org.netbeans.modules.maven.model.pom.DependencyManagement;
+import org.netbeans.modules.maven.model.pom.POMComponent;
+import org.netbeans.modules.maven.model.pom.POMExtensibilityElement;
+import org.netbeans.modules.maven.model.pom.POMModel;
+import org.netbeans.modules.maven.model.pom.PluginManagement;
+import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.model.pom.VersionablePOMComponent;
+import org.netbeans.spi.editor.hints.ChangeInfo;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.editor.hints.Severity;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Marks the artifact if there is a newer version available and provides a fix
+ * which updates the version.
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UpdateDependencyHint=Mark artifact upgrade opportunities.",
+    "DESC_UpdateDependencyHint=Marks the artifact if there is a new version available and provides a version upgrde fix."
+                            + "<p>Upgrades of major versions can be optionally omitted.</p>",
+    "HINT_UpdateDependencyHint=New version available: ",
+    "FIX_UpdateDependencyHint=upgrade to: "})
+public class UpdateDependencyHint implements POMErrorFixProvider {
+
+    private static final Configuration config = new Configuration(UpdateDependencyHint.class.getSimpleName(),
+                TIT_UpdateDependencyHint(), DESC_UpdateDependencyHint(), true, Configuration.HintSeverity.WARNING);
+
+    static final String KEY_NO_MAJOR_UPGRADE = "no_major_upgrade";
+
+    private boolean noMajorUpgrde;
+    private UpdateDependencyHintCustomizer customizer;
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        noMajorUpgrde = getNoMajorUpgradeOption();
+
+        Map<POMComponent, ErrorDescription> hints = new HashMap<>();
+
+        List<Dependency> deps = model.getProject().getDependencies();
+        if (deps != null) {
+            addHintsTo(deps, hints);
+        }
+
+        DependencyManagement depman = model.getProject().getDependencyManagement();
+        if (depman != null && depman.getDependencies() != null) {
+            addHintsTo(depman.getDependencies(), hints);
+        }
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            if (build.getPlugins() != null) {
+                addHintsTo(build.getPlugins(), hints);
+            }
+
+            PluginManagement plugman = build.getPluginManagement();
+            if (plugman != null && plugman.getPlugins() != null) {
+                addHintsTo(plugman.getPlugins(), hints);
+            }
+        }
+
+        return new ArrayList<>(hints.values());
+    }
+
+    private void addHintsTo(List<? extends VersionablePOMComponent> components, Map<POMComponent, ErrorDescription> hints) {
+
+        for (VersionablePOMComponent comp : components) {
+
+            String groupId = comp.getGroupId();
+            String artifactId = comp.getArtifactId();
+
+            if (groupId != null && artifactId != null && !groupId.isEmpty() && !artifactId.isEmpty()) {
+
+                boolean property = false;
+                String version = comp.getVersion();
+                if (PomModelUtils.isPropertyExpression(version)) {
+                    version = PomModelUtils.getProperty(comp.getModel(), version);
+                    property = true;
+                }
+
+                if (version != null) {
+
+                    // don't upgrade clean numerical versions to timestamps or non-numerical versions (other way around is allowed)
+                    boolean allow_qualifier = !isNumerical(version);
+                    boolean allow_timestamp = !noTimestamp(version);
+                    String requiredPrefix = noMajorUpgrde ? getMajorComponentPrefix(version) : "";
+
+                    Optional<ComparableVersion> latest = RepositoryQueries.getVersionsResult(groupId, artifactId, null)
+                            .getResults().stream()
+                            .map(NBVersionInfo::getVersion)
+                            .filter((v) -> !v.isEmpty() && v.startsWith(requiredPrefix))
+                            .filter((v) -> allow_qualifier || !Character.isDigit(v.charAt(0)) || isNumerical(v))
+                            .filter((v) -> allow_timestamp || !Character.isDigit(v.charAt(0)) || noTimestamp(v))
+                            .map(ComparableVersion::new)
+                            .max(ComparableVersion::compareTo);
+
+                    if (latest.isPresent() && latest.get().compareTo(new ComparableVersion(version)) > 0) {
+                        POMComponent version_comp = null;
+                        if (property) {
+                            Properties props = comp.getModel().getProject().getProperties();
+                            if (props != null) {
+                                version_comp = PomModelUtils.getFirstChild(props, PomModelUtils.getPropertyName(comp.getVersion()));
+                            }
+                        } else {
+                            version_comp = PomModelUtils.getFirstChild(comp, "version");
+                        }
+                        if (version_comp instanceof POMExtensibilityElement) {
+                            ErrorDescription previous = hints.get(version_comp);
+                            if (previous == null || compare(((UpdateVersionFix) previous.getFixes().getFixes().get(0)).version, version) > 0) {
+                                hints.put(version_comp, createHintForComponent((POMExtensibilityElement) version_comp, latest.get().toString()));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private static boolean isNumerical(String v) {
+        for (char c : v.toCharArray()) {
+            if (!(Character.isDigit(c) || c == '.')) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private boolean noTimestamp(String v) {
+        char[] chars = v.toCharArray();
+        for (int i = 0; i < chars.length; i++) {
+            if (!(Character.isDigit(chars[i]))) {
+                return i == 0 || Integer.parseInt(v.substring(0, i)) < 10_000;
+            }
+        }
+        return Integer.parseInt(v) < 10_000;
+    }
+
+    // example: in '3.14' -> out '3.'
+    private String getMajorComponentPrefix(String v) {
+        int dot = v.indexOf('.');
+        if (dot > 0) {
+            String major = v.substring(0, dot+1);
+            if (isNumerical(major)) {
+                return major;
+            }
+        }
+        return "";
+    }
+
+    private static int compare(String version1, String version2) {
+        return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
+    }
+
+    private ErrorDescription createHintForComponent(POMExtensibilityElement comp, String version) {
+        Document doc = comp.getModel().getBaseDocument();
+        int line = NbEditorUtilities.getLine(doc, comp.findPosition(), false).getLineNumber() + 1;
+        List<Fix> fix =  Collections.singletonList(new UpdateVersionFix(comp, version));
+        return ErrorDescriptionFactory.createErrorDescription(Severity.HINT, HINT_UpdateDependencyHint() + version, fix, doc, line);
+    }
+
+    private static class UpdateVersionFix implements Fix {
+
+        private final POMExtensibilityElement version_comp;
+        private final String version;
+
+        private UpdateVersionFix(POMExtensibilityElement component, String toVersion) {
+            this.version_comp = component;
+            this.version = toVersion;
+        }
+
+        @Override
+        public String getText() {
+            return FIX_UpdateDependencyHint() + version;
+        }
+
+        @Override
+        public ChangeInfo implement() throws Exception {
+            PomModelUtils.implementInTransaction(version_comp.getModel(), () -> {
+                version_comp.setElementText(version);
+            });
+            return new ChangeInfo();
+        }
+
+    }
+
+    @Override
+    public void cancel() {
+        customizer = null;
+    }
+
+    @Override
+    public Configuration getConfiguration() {
+        return config;
+    }
+
+    private static boolean getNoMajorUpgradeOption() {
+        return config.getPreferences().getBoolean(KEY_NO_MAJOR_UPGRADE, false);
+    }
+
+    @Override
+    public String getSavedValue(JComponent customizer, String key) {
+        if (KEY_NO_MAJOR_UPGRADE.equals(key) && customizer instanceof UpdateDependencyHintCustomizer) {
+            return Boolean.toString(((UpdateDependencyHintCustomizer)customizer).getSavedNoMajorUpgradeOption());
+        }
+        return null;
+    }

Review Comment:
   The maven hints option panel seems to be fairly broken atm. Cancel doesn't work for most hint options etc. The implementation is also a bit... unusual. It polls the `getSavedValue` method twice per second and it forgets polling again when reopened. The `cancel` method is also called on ok (after reopening) - this isn't mentioned in the doc.
   
   
   It also expects the UI to remember the old ("saved") value for the options? I don't think it is right now possible to make the ok, cancel and apply button work at the same time. I chose 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 merged pull request #5009: adds a basic maven dependency updater hint.

Posted by GitBox <gi...@apache.org>.
mbien merged PR #5009:
URL: https://github.com/apache/netbeans/pull/5009


-- 
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