HuntController and EventQueue for NetBeans

We are creating an application on the Netbeans platform, and one part of it is an editor for the specific language that we use.

We have the following class to highlight syntax errors:

class SyntaxErrorsHighlightingTask extends org.netbeans.modules.parsing.spi.ParserResultTask { public SyntaxErrorsHighlightingTask () { } @Override public void run (org.netbeans.modules.parsing.spi.Parser.Result result, org.netbeans.modules.parsing.spi.SchedulerEvent event) { try { final javax.swing.text.Document document = result.getSnapshot().getSource ().getDocument(false); final List<ErrorDescription> errors = new ArrayList<ErrorDescription> (); // finds errors on the document and add them to 'errors' list } /*** OFFENDING CODE GOES HERE ***/ } catch (javax.swing.text.BadLocationException ex1) { org.openide.util.Exceptions.printStackTrace (ex1); } catch (org.netbeans.modules.parsing.spi.ParseException ex1) { Exceptions.printStackTrace (ex1); } } @Override public int getPriority () { return 100; } @Override public Class<? extends Scheduler> getSchedulerClass () { return Scheduler.EDITOR_SENSITIVE_TASK_SCHEDULER; } @Override public void cancel () { } 

}

The violation code that throws the exception is as follows:

 org.netbeans.spi.editor.hints.HintsController.setErrors (document, "testsequence", errors); 

Based on the search results has been changed to the following:

 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.err.println("is EDT? " + SwingUtilities.isEventDispatchThread()); HintsController.setErrors (document, "testsequence", errors); } }); 

The following is information about what happens when a syntax error is entered in the editor:

 is EDT? true SEVERE [org.openide.util.RequestProcessor]: Error in RequestProcessor org.netbeans.spi.editor.hints.HintsController$1 java.lang.IllegalStateException: Must be run in EQ at org.netbeans.editor.Annotations.addAnnotation(Annotations.java:195) at org.netbeans.modules.editor.NbEditorDocument.addAnnotation(NbEditorDocument.java:251) at org.openide.text.NbDocument.addAnnotation(NbDocument.java:504) at org.netbeans.modules.editor.hints.AnnotationHolder$NbDocumentAttacher.attachAnnotation(AnnotationHolder.java:235) at org.netbeans.modules.editor.hints.AnnotationHolder.attachAnnotation(AnnotationHolder.java:208) at org.netbeans.modules.editor.hints.AnnotationHolder.updateAnnotationOnLine(AnnotationHolder.java:674) at org.netbeans.modules.editor.hints.AnnotationHolder.setErrorDescriptionsImpl(AnnotationHolder.java:899) at org.netbeans.modules.editor.hints.AnnotationHolder.access$1300(AnnotationHolder.java:113) at org.netbeans.modules.editor.hints.AnnotationHolder$4.run(AnnotationHolder.java:812) at org.netbeans.editor.BaseDocument.render(BaseDocument.java:1409) at org.netbeans.modules.editor.hints.AnnotationHolder.setErrorDescriptions(AnnotationHolder.java:809) at org.netbeans.modules.editor.hints.HintsControllerImpl.setErrorsImpl(HintsControllerImpl.java:111) at org.netbeans.modules.editor.hints.HintsControllerImpl.setErrors(HintsControllerImpl.java:93) at org.netbeans.spi.editor.hints.HintsController$1.run(HintsController.java:79) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1424) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:1968) Caused: org.openide.util.RequestProcessor$SlowItem: task failed due to at org.openide.util.RequestProcessor.post(RequestProcessor.java:425) at org.netbeans.spi.editor.hints.HintsController.setErrors(HintsController.java:77) at com.#.#.#.editor.parser.SyntaxErrorsHighlightingTask$1.run(SyntaxErrorsHighlightingTask.java:74) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:148) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) [catch] at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

What happens is a call to the HintsController in the EDT (ThreadDispatch Thread). However, Annotations.addAnnotation () starts in another thread - sometimes in the System Clipboard Synchronizer thread, sometimes in the Inactive Processor Request thread. Since it checks if it is running on EDT, it always throws an IllegalStateException.

I am not an expert in using the Netbeans platform, and I am pretty new to this particular application in the company, so I might be missing out on something really obvious. Google did not help much. Anyone have any tips?

+4
source share
2 answers

Turns out this is not a code issue.

As indicated in the NetBeans-dev list:

HintsController.setErrors can be called from any thread - it uses its own workflow and, if necessary, reconfigure the AWT thread.

The requirement to call Annotations.addAnnotation on an AWT stream has been removed a while ago: http://hg.netbeans.org/main-silver/rev/db82e4e0fbcc

In the same set of changes, automatic rebuilding to the AWT stream in NbDocument.addAnnotation has also been removed. Thus, it seems that the assembly you are using has the second part of a set of changes, but not the first part (...)

After a thorough analysis of the maven pom.xml files, I realized that the application loads newer versions of libs when the module loads older versions, so it runs the wrong code. A related question is what is here .

+3
source

Maybe you should try updating your errors with a method like this:

 private void updateError(javax.swing.text.Document document, List<ErrorDescription> errors) { if(javax.swing.SwingUtilities.isEventDispatchThread()) { HintsController.setErrors (document, "testsequence", errors); } else { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { HintsController.setErrors (document, "testsequence", errors); } }); } } 
+1
source

Source: https://habr.com/ru/post/1392875/


All Articles