ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 751 forks source link

Null pointer during SyntaxTree generation with TextDocumentChange #23719

Closed suhothayan closed 4 years ago

suhothayan commented 4 years ago

Description: Null pointer exception is thrown when executing the bellow code.

        String input = "import ballerina/io;\n" +
                "\n" +
                "public function main() {\n" +
                "    io:println(\"Hello, World!\");\n" +
                "}\n";

        TextDocument textDocument = TextDocuments.from(input);
        SyntaxTree oldTree = BLModules.parse(textDocument);
        System.out.println(oldTree);
        System.out.println(((FunctionDefinitionNode) oldTree.getModulePart().members().get(0)).functionBody().statements().get(0).span());

        TextEdit[] edits = new TextEdit[]{new TextEdit(new TextRange(47, 47), "accuweather:Client accuweatherClient = new (\"8dbh68Zg2J6WxAK37Cy2jVJTSMdnyAmV\");\n")
        };
        TextDocumentChange textDocumentChange = TextDocumentChange.from(edits.toArray(
                new io.ballerinalang.compiler.text.TextEdit[0]));
        SyntaxTree newTree = SyntaxTree.from(oldTree, textDocumentChange);
        System.out.println(newTree);

The exception is as follows

java.lang.NullPointerException
at io.ballerinalang.compiler.internal.parser.incremental.HybridTokenReader.read(HybridTokenReader.java:47)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.consume(BallerinaParser.java:308)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseQualifiedIdentifier(BallerinaParser.java:2463)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseQualifiedIdentifier(BallerinaParser.java:2442)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatementStartIdentifier(BallerinaParser.java:1903)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatementStartsWithIdentifier(BallerinaParser.java:5494)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatement(BallerinaParser.java:2744)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatement(BallerinaParser.java:2679)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatement(BallerinaParser.java:2621)
at io.ballerinalang.compiler.internal.parser.incremental.IncrementalParser.parseStatement(IncrementalParser.java:54)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseStatements(BallerinaParser.java:2603)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseFunctionBodyBlock(BallerinaParser.java:1794)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseFunctionBody(BallerinaParser.java:1753)
at io.ballerinalang.compiler.internal.parser.incremental.IncrementalParser.parseFunctionBody(IncrementalParser.java:49)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseFunctionBody(BallerinaParser.java:1739)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseFunctionDefinition(BallerinaParser.java:1123)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseTopLevelNode(BallerinaParser.java:1015)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseTopLevelNode(BallerinaParser.java:499)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseTopLevelNode(BallerinaParser.java:434)
at io.ballerinalang.compiler.internal.parser.incremental.IncrementalParser.parseTopLevelNode(IncrementalParser.java:44)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parseCompUnit(BallerinaParser.java:346)
at io.ballerinalang.compiler.internal.parser.BallerinaParser.parse(BallerinaParser.java:60)
at io.ballerinalang.compiler.syntax.BLModules.parse(BLModules.java:45)
at io.ballerinalang.compiler.parser.test.incremental.ModuleLevelDeclarationTest.testVariableNameChange(ModuleLevelDeclarationTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.runTests(TestNGTestClassProcessor.java:141)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.stop(TestNGTestClassProcessor.java:90)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)

A possible solution for this issue is at https://github.com/ballerina-platform/ballerina-lang/pull/23522/files/2175afd8d961c8c19f2386f703f2dac00fa33ea8#diff-ad6f2e631a5bae6f6ac5edce17a7e028

As requested by @sameerajayasoma, the same fix need to be added to other relevant places too.

Affected Versions: version=2.0.0-SNAPSHOT

sameerajayasoma commented 4 years ago

This is fixed in the current master.