I found the way to enforce my singleton action: annotate a static factory 
method instead of the action class. It was not easy to find -thank you copilot !
The different returned instances issue is probably due to the GC. I think 
lookup has to use weak references in the process, and I did not hold a strong 
reference to the action between the 2 calls (I thought it was done by the 
framework). 
I'll try to update some NB docs regarding actions, because the overall thing is 
(too IMHO) complex...
Jerome

On 2025/10/03 19:31:02 Jerome Lelasseux wrote: Still trying to use NB 
annotations to declare a singleton action class, this is driving me nuts...
I did some tests. Calling Actions.forID(x, y) twice in a row returns the same 
instance. Same for Utilities.actionsForPath(). Same in my app. 
BUT, if I run the second call to Actions.forID(x,y) a few seconds later in my 
app (I trigger it manually with the mouse) then, *sometimes* (1/3?), I get a 
new instance ! Same problem for Utilities.actionsForPath(). Note that both 
calls are done on the EDT.
I looked at the NB code on github, the underlying lookup mechanism in 
filesystem/RecognizeInstanceFiles.java is based on ProxyLookup, which uses lazy 
instanciation. So I assume that sometimes something happens differently here. 
But can not understand why...
I noticed that Actions.forID() uses FileUtil.getFileObject(). Doc says "In 
environment with multiple contextual Lookups, the method may return different 
FileObject depending on what Lookup serves the executing thread." And 
recommends to use FileUtil.getSystemFileObject() to make sure to get a single 
instance. What is a "contextual lookup" here ? I guess I have to try recoding 
forID() with getSystemFileObject...
Jerome

On 2025/10/02 21:17:20 Jerome Lelasseux wrote: Hi, 
I have an action with a no-arg constructor :

@ActionID(category = "jjazz", id = "myactionid")
@ActionRegistration(displayName = "not_used", lazy = false)
@ActionReference(path = "Actions/popupmenu", position = 100)public class 
MyAction extends AbstractAction........

In my code an instance is first created using Actions.forId("jjazz", 
"myactionid") 

I also want to use MyAction in a popup menu. I use 
Utilities.actionsForPath("Actions/popupmenu") to retrieve the actions before 
creating the popupmenu. But actionsForPath() creates a new instance of 
MyAction. Why ?   

I'm pretty sure in other cases it reuses the initial instance. Tried to debug 
but it does not work inside actionsForPath() -don't know why...
Any tip welcome, thanks.
Jerome

Reply via email to