ClassCastException codeAction failure in VSCode
ClassCastException
Continued from #454 When highlighting text in a Jakarta file, this sometimes appears. Unsure of specifics, but seems to be when a method has an annotation, and highlighting the entire method and annotation at the same time.
[Error - 5:19:09 PM] Request textDocument/codeAction failed.
Message: Request jakarta/java/codeaction failed with message: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.TypeDeclaration cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @45a2759a)
Code: -32603
To reproduce:
-
In InjectAndDisposesObservesObservesAsync.java, highlight any method starting from the
@Injectall the way to the method closing bracket} -
- Notes: If starting from before the
@, error shows when highlighting down to the closing bracket}
- Notes: If starting from before the
-
- If starting from after the
@but beforeInject, error shows when highlight reaches the method name (with the diagnostic).
- If starting from after the
Need to determine how pervasive this issue is.
Was seeing this behaviour before the changes in https://github.com/eclipse/lsp4jakarta/pull/453 Initially reported in https://github.com/OpenLiberty/liberty-tools-vscode/issues/247 and #449 Here is the stacktrace output from Java language support in VS Code:
[Info - 1:59:54 PM] May 26, 2023, 1:59:54 p.m. Executing command 'jakarta/java/codeaction' in LSP4Jakarta JDT LS extension
[Error - 1:59:54 PM] May 26, 2023, 1:59:54 p.m. Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ls.core".
java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
java.util.concurrent.ExecutionException: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
at java.base/java.util.concurrent.CompletableFuture.reportGet(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.get(Unknown Source)
at org.eclipse.lsp4jakarta.jdt.internal.core.ls.JakartaDelegateCommandHandlerForJava.executeCommand(JakartaDelegateCommandHandlerForJava.java:52)
at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)
at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:585)
at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
at org.eclipse.lsp4jakarta.jdt.core.jax_rs.NonPublicResourceMethodQuickFix.getCodeActions(NonPublicResourceMethodQuickFix.java:46)
at org.eclipse.lsp4jakarta.jdt.codeAction.CodeActionHandler.codeAction(CodeActionHandler.java:171)
at org.eclipse.lsp4jakarta.jdt.core.JDTServicesManager.getCodeAction(JDTServicesManager.java:185)
at org.eclipse.lsp4jakarta.jdt.internal.core.ls.JakartaDelegateCommandHandlerForJava.lambda$2(JakartaDelegateCommandHandlerForJava.java:143)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.helpAsyncBlocker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.helpAsyncBlocker(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.waitingGet(Unknown Source)
... 14 more
[Error - 1:59:54 PM] May 26, 2023, 1:59:54 p.m. Error in calling delegate command handler
java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
java.util.concurrent.ExecutionException: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
at java.base/java.util.concurrent.CompletableFuture.reportGet(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.get(Unknown Source)
at org.eclipse.lsp4jakarta.jdt.internal.core.ls.JakartaDelegateCommandHandlerForJava.executeCommand(JakartaDelegateCommandHandlerForJava.java:52)
at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)
at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:585)
at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.scan(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1c788329)
at org.eclipse.lsp4jakarta.jdt.core.jax_rs.NonPublicResourceMethodQuickFix.getCodeActions(NonPublicResourceMethodQuickFix.java:46)
at org.eclipse.lsp4jakarta.jdt.codeAction.CodeActionHandler.codeAction(CodeActionHandler.java:171)
at org.eclipse.lsp4jakarta.jdt.core.JDTServicesManager.getCodeAction(JDTServicesManager.java:185)
at org.eclipse.lsp4jakarta.jdt.internal.core.ls.JakartaDelegateCommandHandlerForJava.lambda$2(JakartaDelegateCommandHandlerForJava.java:143)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$Completion.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.helpAsyncBlocker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.helpAsyncBlocker(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.waitingGet(Unknown Source)
... 14 more
Interestingly, when surrounding the LSP4Jakarta request in VS Code with a try/catch, the results change up a bit. This error is caught on the VS Code plugin side:
Error: java.lang.ClassCastException: class org.eclipse.jdt.core.dom.CompilationUnit cannot be cast to class org.eclipse.jdt.core.dom.MethodDeclaration (org.eclipse.jdt.core.dom.CompilationUnit and org.eclipse.jdt.core.dom.MethodDeclaration are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1dfa95ca)
at c:\Users\TREVORCrawford\.vscode\extensions\redhat.java-1.18.0-win32-x64\dist\extension.js:2:1071113
at ie (c:\Users\TREVORCrawford\.vscode\extensions\redhat.java-1.18.0-win32-x64\dist\extension.js:2:1071407)
at c:\Users\TREVORCrawford\.vscode\extensions\redhat.java-1.18.0-win32-x64\dist\extension.js:2:1066191
at Immediate.<anonymous> (c:\Users\TREVORCrawford\.vscode\extensions\redhat.java-1.18.0-win32-x64\dist\extension.js:2:1066211)
at process.processImmediate (C:\Users\TREVORCrawford\Git\liberty-tools-vscode\lib\internal\timers.js:466:21)
at process.callbackTrampoline (node:internal/async_hooks:130:17) {code: -32001, data: {…}, stack: 'Error: java.lang.ClassCastException: class or…Trampoline (node:internal/async_hooks:130:17)', message: 'java.lang.ClassCastException: class org.ecli…nternal.loader.EquinoxClassLoader @1dfa95ca)'}
The same stacktrace from the above comment shows up in the Java language support output. And there is now an NPE stacktrace in the Jakarta LS output:
May 26, 2023 6:43:26 P.M. org.eclipse.lsp4j.jsonrpc.RemoteEndpoint fallbackResponseError
SEVERE: Internal error: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.complete(Unknown Source)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleResponse(RemoteEndpoint.java:212)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:193)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at org.eclipse.lsp4jakarta.JakartaTextDocumentService.lambda$codeAction$6(JakartaTextDocumentService.java:186)
... 13 more
[Error - 6:43:26 PM] Request textDocument/codeAction failed.
Message: Internal error.
Code: -32603
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.complete(Unknown Source)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleResponse(RemoteEndpoint.java:212)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:193)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at org.eclipse.lsp4jakarta.JakartaTextDocumentService.lambda$codeAction$6(JakartaTextDocumentService.java:186)
... 13 more
May 26, 2023 6:43:26 P.M. org.eclipse.lsp4j.jsonrpc.RemoteEndpoint handleCancellation
WARNING: Unmatched cancel notification for request id 6
May 26, 2023 6:43:26 P.M. org.eclipse.lsp4j.jsonrpc.RemoteEndpoint fallbackResponseError
SEVERE: Internal error: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.complete(Unknown Source)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleResponse(RemoteEndpoint.java:212)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:193)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at org.eclipse.lsp4jakarta.JakartaTextDocumentService.lambda$codeAction$6(JakartaTextDocumentService.java:186)
... 13 more
[Error - 6:43:26 PM] Request textDocument/codeAction failed.
Message: Internal error.
Code: -32603
java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.base/java.util.concurrent.CompletableFuture.complete(Unknown Source)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleResponse(RemoteEndpoint.java:212)
at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:193)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:194)
at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94)
at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:113)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "codeActions" is null
at org.eclipse.lsp4jakarta.JakartaTextDocumentService.lambda$codeAction$6(JakartaTextDocumentService.java:186)
... 13 more
In my testing with VSCode using the latest driver dated May 29th, liberty-dev-vscode-ext-23.0.6-SNAPSHOT.vsix, I was not able to recreate this exception.
I was able to recreate this issue with the 23.0.6-SNAPSHOT on Mac.
@dshimo can you record how you were able to recreate the exception? Were there multiple ways it occurred or just one?
I was able to consistently recreate it with InjectAndDisposesObservesObservesAsync.java and all it took was the simple highlight as suggested.
The issue seems to occur when highlighting with the @Inject validator diagnostic. I copied that method and put it in another Java file to reproduce it.
If you resolve the diagnostic by removing the @Dispose in the param, the highlight error vanishes.