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 2018/03/22 12:42:12 UTC

[GitHub] emilianbold commented on a change in pull request #463: netbeans-479: Added ConvertToVarHint to replace explicit type with var

emilianbold commented on a change in pull request #463: netbeans-479: Added ConvertToVarHint to replace explicit type with var
URL: https://github.com/apache/incubator-netbeans/pull/463#discussion_r176404726
 
 

 ##########
 File path: java.hints/src/org/netbeans/modules/java/hints/suggestions/ConvertToVarHint.java
 ##########
 @@ -0,0 +1,449 @@
+/*
+ * 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.suggestions;
+
+import com.sun.source.tree.BlockTree;
+import com.sun.source.tree.ExpressionTree;
+import com.sun.source.tree.NewClassTree;
+import com.sun.source.tree.ParameterizedTypeTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.tree.Tree.Kind;
+import com.sun.source.tree.VariableTree;
+import com.sun.source.util.TreePath;
+import java.util.List;
+import javax.lang.model.type.TypeMirror;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.TreeMaker;
+import org.netbeans.api.java.source.WorkingCopy;
+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.HintContext;
+import org.netbeans.spi.java.hints.JavaFix;
+import org.netbeans.spi.java.hints.JavaFix.TransformationContext;
+import org.netbeans.spi.java.hints.TriggerPattern;
+import org.openide.util.NbBundle.Messages;
+import org.netbeans.spi.editor.hints.Fix;
+import org.openide.filesystems.FileObject;
+import org.openide.modules.SpecificationVersion;
+import org.netbeans.spi.java.hints.TriggerTreeKind;
+import com.sun.tools.javac.tree.JCTree;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Hint will convert explicit type to 'var'. Supported: JDK 10 or above
+ *
+ * @author arusinha
+ */
+public class ConvertToVarHint {
+
+    private static final SpecificationVersion JDK_10 = new SpecificationVersion("10"); //NOI18N
+
+    private final static Set<Tree.Kind> LITERALS_TYPE_SET = Collections.unmodifiableSet(
+            new HashSet<>(Arrays.asList(
+                    Tree.Kind.INT_LITERAL,
+                    Tree.Kind.LONG_LITERAL,
+                    Tree.Kind.FLOAT_LITERAL,
+                    Tree.Kind.DOUBLE_LITERAL,
+                    Tree.Kind.BOOLEAN_LITERAL,
+                    Tree.Kind.CHAR_LITERAL,
+                    Tree.Kind.STRING_LITERAL
+            )));
+
+    /**
+     *
+     * @param ctx : HintContext
+     * @return ErrorDescription in case Object reference type and Object
+     * instance type are same.
+     */
+    @Hint(displayName = "#DN_CanUseVarForObjectRef", description = "#DESC_CanUseVarForObjectRef", category = "suggestions") //NOI18N
+
+    @TriggerPattern("$mods$ $type $var =new $type($params$);") //NOI18N
+    @Messages("MSG_ConvertibleToVarType=Explict type can be replaced with 'var'")  //NOI18N
+    public static ErrorDescription checkNewObjInit(HintContext ctx) {
+
+        TreePath treePath = ctx.getVariables().get("$var");
+
+        if (treePath == null) {
+            return null;
+        }
+
+        if (!isHintEnabled(ctx)) {
+            return null;
+        }
+
+        Fix fix = new FixImpl(ctx.getInfo(), treePath).toEditorFix();
+        return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.MSG_ConvertibleToVarType(), fix);
+
+    }
+
+    /**
+     *
+     * @param ctx : HintContext
+     * @return ErrorDescription if variable initializer is a Literal
+     */
+    @Hint(displayName = "#DN_CanUseVarForLiteralRef", description = "#DESC_CanUseVarForLiteralRef", category = "suggestions")
+    @TriggerPattern("$mods$ $type $var = $value;") //NOI18N
+
+    public static ErrorDescription checkLiteralInit(HintContext ctx) {
+
+        TreePath initPath = ctx.getVariables().get("$value");
+
+        if (initPath == null) {
+            return null;
+        }
+
+        Tree l = initPath.getLeaf();
+
+        // checks rhs is a literal or not
+        if (!(isLiteral(l))) {
+            return null;
+        }
+
+        if (!isHintEnabled(ctx)) {
 
 Review comment:
   Same here why is isHintEnabled checked so late?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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