From bc26bb922376581bfa62955c0f04892dcdcf6f58 Mon Sep 17 00:00:00 2001 From: Achal Talati Date: Wed, 3 Jan 2024 15:01:27 +0530 Subject: [PATCH] added 6780 patch Signed-off-by: Achal Talati --- build.xml | 4 +- patches/6780.diff | 223 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 patches/6780.diff diff --git a/build.xml b/build.xml index 6ab1cab..4a7a08d 100644 --- a/build.xml +++ b/build.xml @@ -31,7 +31,7 @@ - + @@ -215,4 +215,4 @@ - \ No newline at end of file + diff --git a/patches/6780.diff b/patches/6780.diff new file mode 100644 index 0000000..02a7ac5 --- /dev/null +++ b/patches/6780.diff @@ -0,0 +1,223 @@ +diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Bundle.properties b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Bundle.properties +index aa59c260fd14..eb396dcc3aca 100644 +--- a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Bundle.properties ++++ b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Bundle.properties +@@ -49,6 +49,10 @@ DESC_org.netbeans.modules.java.hints.bugs.Tiny.equalsNull=Finds invocations of t + ERR_equalsNull=Object equals "null" is never true + FIX_equalsNull=Use == instead of equals method + ++DN_org.netbeans.modules.java.hints.bugs.Tiny.varTypeDiamondOperator= var used with diamond operator ++DESC_org.netbeans.modules.java.hints.bugs.Tiny.varTypeDiamondOperator=Finds variables initialzed using var and diamond operator ++ERR_varTypeDiamondOperator=<> would be inferred as ++ + DN_org.netbeans.modules.java.hints.bugs.Tiny.resultSet=Incorrect column index in ResultSet + DESC_org.netbeans.modules.java.hints.bugs.Tiny.resultSet=Reports Iincorrect column indices passed to various methods of java.sql.ResultSet + ERR_ResultSetZero=Column index is zero, but ResultSet columns are counted from 1 +diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Tiny.java b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Tiny.java +index c3544a5a9f90..4ad1f50f2677 100644 +--- a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Tiny.java ++++ b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Tiny.java +@@ -32,9 +32,11 @@ + import com.sun.source.tree.LiteralTree; + import com.sun.source.tree.MemberSelectTree; + import com.sun.source.tree.MethodInvocationTree; ++import com.sun.source.tree.NewClassTree; + import com.sun.source.tree.SwitchTree; + import com.sun.source.tree.Tree; + import com.sun.source.tree.Tree.Kind; ++import com.sun.source.tree.VariableTree; + import com.sun.source.tree.WhileLoopTree; + import com.sun.source.util.SourcePositions; + import com.sun.source.util.TreePath; +@@ -51,6 +53,8 @@ + import javax.lang.model.element.ExecutableElement; + import javax.lang.model.element.Name; + import javax.lang.model.element.TypeElement; ++import javax.lang.model.element.TypeParameterElement; ++import javax.lang.model.element.VariableElement; + import javax.lang.model.type.DeclaredType; + import javax.lang.model.type.TypeKind; + import javax.lang.model.type.TypeMirror; +@@ -195,6 +199,42 @@ public static ErrorDescription equalsNull(HintContext ctx) { + return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), displayName, fix); + } + ++ @Hint(displayName = "#DN_org.netbeans.modules.java.hints.bugs.Tiny.varTypeDiamondOperator", description = "#DESC_org.netbeans.modules.java.hints.bugs.Tiny.varTypeDiamondOperator", category="bugs", suppressWarnings="AllowVarTypeDiamondOperator") ++ @TriggerPatterns({ ++ @TriggerPattern(value="$mods$ $varType $name = new $type<>($args$)", constraints=@ConstraintVariableType(variable="$type", type="java.util.Collection")), ++ @TriggerPattern(value="$mods$ $varType $name = new $type<>($args$)", constraints=@ConstraintVariableType(variable="$type", type="java.util.Map")) ++ }) ++ public static ErrorDescription varTypeDiamondOperator(HintContext ctx) { ++ TreePath path = ctx.getPath(); ++ Boolean isVarUsed = ctx.getInfo().getTreeUtilities().isVarType(path); ++ if(!isVarUsed){ ++ return null; ++ } ++ ++ VariableTree vt = (VariableTree) ctx.getPath().getLeaf(); ++ NewClassTree nct = (NewClassTree) vt.getInitializer(); ++ Element constructorCand = ctx.getInfo().getTrees().getElement(new TreePath(ctx.getPath(), nct)); ++ ++ if (constructorCand.getKind() != ElementKind.CONSTRUCTOR) { ++ return null; ++ } ++ ++ ExecutableElement constructor = (ExecutableElement) constructorCand; ++ ++ for (VariableElement param : constructor.getParameters()) { ++ if (param.asType().getKind() == TypeKind.DECLARED) { ++ DeclaredType dt = (DeclaredType) param.asType(); ++ if (!dt.getTypeArguments().isEmpty()) { ++ return null; ++ } ++ } ++ } ++ ++ String displayName = NbBundle.getMessage(Tiny.class, "ERR_varTypeDiamondOperator"); ++ ++ return ErrorDescriptionFactory.forTree(ctx, path, displayName); ++ } ++ + @Hint(displayName = "#DN_org.netbeans.modules.java.hints.bugs.Tiny.resultSet", description = "#DESC_org.netbeans.modules.java.hints.bugs.Tiny.resultSet", category="bugs", suppressWarnings="UseOfIndexZeroInJDBCResultSet", options=Options.QUERY) + @TriggerPattern(value="$set.$method($columnIndex, $other$)", + constraints={ +diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/Bundle_test.properties b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/Bundle_test.properties +index 6b96fc845f38..e92c7d63fec4 100644 +--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/Bundle_test.properties ++++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/Bundle_test.properties +@@ -33,6 +33,8 @@ ERR_system_arraycopy_negative={0} is negative + ERR_equalsNull=ERR_equalsNull + FIX_equalsNull=FIX_equalsNull + ++ERR_varTypeDiamondOperator=ERR_varTypeDiamondOperator ++ + ERR_ResultSetZero=ERR_ResultSetZero + ERR_ResultSetNegative=ERR_ResultSetNegative + +diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/TinyTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/TinyTest.java +index 824bfca951c1..4e496beda630 100644 +--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/TinyTest.java ++++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/TinyTest.java +@@ -356,6 +356,121 @@ public void testInconsistentIndentationLast() throws Exception { + } + } + ++ public void testVarUsageWithoutExplicitType() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " var v = new java.util.ArrayList<>();\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings("3:8-3:44:verifier:ERR_varTypeDiamondOperator"); ++ } ++ ++ public void testVarUsageWithoutExplicitType2() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " var v = new java.util.HashSet<>();\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings("3:8-3:42:verifier:ERR_varTypeDiamondOperator"); ++ } ++ ++ public void testVarUsageWithoutExplicitType3() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " var v = new java.util.HashMap<>();\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings("3:8-3:42:verifier:ERR_varTypeDiamondOperator"); ++ } ++ ++ public void testVarUsageWithExplicitType() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " var v = new java.util.ArrayList();\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ ++ public void testVarUsageWithExplicitType2() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " var v = new String[2];\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ ++ public void testWithoutVarUsageWithExplicitType() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " java.util.List v = new java.util.ArrayList();\n" + ++ " }\n" + ++ "}\n") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ ++ public void testWithoutVarUsageWithExplicitType2() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test() {\n" + ++ " java.util.List v = new java.util.ArrayList<>();\n" + ++ " }\n" + ++ "}\n") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ ++ public void testVarUsageSensibleTypeInferred1() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test(java.util.Set input) {\n" + ++ " var v = new java.util.HashSet<>(input);\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ ++ public void testVarUsageSensibleTypeInferred2() throws Exception { ++ HintTest.create() ++ .input("package test;\n" + ++ "public class Test {\n" + ++ " public void test(java.util.Map input) {\n" + ++ " var v = new java.util.HashMap<>(input);\n" + ++ " }\n" + ++ "}\n") ++ .sourceLevel("11") ++ .run(Tiny.class) ++ .assertWarnings(); ++ } ++ + private static Map alterSettings(String... settings) throws Exception { + //XXX: hack, need to initialize the HintTest's lookup before setting the + //formatting preferences +