You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by sk...@apache.org on 2020/03/14 18:27:39 UTC

[netbeans] branch master updated: Adding a new hint for hardcoded version numbers after -source when --enable-preview.

This is an automated email from the ASF dual-hosted git repository.

skygo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 3f12316  Adding a new hint for hardcoded version numbers after -source when --enable-preview.
     new 8e98e1e  Merge pull request #1993 from jlahoda/hardcoded-source-version-hint
3f12316 is described below

commit 3f12316207dc6b98d56e9372b0e1a6ef45f3de0d
Author: Jan Lahoda <jl...@netbeans.org>
AuthorDate: Mon Feb 3 06:21:43 2020 +0100

    Adding a new hint for hardcoded version numbers after -source when --enable-preview.
---
 .../java/openjdk/jtreg/WrongSourceVersion.java     | 103 +++++++++++++++++++++
 .../java/openjdk/jtreg/WrongSourceVersionTest.java |  58 ++++++++++++
 2 files changed, 161 insertions(+)

diff --git a/java/java.openjdk.project/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersion.java b/java/java.openjdk.project/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersion.java
new file mode 100644
index 0000000..e8a5a8a
--- /dev/null
+++ b/java/java.openjdk.project/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersion.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+ package org.netbeans.modules.java.openjdk.jtreg;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.sun.source.tree.Tree.Kind;
+import com.sun.source.util.TreePath;
+import java.io.IOException;
+import java.util.Arrays;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.modules.java.openjdk.jtreg.TagParser.Result;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.java.hints.Hint;
+import org.netbeans.spi.java.hints.Hint.Options;
+import org.netbeans.spi.java.hints.HintContext;
+import org.netbeans.spi.java.hints.JavaFix;
+import org.netbeans.spi.java.hints.TriggerTreeKind;
+import org.openide.util.NbBundle.Messages;
+
+@Hint(displayName = "#DN_WrongSourceVersion", description = "#DESC_WrongSourceVersion", category = "general", options=Options.NO_BATCH)
+@Messages({
+    "DN_WrongSourceVersion=Wrong -source",
+    "DESC_WrongSourceVersion=Checks for hardcoded values for -source when --enable-preview"
+})
+public class WrongSourceVersion {
+
+    @TriggerTreeKind(Kind.COMPILATION_UNIT)
+    @Messages({
+        "ERR_HardcodedSource=Hardcoded source version, should use ${jdk.version}"
+    })
+    public static List<ErrorDescription> computeWarning(HintContext ctx) {
+        Result tags = TagParser.parseTags(ctx.getInfo());
+        List<ErrorDescription> result = new ArrayList<>();
+        for (Tag tag : tags.getTags()) {
+            if (!"compile".equals(tag.getName())) {
+                continue;
+            }
+            String[] params = tag.getValue().split("[\\s]+");
+            boolean hasEnablePreview = Arrays.stream(params).anyMatch(s -> "--enable-preview".equals(s));
+            if (hasEnablePreview) {
+                for (int i = 0; i < params.length; i++) {
+                    if ((params[i].equals("-source") || params[i].equals("--source")) && i + 1 < params.length) {
+                        try {
+                            Integer.parseInt(params[i + 1]);
+                            int pos = tag.getValue().indexOf(params[i]);
+                            int start = tag.getValue().indexOf(params[i + 1], pos) + tag.getTagEnd();
+                            int end = start + params[i + 1].length();
+                            ErrorDescription idealED = ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_HardcodedSource(), new UseJdkSource(ctx.getInfo(), ctx.getPath(), start, end).toEditorFix());
+
+                            result.add(org.netbeans.spi.editor.hints.ErrorDescriptionFactory.createErrorDescription(idealED.getSeverity(), idealED.getDescription(), idealED.getFixes(), ctx.getInfo().getFileObject(), start, end));
+                        } catch (NumberFormatException ex) {
+                            //OK
+                        }
+                    }
+                }
+            }
+        }
+        return result;
+    }
+
+    private static final class UseJdkSource extends JavaFix {
+
+        private final int start;
+        private final int end;
+
+        public UseJdkSource(CompilationInfo info, TreePath tp, int start, int end) {
+            super(info, tp);
+            this.start = start;
+            this.end = end;
+        }
+
+        @Override
+        @Messages("FIX_UseJdkVersion=Use ${jdk.version}")
+        protected String getText() {
+            return Bundle.FIX_AddRefOutput();
+        }
+
+        @Override
+        protected void performRewrite(TransformationContext ctx) throws IOException {
+            ctx.getWorkingCopy().rewriteInComment(start, end - start, "${jdk.version}");
+        }
+    }
+
+}
diff --git a/java/java.openjdk.project/test/unit/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersionTest.java b/java/java.openjdk.project/test/unit/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersionTest.java
new file mode 100644
index 0000000..bebd52d
--- /dev/null
+++ b/java/java.openjdk.project/test/unit/src/org/netbeans/modules/java/openjdk/jtreg/WrongSourceVersionTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.openjdk.jtreg;
+
+import java.util.List;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.netbeans.modules.java.hints.test.api.HintTest;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.java.hints.HintContext;
+
+/**
+ *
+ * @author jlahoda
+ */
+public class WrongSourceVersionTest {
+    
+    public WrongSourceVersionTest() {
+    }
+    
+    @Test
+    public void testNoRefOutput() throws Exception {
+        HintTest.create()
+                .input("/*@test\n" +
+                       " *@compile --enable-preview -source 14 Test.java\n" +
+                       " */\n" +
+                       "class Test {\n" +
+                       "}\n")
+                .run(WrongSourceVersion.class)
+                .findWarning("1:36-1:38:verifier:" + Bundle.ERR_HardcodedSource())
+                .applyFix()
+                .assertVerbatimOutput("/*@test\n" +
+                                      " *@compile --enable-preview -source ${jdk.version} Test.java\n" +
+                                      " */\n" +
+                                      "class Test {\n" +
+                                      "}\n");
+    }
+}


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

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