You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "Achal1607 (via GitHub)" <gi...@apache.org> on 2025/04/26 11:54:33 UTC

[PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Achal1607 opened a new pull request, #8460:
URL: https://github.com/apache/netbeans/pull/8460

   When you open any Maven project, when Netbeans tries to parse `pom.xml` sometimes it shows the class cast exception as seen in issue https://github.com/oracle/javavscode/issues/378 and https://github.com/oracle/javavscode/issues/185. The root cause is if some of the modules are not included like `org.netbeans.modules.editor.structure` and `org.netbeans.modules.editor.deprecated.pre65formatting`, then `LspMavenErrorProvider` class sends document as `FilterDocument` instead of `NbEditorDocument` which causes this class cast exception. 
   
   This PR addresses above mentioned issue by introducing a new utility method that accepts a `Document` and ensures it is returned as a `BaseDocument`. If the given `Document` is already an instance of `BaseDocument`, it is returned as-is; otherwise, it is converted accordingly.  
   This approach ensures that the implementation remains agnostic to the available modules, providing a consistent and reliable way to work with `BaseDocument` regardless of the module configuration.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Posted by "Achal1607 (via GitHub)" <gi...@apache.org>.
Achal1607 closed pull request #8460: Fix occasional ClassCastException when parsing Maven pom.xml files
URL: https://github.com/apache/netbeans/pull/8460


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #8460:
URL: https://github.com/apache/netbeans/pull/8460#discussion_r2061495133


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/MavenPOMParser.java:
##########
@@ -76,13 +78,17 @@ public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) t
             return;
         }
         //#236116 passing document protects from looking it up later and causing a deadlock.
-        final BaseDocument document = (BaseDocument)snapshot.getSource().getDocument(false);
+        final Document doc = snapshot.getSource().getDocument(false);
         final DataObject d = sFile.getLookup().lookup(DataObject.class);
+        try {
+            final BaseDocument document = Utilities.asBaseDocument(doc, d);
         ModelSource ms = Utilities.createModelSource(sFile, d, document);
         synchronized (this) {
             theModel = POMModelFactory.getDefault().getModel(ms);
             lastSnapshot = snapshot;
         }
+        } catch (IOException ignored) {
+    }

Review Comment:
   would replace this section with:
   ```java
           final Document document = snapshot.getSource().getDocument(false);
           final DataObject d = sFile.getLookup().lookup(DataObject.class);
           ModelSource ms = Utilities.createModelSource(sFile, d, document instanceof BaseDocument bd ? bd : null);
           synchronized (this) {
               theModel = POMModelFactory.getDefault().getModel(ms);
               lastSnapshot = snapshot;
           }
   ```
   and drop the change in `Utilities`.
   
   Looking through the `getDocument()` code and the comments in it, it sounds like that the VSCode plugins will hit fallback paths which were meant for tests when the document type is not `BaseDocument`. Other methods throw illegal argument exceptions when document isn't `BaseDocument`.
   
   But adding the instanceof check as hotfix is probably ok.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Posted by "Achal1607 (via GitHub)" <gi...@apache.org>.
Achal1607 commented on code in PR #8460:
URL: https://github.com/apache/netbeans/pull/8460#discussion_r2062908392


##########
java/maven.hints/src/org/netbeans/modules/maven/hints/pom/MavenPOMParser.java:
##########
@@ -76,13 +78,17 @@ public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) t
             return;
         }
         //#236116 passing document protects from looking it up later and causing a deadlock.
-        final BaseDocument document = (BaseDocument)snapshot.getSource().getDocument(false);
+        final Document doc = snapshot.getSource().getDocument(false);
         final DataObject d = sFile.getLookup().lookup(DataObject.class);
+        try {
+            final BaseDocument document = Utilities.asBaseDocument(doc, d);
         ModelSource ms = Utilities.createModelSource(sFile, d, document);
         synchronized (this) {
             theModel = POMModelFactory.getDefault().getModel(ms);
             lastSnapshot = snapshot;
         }
+        } catch (IOException ignored) {
+    }

Review Comment:
   Okay thanks @mbien, did the needful change.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Posted by "Achal1607 (via GitHub)" <gi...@apache.org>.
Achal1607 commented on PR #8460:
URL: https://github.com/apache/netbeans/pull/8460#issuecomment-2849981101

   Hey @mbien, Can you please review the PR?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] Fix occasional ClassCastException when parsing Maven pom.xml files [netbeans]

Posted by "lahodaj (via GitHub)" <gi...@apache.org>.
lahodaj commented on PR #8460:
URL: https://github.com/apache/netbeans/pull/8460#issuecomment-2850200019

   The comment on line 79 talks about a possible deadlock, referring to this issue:
   https://bz.apache.org/netbeans/show_bug.cgi?id=236116
   
   It is not completely clear if the deadlock can still happen. But some care is probably needed here.
   
   I wonder if the requirement for `BaseDocument` still applies - what would break if simply an ordinary `Document` would be used to create the `ModelSource`?
   
   Alternatively, if the `BaseDocument` is really needed by the users of `ModelSource`, maybe the `Document` can be wrapped to `BaseDocument` in `Utilities.createModelSource` around line 187 using the approach from `Utilities.getDocument`? (Although the code to create the `BaseDocument` out of thin air in `Utilities.getDocument` seems dubious to me.) Possibly factor the `BaseDocument` creation out of the `getDocument` method, or something like that.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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