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/09/28 20:44:42 UTC

[GitHub] [netbeans] mbien commented on a diff in pull request #4592: Provide hints for conversion to Virtual thread executor when thread pools are used

mbien commented on code in PR #4592:
URL: https://github.com/apache/netbeans/pull/4592#discussion_r982841116


##########
java/java.hints/src/org/netbeans/modules/java/hints/jdk/CanUseVT.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.hints.jdk;
+
+import org.netbeans.api.java.queries.CompilerOptionsQuery;
+import org.netbeans.modules.java.hints.errors.Utilities;
+import org.netbeans.spi.editor.hints.ErrorDescription;
+import org.netbeans.spi.editor.hints.Fix;
+import org.netbeans.spi.java.hints.ConstraintVariableType;
+import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
+import org.netbeans.spi.java.hints.Hint;
+import org.netbeans.spi.java.hints.HintContext;
+import org.netbeans.spi.java.hints.TriggerPattern;
+import org.netbeans.spi.java.hints.TriggerPatterns;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author mjayan
+ */
+@NbBundle.Messages({
+    "DN_CanUseVT=Can use Virtual Threads",
+    "DESC_CanUseVT=Can use virtual threads if workload is not CPU bound or number of concurrent tasks is high",
+    "ERR_CanUseVT=Can Use Virtual Thread Executor",
+    "ERR_CanUseThreadPerTask=Can Use Executors.newThreadPerTaskExecutor"
+})
+@Hint(displayName = "#DN_CanUseVT", description = "#DESC_CanUseVT", category = "rules15",
+        minSourceVersion = "19")
+public class CanUseVT {
+
+    private static final int VT_PREVIEW_JDK_VERSION = 19;
+
+    @TriggerPatterns({
+        @TriggerPattern(value = "java.util.concurrent.Executors.newFixedThreadPool($size)", constraints = @ConstraintVariableType(variable = "$size", type = "int")),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newFixedThreadPool($size, $factory)", constraints = {
+            @ConstraintVariableType(variable = "$factory", type = "java.util.concurrent.ThreadFactory"),
+            @ConstraintVariableType(variable = "$size", type = "int")}),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newCachedThreadPool()"),
+        @TriggerPattern(value = "java.util.concurrent.Executors.newCachedThreadPool($factory)", constraints
+                = @ConstraintVariableType(variable = "$factory", type = "java.util.concurrent.ThreadFactory"))})
+    public static ErrorDescription compute(HintContext ctx) {
+        if (Utilities.isJDKVersionLower(VT_PREVIEW_JDK_VERSION) && !CompilerOptionsQuery.getOptions(ctx.getInfo().getFileObject()).getArguments().contains("--enable-preview")) {
+            return null;
+        }
+        if (ctx.getVariables().get("$factory") != null) {
+            return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_CanUseThreadPerTask(), new Fix[0]);
+        } else {
+            return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_CanUseVT(), new Fix[0]);
+        }

Review Comment:
   this could return a rewrite fix, otherwise it will just mark the line and do nothing.
   something like:
   
   ```java
   Fix fix = JavaFixUtilities.rewriteFix(ctx, "converts to VT msg", ctx.getPath(), "java.util.concurrent.Executors.newThreadPerTaskExecutor($factory)");
   ```
   and...
   ```java
   Fix fix = JavaFixUtilities.rewriteFix(ctx, "converts to VT msg", ctx.getPath(), "java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor()");
   ```



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