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/10/17 19:37:32 UTC

[GitHub] [netbeans] matthiasblaesing commented on a diff in pull request #4802: Implement maven pom javac release option hint (JEP 247).

matthiasblaesing commented on code in PR #4802:
URL: https://github.com/apache/netbeans/pull/4802#discussion_r997435067


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.List;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.xml.namespace.QName;
+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.model.pom.Build;
+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.POMQName;
+import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.PluginExecution;
+import org.netbeans.modules.maven.model.pom.Properties;
+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.text.Line;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Converts source/target javac options to the release option where possible (JEP 247).
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UseReleaseVersionHint=Convert matching source/target javac options to the release option.",
+    "DESC_UseReleaseVersionHint=Matching source/target options can be replaced with a single release option (JEP 247). This enforces API compatibility with the chosen Java release.",
+    "FIX_UseReleaseVersionHint=Convert to release option for strict compatibility checks."})
+public class UseReleaseOptionHint implements POMErrorFixProvider {
+
+    private static final String TARGET_TAG = "target";
+    private static final String SOURCE_TAG = "source";
+    private static final String RELEASE_TAG = "release";
+
+    private static final Configuration config = new Configuration(UseReleaseOptionHint.class.getName(),
+                TIT_UseReleaseVersionHint(), DESC_UseReleaseVersionHint(), true, Configuration.HintSeverity.WARNING);
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        List<ErrorDescription> hints = new ArrayList<>();
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            for (Plugin plugin : build.getPlugins()) {
+                if (plugin.getArtifactId().equals("maven-compiler-plugin")) {
+                    if (!isPluginCompatible(plugin)) {

Review Comment:
   Should there also be a guard against using this when JDK platform is lower than 9?



##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.List;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.xml.namespace.QName;
+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.model.pom.Build;
+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.POMQName;
+import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.PluginExecution;
+import org.netbeans.modules.maven.model.pom.Properties;
+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.text.Line;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Converts source/target javac options to the release option where possible (JEP 247).
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UseReleaseVersionHint=Convert matching source/target javac options to the release option.",
+    "DESC_UseReleaseVersionHint=Matching source/target options can be replaced with a single release option (JEP 247). This enforces API compatibility with the chosen Java release.",
+    "FIX_UseReleaseVersionHint=Convert to release option for strict compatibility checks."})
+public class UseReleaseOptionHint implements POMErrorFixProvider {
+
+    private static final String TARGET_TAG = "target";
+    private static final String SOURCE_TAG = "source";
+    private static final String RELEASE_TAG = "release";
+
+    private static final Configuration config = new Configuration(UseReleaseOptionHint.class.getName(),
+                TIT_UseReleaseVersionHint(), DESC_UseReleaseVersionHint(), true, Configuration.HintSeverity.WARNING);
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        List<ErrorDescription> hints = new ArrayList<>();
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            for (Plugin plugin : build.getPlugins()) {
+                if (plugin.getArtifactId().equals("maven-compiler-plugin")) {
+                    if (!isPluginCompatible(plugin)) {
+                        return Collections.emptyList();
+                    }
+                    if (plugin.getConfiguration() != null) {
+                        hints.addAll(createHintsForParent("", plugin.getConfiguration()));
+                    }
+                    if (plugin.getExecutions() != null) {
+                        for (PluginExecution execution : plugin.getExecutions()) {
+                            if (execution.getConfiguration() != null) {
+                                hints.addAll(createHintsForParent("", execution.getConfiguration()));
+                            }
+                        }
+                    }
+                    break;
+                }
+            }
+        }
+
+        Properties properties = model.getProject().getProperties();
+        if (properties != null) {
+            hints.addAll(createHintsForParent("maven.compiler.", properties));
+        }
+
+        return hints;
+    }
+
+    private List<ErrorDescription> createHintsForParent(String prefix, POMComponent parent) {
+
+        int source;
+        int target;
+
+        // property name or an int value
+        String release = null;
+
+        try {
+            String sourceText = parent.getChildElementText(POMQName.createQName(prefix+SOURCE_TAG, true));
+            if (isPropertyName(sourceText)) {
+                release = sourceText;
+                sourceText = getProperty(sourceText, parent.getModel());
+            }
+
+            String targetText = parent.getChildElementText(POMQName.createQName(prefix+TARGET_TAG, true));
+            if (isPropertyName(targetText)) {
+                release = targetText;
+                targetText = getProperty(targetText, parent.getModel());

Review Comment:
   Is this a good idea? If I read this correctly, this evaluates a property. My assumption would be, that there is a reason the user does not use the default properties from maven compiler plugin. If so you can't assume that the property will always hold the same value.



##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.List;
+import java.util.prefs.Preferences;
+import javax.swing.JComponent;
+import javax.xml.namespace.QName;
+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.model.pom.Build;
+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.POMQName;
+import org.netbeans.modules.maven.model.pom.Plugin;
+import org.netbeans.modules.maven.model.pom.PluginExecution;
+import org.netbeans.modules.maven.model.pom.Properties;
+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.text.Line;
+import org.openide.util.NbBundle;
+
+import static org.netbeans.modules.maven.hints.pom.Bundle.*;
+
+/**
+ * Converts source/target javac options to the release option where possible (JEP 247).
+ * @author mbien
+ */
+@NbBundle.Messages({
+    "TIT_UseReleaseVersionHint=Convert matching source/target javac options to the release option.",
+    "DESC_UseReleaseVersionHint=Matching source/target options can be replaced with a single release option (JEP 247). This enforces API compatibility with the chosen Java release.",
+    "FIX_UseReleaseVersionHint=Convert to release option for strict compatibility checks."})
+public class UseReleaseOptionHint implements POMErrorFixProvider {
+
+    private static final String TARGET_TAG = "target";
+    private static final String SOURCE_TAG = "source";
+    private static final String RELEASE_TAG = "release";
+
+    private static final Configuration config = new Configuration(UseReleaseOptionHint.class.getName(),
+                TIT_UseReleaseVersionHint(), DESC_UseReleaseVersionHint(), true, Configuration.HintSeverity.WARNING);
+
+    @Override
+    public List<ErrorDescription> getErrorsForDocument(POMModel model, Project prj) {
+
+        List<ErrorDescription> hints = new ArrayList<>();
+
+        Build build = model.getProject().getBuild();
+        if (build != null) {
+            for (Plugin plugin : build.getPlugins()) {
+                if (plugin.getArtifactId().equals("maven-compiler-plugin")) {
+                    if (!isPluginCompatible(plugin)) {
+                        return Collections.emptyList();
+                    }
+                    if (plugin.getConfiguration() != null) {
+                        hints.addAll(createHintsForParent("", plugin.getConfiguration()));
+                    }
+                    if (plugin.getExecutions() != null) {
+                        for (PluginExecution execution : plugin.getExecutions()) {
+                            if (execution.getConfiguration() != null) {
+                                hints.addAll(createHintsForParent("", execution.getConfiguration()));
+                            }
+                        }
+                    }
+                    break;
+                }
+            }
+        }
+
+        Properties properties = model.getProject().getProperties();
+        if (properties != null) {
+            hints.addAll(createHintsForParent("maven.compiler.", properties));
+        }
+
+        return hints;
+    }
+
+    private List<ErrorDescription> createHintsForParent(String prefix, POMComponent parent) {
+
+        int source;
+        int target;
+
+        // property name or an int value
+        String release = null;
+
+        try {
+            String sourceText = parent.getChildElementText(POMQName.createQName(prefix+SOURCE_TAG, true));
+            if (isPropertyName(sourceText)) {
+                release = sourceText;
+                sourceText = getProperty(sourceText, parent.getModel());
+            }
+
+            String targetText = parent.getChildElementText(POMQName.createQName(prefix+TARGET_TAG, true));
+            if (isPropertyName(targetText)) {
+                release = targetText;
+                targetText = getProperty(targetText, parent.getModel());
+            }
+
+            source = Integer.parseInt(sourceText);
+            target = Integer.parseInt(targetText);
+            if (release == null) {
+                release = String.valueOf(target);
+            }
+        } catch (NumberFormatException ignored) {
+            return Collections.emptyList();
+        }
+
+        if (source == target && source >= 9) {

Review Comment:
   I think you could go down to 6 - at least with OpenJDK 11 javac reports:
   
   ```
     --release <release>
           Compile for a specific VM version. Supported targets: 6, 7, 8, 9, 10, 11
   ```



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