This is an automated email from the ASF dual-hosted git repository. jlahoda pushed a commit to branch release90 in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git
commit de3c2bf71c059c9bbc6d811c9a95ea48e84b04bf Author: Arunava Sinha <[email protected]> AuthorDate: Mon May 7 23:14:51 2018 +0530 [NETBEANS-481] JDK10-LVTI: 1.Refactored Test File for CasualDiff changes 2. Corrected Code for Skipping hint for Parameterized type array members --- .../ConvertInvalidVarToExplicitArrayType.java | 25 ++--- .../ConvertInvalidVarToExplicitArrayTypeTest.java | 60 +++++++++--- ...> InvalidVarToExplicitArrayConversionTest.java} | 104 ++++++++------------- 3 files changed, 100 insertions(+), 89 deletions(-) diff --git a/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java b/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java index cdc9972..1f9bc12 100644 --- a/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java @@ -21,7 +21,6 @@ package org.netbeans.modules.java.hints.errors; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.NewArrayTree; import com.sun.source.tree.NewClassTree; -import com.sun.source.tree.Scope; import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; import java.util.Collections; @@ -30,8 +29,7 @@ import java.util.List; import java.util.Set; import com.sun.source.tree.VariableTree; import com.sun.source.util.Trees; -import java.util.HashMap; -import java.util.Map; +import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import org.netbeans.api.java.source.CompilationInfo; @@ -49,7 +47,7 @@ import javax.lang.model.util.Types; * @author arusinha */ @Messages({ - "DN_ConvertVarToExplicitType=Convert Var to Explicit Type" + "DN_ConvertVarToExplicitType=Replace var with explicit type" // NOI18N }) public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> { @@ -76,6 +74,13 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> { TypeMirror arrayType = null; if (treePath.getLeaf().getKind() == Tree.Kind.VARIABLE) { VariableTree oldVariableTree = (VariableTree) treePath.getLeaf(); + + ExpressionTree init = oldVariableTree.getInitializer(); + + if (init == null || init.getKind() != Tree.Kind.NEW_ARRAY) { + return null; + } + NewArrayTree arrayTree = (NewArrayTree) oldVariableTree.getInitializer(); List<? extends ExpressionTree> currentValues = arrayTree.getInitializers(); TreePath initArrayTreePath = new TreePath(treePath, arrayTree); @@ -87,13 +92,10 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> { TypeMirror etType = trees.getTypeMirror(new TreePath(initArrayTreePath, tree)); //skipped fix for parameterized array member as parameterized array is not possible. - if (tree.getKind() == Tree.Kind.NEW_CLASS) { - NewClassTree nct = (NewClassTree) tree; - if (nct.getIdentifier().getKind() == Tree.Kind.PARAMETERIZED_TYPE) { - - return null; - } + if (etType.getKind() == TypeKind.DECLARED && !((DeclaredType) etType).getTypeArguments().isEmpty()) { + return null; } + if (arrayType == null) { arrayType = etType; } else if (!types.isAssignable(etType, arrayType)) { @@ -104,8 +106,9 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> { } } } + return Collections.<Fix>singletonList(new FixImpl(compilationInfo, treePath, arrayType).toEditorFix()); } - return Collections.<Fix>singletonList(new FixImpl(compilationInfo, treePath, arrayType).toEditorFix()); + return Collections.<Fix>emptyList(); } diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java index b357242..50d07eb 100644 --- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java +++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java @@ -33,6 +33,8 @@ import org.netbeans.api.java.source.CompilationInfo; */ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase { + private static final String FIX_MSG = "Replace var with explicit type"; // NOI18N + public ConvertInvalidVarToExplicitArrayTypeTest(String name) throws Exception { super(name, ConvertInvalidVarToExplicitArrayType.class); } @@ -56,6 +58,38 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase -1); } + public void testParameterizedElements2() throws Exception { + performAnalysisTest("test/Test.java", + "package test; import java.util.ArrayList; import java.util.Arrays; public class Test" + + "{{ \n" + + " ArrayList<ArrayList<String>> l = new ArrayList<ArrayList<String>>(); \n" + + " ArrayList<String> places = new ArrayList<String>(Arrays.asList(\"New York\", \"Tokyo\"));\n" + + " l.add(places); " + + " final var j = {l.get(0)};" + + "}}", + -1); + } + + public void testMethodInvocation() throws Exception { + performAnalysisTest("test/Test.java", + "package test; import java.util.ArrayList; import java.util.Arrays; public class Test" + + " {{ \n" + + " ArrayList<String> places = new ArrayList<String>(Arrays.asList(\"New York\", \"Tokyo\"));\n" + + " final var j = {places.get(0)};\n" + + "}}", + -1, FIX_MSG); + } + + public void testMethodInvocation2() throws Exception { + performFixTest("test/Test.java", + "package test; public class Test" + + " {{ \n" + + " final var arr = {m3()};}" + + " static String m3(){ return new String(\"hello\") ;}" + + "}", + -1, FIX_MSG, "package test; public class Test {{ final String[] arr = {m3()};} static String m3(){ return new String(\"hello\") ;}}"); + } + public void testArrayHetrogeneousElements() throws Exception { performAnalysisTest("test/Test.java", "package test; public class Test {{final/*comment1*/ var/**comment2**/ j/*comment3*/ = /*comment4*/{new java.util.ArrayList(),new java.util.HashMap()};}}", @@ -65,7 +99,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase public void testArrayObjectElementsFix() throws Exception { performFixTest("test/Test.java", "package test; public class Test {{final/*comment1*/ var/**comment2**/ j/*comment3*/ = /*comment4*/{new java.util.ArrayList(),new java.util.ArrayList()};}}", - -1, "Convert Var to Explicit Type", + -1, FIX_MSG, "package test; import java.util.ArrayList; public class Test {{final/*comment1*/ ArrayList[]/**comment2**/ j/*comment3*/ = /*comment4*/{new java.util.ArrayList(),new java.util.ArrayList()};}}"); } @@ -73,7 +107,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{final var j = {1,2.1,3f};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{final double[] j = {1,2.1,3f};}}"); } @@ -81,7 +115,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{final var j = {(short)1,(byte)2};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{final short[] j = {(short)1,(byte)2};}}"); } @@ -89,7 +123,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{/*comment1*/ /*comment2*/@NotNull final var j = {\"hello\",\"world\"};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{/*comment1*/ /*comment2*/@NotNull final String[] j = {\"hello\",\"world\"};}}"); } @@ -97,7 +131,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{@NotNull final var j = {new Object(),new Object()};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{@NotNull final Object[] j = {new Object(),new Object()};}}"); } @@ -105,7 +139,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{@NotNull var j = {new Object(),new Object()};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{@NotNull Object[] j = {new Object(),new Object()};}}"); } @@ -113,7 +147,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{final @NotNull var j = {new Object(),new Object()};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{final @NotNull Object[] j = {new Object(),new Object()};}}"); } @@ -121,7 +155,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{final/*comment1*/var a = {new Object(),new Object()};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{final/*comment1*/Object[] a = {new Object(),new Object()};}}"); } @@ -129,7 +163,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{final/*comment1*/var /*comment2*/ a = {2,3.1f};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{final/*comment1*/float[] /*comment2*/ a = {2,3.1f};}}"); } @@ -137,7 +171,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{/*comment1*/var/*comment2*/ a = {2,3.1f};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{/*comment1*/float[]/*comment2*/ a = {2,3.1f};}}"); } @@ -145,7 +179,7 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{var/*comment1*/ a = {2,3.1f};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{float[]/*comment1*/ a = {2,3.1f};}}"); } @@ -153,14 +187,14 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase performFixTest("test/Test.java", "package test; public class Test {{@NotNull var j = {new Object(),new Object()};}}", -1, - "Convert Var to Explicit Type", + FIX_MSG, "package test; public class Test {{@NotNull Object[] j = {new Object(),new Object()};}}"); } public void testArrayObject9ElementsFix() throws Exception { performFixTest("test/Test.java", "package test; public class Test {{var/*comment1*/ k = {1,'c'};}}", - -1, "Convert Var to Explicit Type", + -1, FIX_MSG, "package test; public class Test {{int[]/*comment1*/ k = {1,'c'};}}"); } diff --git a/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/VarTypeTest.java b/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/InvalidVarToExplicitArrayConversionTest.java similarity index 67% rename from java.source.base/test/unit/src/org/netbeans/api/java/source/gen/VarTypeTest.java rename to java.source.base/test/unit/src/org/netbeans/api/java/source/gen/InvalidVarToExplicitArrayConversionTest.java index 4ce6433..c2c5d98 100644 --- a/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/VarTypeTest.java +++ b/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/InvalidVarToExplicitArrayConversionTest.java @@ -23,63 +23,45 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.MethodTree; import com.sun.source.tree.VariableTree; import java.io.IOException; -import java.net.URL; import static junit.framework.TestCase.assertNotNull; import org.netbeans.api.java.source.JavaSource; -import org.netbeans.api.java.source.SourceUtilsTestUtil; import org.netbeans.api.java.source.Task; import org.netbeans.api.java.source.TestUtilities; import org.netbeans.api.java.source.TreeMaker; import org.netbeans.api.java.source.WorkingCopy; -import org.netbeans.core.startup.Main; import org.netbeans.junit.NbTestSuite; -import org.netbeans.modules.java.source.indexing.TransactionContext; import org.netbeans.modules.java.source.parsing.JavacParser; -import org.netbeans.modules.java.source.usages.ClassIndexManager; -import org.netbeans.modules.parsing.api.indexing.IndexingManager; -import org.netbeans.modules.parsing.impl.indexing.CacheFolder; -import org.openide.filesystems.FileObject; -import org.openide.filesystems.FileUtil; /** - * Tests 'var' type variable rewrite statements. + * Tests conversion of invalid var type variable to explicit array type. * * @author arusinha */ -public class VarTypeTest extends GeneratorTestBase { +public class InvalidVarToExplicitArrayConversionTest extends TreeRewriteTestBase { - private static final String SOURCE_LEVEL = "1.10"; // NOI18N - - public VarTypeTest(String testName) { + public InvalidVarToExplicitArrayConversionTest(String testName) { super(testName); } public static NbTestSuite suite() { NbTestSuite suite = new NbTestSuite(); - suite.addTestSuite(VarTypeTest.class); + suite.addTestSuite(InvalidVarToExplicitArrayConversionTest.class); return suite; } @Override protected void setUp() throws Exception { - JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; super.setUp(); - TestUtilities.analyzeBinaries(SourceUtilsTestUtil.getBootClassPath()); - Main.initializeURLFactory(); + sourceLevel = "1.10"; + JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; + } @Override protected void tearDown() throws Exception { - JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = false; - for (URL bootCP : SourceUtilsTestUtil.getBootClassPath()) { - TransactionContext ctx = TransactionContext.beginStandardTransaction(bootCP, false, () -> false, false); - try { - ClassIndexManager.getDefault().removeRoot(bootCP); - } finally { - ctx.commit(); - } - } super.tearDown(); + JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = false; + } public void testInvalidVarToExplicitArrayConversion() throws Exception { @@ -97,10 +79,10 @@ public class VarTypeTest extends GeneratorTestBase { + " }\n" + "}\n"; - prepareTest(code); + prepareTest("Test", code); - RewriteInvalidVarArrayInitStatement("int"); - String res = TestUtilities.copyFileToString(testFile); + rewriteStatement("int"); + String res = TestUtilities.copyFileToString(getTestFile()); System.err.println(res); assertEquals(golden, res); @@ -115,6 +97,7 @@ public class VarTypeTest extends GeneratorTestBase { + " /*comment1*/var k = {new ArrayList(), new ArrayList()};\n" + " }\n" + "}\n"; + String golden = "package test;\n" + "import java.util.ArrayList;\n" + "public class Test {\n" @@ -123,16 +106,15 @@ public class VarTypeTest extends GeneratorTestBase { + " }\n" + "}\n"; - prepareTest(code); + prepareTest("Test", code); - RewriteInvalidVarArrayInitStatement("ArrayList"); - String res = TestUtilities.copyFileToString(testFile); + rewriteStatement("ArrayList"); + String res = TestUtilities.copyFileToString(getTestFile()); System.err.println(res); assertEquals(golden, res); } public void testInvalidVarToExplicitArray3Conversion() throws Exception { - String code = "package test;\n" + "import java.util.ArrayList;\n" + "public class Test {\n" @@ -148,34 +130,35 @@ public class VarTypeTest extends GeneratorTestBase { + " }\n" + "}\n"; - prepareTest(code); + prepareTest("Test", code); - RewriteInvalidVarArrayInitStatement("String"); - String res = TestUtilities.copyFileToString(testFile); + rewriteStatement("String"); + String res = TestUtilities.copyFileToString(getTestFile()); System.err.println(res); assertEquals(golden, res); } - private void prepareTest(String code) throws Exception { - - FileObject workFO = FileUtil.toFileObject(getWorkDir()); - - assertNotNull(workFO); + public void testInvalidVarToExplicitArray4Conversion() throws Exception { - FileObject sourceRoot = FileUtil.createFolder(workFO, "src"); - FileObject buildRoot = workFO.createFolder("build"); - FileObject data = FileUtil.createData(sourceRoot, "Test.java"); - - testFile = FileUtil.toFile(data); - assertNotNull(testFile); - TestUtilities.copyStringToFile(testFile, code); - - SourceUtilsTestUtil.setSourceLevel(data, SOURCE_LEVEL); + String code = "package test;\n" + + "public class Test {\n" + + " void m1() {\n" + + " var/*comment2*/ k = {1, 'C'};\n" + + " }\n" + + "}\n"; + String golden = "package test;\n" + + "public class Test {\n" + + " void m1() {\n" + + " int[]/*comment2*/ k = {1, 'C'};\n" + + " }\n" + + "}\n"; - SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, CacheFolder.getCacheFolder(), new FileObject[0]); + prepareTest("Test", code); - //re-index, in order to find classes-living-elsewhere - IndexingManager.getDefault().refreshIndexAndWait(sourceRoot.getURL(), null); + rewriteStatement("int"); + String res = TestUtilities.copyFileToString(getTestFile()); + System.err.println(res); + assertEquals(golden, res); } /** @@ -185,9 +168,9 @@ public class VarTypeTest extends GeneratorTestBase { * @param arrayType : target explicit array type. * @throws IOException */ - private void RewriteInvalidVarArrayInitStatement(String arrayType) throws IOException { + private void rewriteStatement(String arrayType) throws IOException { - JavaSource js = getJavaSource(getTestFile()); + JavaSource js = getJavaSource(); assertNotNull(js); Task<WorkingCopy> task = new Task<WorkingCopy>() { @@ -211,15 +194,6 @@ public class VarTypeTest extends GeneratorTestBase { } }; js.runModificationTask(task).commit(); - - } - - String getGoldenPckg() { - return ""; - } - - String getSourcePckg() { - return ""; } } -- To stop receiving notification emails like this one, please contact [email protected]. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
