Re: lookup confusing me

"Ingleby, Graeme" <[email protected]> Fri, 15 Sep 2017 05:39:45 +0000
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1@SATLRCCDLMB514.delta.rl.delta.com>
--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

It's not clear exactly what you are trying to achieve when you say "all ope=
ned files".

As others have said, you don't seem to understand what NetBeans Lookups are=
 or what they are used for so I would recommend reading up on Lookup.

If you understood Lookup, you would know that the global Lookup does not al=
low you to find all instances of a particular class type within your applic=
ation.

It's not really clear if you are trying to find all DataObject instances or=
 if you are trying to find opened files that are associated with "editor" T=
op Components.

Neil provided a link to a Wiki page http://wiki.netbeans.org/DevFaqGetOpenE=
ditorWindows that shows you how you can determine which editors are open fr=
om which you can get the associated DataObject.  If you are trying to find =
editor files - this is all you need.

That however would not find other DataObjects that were opened using the Fi=
leSystem/DataSystems API's directly e.g.

If you called DataObject dobj =3D DataObject.find(FileUtil.toFileObject(new=
 File("/path/to/somefile"));

When a DataObject instance is created it is registered in a DataObjectPool =
unfortunately this pool is not public.  You could however get the DataObjec=
ts registered in the pool using reflection.  Using a dirty hack like this r=
eally isn't recommended and you really need to understand what you are doin=
g.

This approach would return ALL DataObject instances and not just DataObject=
 instances that you opened (the platform opens many configuration files, ot=
her plugins could create DataObjects).  Determining which DataObjects you a=
re truly interested in would be another problem for you to solve. Manipulat=
ing objects you did not create/own could have serious consequences!

For demonstration purposes only:

            Field poolField =3D Class.forName("org.openide.loaders.DataObje=
ctPool").getDeclaredField("POOL");
            poolField.setAccessible(true);
            Object pool =3D poolField.get(null);
            Field mapField =3D pool.getClass().getDeclaredField("map");
            mapField.setAccessible(true);
            Object map =3D mapField.get(pool);
            Map<Object, Object> refMap =3D (Map<Object,Object>)map;
            for(Object o: refMap.values()) {
                Field objField =3D o.getClass().getDeclaredField("obj");
                objField.setAccessible(true);
                Reference<DataObject> result =3D (Reference<DataObject>)obj=
Field.get(o);
                DataObject dataObject =3D (DataObject)result.get();
                System.out.println(dataObject); // Here's the DataObject yo=
u want
            }

The above code would print all DataObject instances - you could return them=
 in a list, filter objects of interest etc.

Do you intend your modules to be used within the IDE or a custom NB Platfor=
m application where you might be able to customize your harness?  If the la=
ter you could provide the functionality you need without using reflection b=
y accessing DataObjectPool and providing a public method to return the info=
rmation you need.

Hopefully you are only interested in Editor files and Neil has already prov=
ided the perfect solution for that.

Using non-public API's is a really bad idea, but sometimes the only way to =
achieve what you want (I've seen reflection used within NB sources).

Just make sure you fully understand the ramifications.


________________________________
From: Peter Cheung [[email protected]]
Sent: Thursday, September 14, 2017 12:39 PM
To: [email protected]
Subject: [platform-dev] lookup confusing me

Hi
   I have trouble with lookup, the below code only print out the current fi=
le instead of all opened files.
Lookup lookup =3D Utilities.actionsGlobalContext();

Collection<? extends DataObject> list =3D lookup.lookupAll(DataObject.class=
);
for (DataObject dataObject : list) {
ModuleLib.log(dataObject);
}

If i use "Lookup.getDefault().lookupAll", looupAll() even returns nothing. =
Please help

Thanks
FromPeter


--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"">=0A=
<!--=0A=
p=0A=
	{margin-top:0;=0A=
	margin-bottom:0}=0A=
-->=0A=
</style><style type=3D"text/css" id=3D"owaParaStyle"></style>
</head>
<body dir=3D"ltr" fpstyle=3D"1" ocsi=3D"0">
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">
<div><span style=3D"font-size: 10pt;">It's not clear exactly what you are t=
rying to achieve when you say &quot;all opened files&quot;. &nbsp;</span></=
div>
<div><br>
</div>
<div>As others have said, you don't seem to understand what NetBeans Lookup=
s are or what they are used for so I would recommend reading up on Lookup.<=
/div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you understood Lookup, you would k=
now that the global Lookup does not allow you to find all instances of a pa=
rticular class type within your application.</span></div>
<div><br>
</div>
<div>It's not really clear if you are trying to find all DataObject instanc=
es or if you are trying to find opened files that are associated with &quot=
;editor&quot; Top Components.</div>
<div><br>
</div>
<div>Neil provided a link to a Wiki page&nbsp;<a href=3D"http://wiki.netbea=
ns.org/DevFaqGetOpenEditorWindows" target=3D"_blank" style=3D"font-size: 10=
pt; font-family: &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif;">http:=
//wiki.netbeans.org/DevFaqGetOpenEditorWindows</a><span style=3D"font-famil=
y: &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif; font-size: medium;">=
&nbsp;</span><span style=3D"font-size: 10pt;">that
 shows you how you can determine which editors are open from which you can =
get the associated DataObject. &nbsp;If you are trying to find editor files=
 - this is all you need.</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">That however would not find other Dat=
aObjects that were opened using the FileSystem/DataSystems API's directly e=
.g.&nbsp;</span><span style=3D"font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp=
; &nbsp; &nbsp;&nbsp;</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you called&nbsp;</span><span style=
=3D"font-size: 10pt;">DataObject dobj =3D DataObject.find(FileUtil.toFileOb=
ject(new File(&quot;/path/to/somefile&quot;));</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">When a DataObject instance is created=
 it is registered in a DataObjectPool unfortunately this pool is not public=
. &nbsp;You could however get the DataObjects registered in the pool using =
reflection. &nbsp;</span><font size=3D"2">Using
 a dirty hack like this really isn't recommended and you really need to und=
erstand what you are doing. &nbsp;&nbsp;</font></div>
<div><font size=3D"2"><br>
</font></div>
<div><font size=3D"2">This approach would return ALL DataObject instances a=
nd not just DataObject instances&nbsp;</font><span style=3D"font-size: 10pt=
;">that you opened (the platform opens many configuration files, other plug=
ins could create DataObjects). &nbsp;Determining
 which DataObjects you are truly interested in would be another problem for=
 you to solve. Manipulating objects you did not create/own could have serio=
us consequences!</span></div>
<div><br>
</div>
<div>For demonstration purposes only:</div>
<div><br>
</div>
<div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field poolField =3D Class.fo=
rName(&quot;org.openide.loaders.DataObjectPool&quot;).getDeclaredField(&quo=
t;POOL&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; poolField.setAccessible(true=
);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object pool =3D poolField.ge=
t(null);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field mapField =3D pool.getC=
lass().getDeclaredField(&quot;map&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapField.setAccessible(true)=
;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object map =3D mapField.get(=
pool); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map&lt;Object, Object&gt; re=
fMap =3D (Map&lt;Object,Object&gt;)map;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Object o: refMap.values(=
)) {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field objField=
 =3D o.getClass().getDeclaredField(&quot;obj&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objField.setAc=
cessible(true);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reference&lt;D=
ataObject&gt; result =3D (Reference&lt;DataObject&gt;)objField.get(o);</div=
>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataObject dat=
aObject =3D (DataObject)result.get();</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.pri=
ntln(dataObject); // Here's the DataObject you want</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div>
</div>
<div><br>
</div>
<div>The above code would print all DataObject instances - you could return=
 them in a list, filter objects of interest etc.</div>
<div><br>
</div>
<div>Do you intend your modules to be used within the IDE or a custom NB Pl=
atform application where you might be able to customize your harness? &nbsp=
;If the later you could provide the functionality you need without using re=
flection by accessing DataObjectPool
 and providing a public method to return the information you need.</div>
<div><br>
</div>
<div>Hopefully you are only interested in Editor files and Neil has already=
 provided the perfect solution for that. &nbsp;</div>
<div><br>
</div>
<div>Using non-public API's is a really bad idea, but sometimes the only wa=
y to achieve what you want&nbsp;<span style=3D"font-size: 10pt;">(I've seen=
 reflection used within NB sources)</span><span style=3D"font-size: 10pt;">=
. &nbsp;</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">Just make sure you fully understand t=
he ramifications.</span></div>
<div><br>
</div>
<div><br>
</div>
<div style=3D"font-family: Times New Roman; color: #000000; font-size: 16px=
">
<hr tabindex=3D"-1">
<div id=3D"divRpF972446" style=3D"direction: ltr;"><font face=3D"Tahoma" si=
ze=3D"2" color=3D"#000000"><b>From:</b> Peter Cheung [[email protected]=
]<br>
<b>Sent:</b> Thursday, September 14, 2017 12:39 PM<br>
<b>To:</b> [email protected]<br>
<b>Subject:</b> [platform-dev] lookup confusing me<br>
</font><br>
</div>
<div></div>
<div>
<div style=3D"font-family:Calibri,Helvetica,sans-serif; font-size:12pt; col=
or:rgb(0,0,0)">
<div>Hi</div>
<div>&nbsp; &nbsp;I have trouble with lookup, the below code only print out=
 the current file instead of all opened files.</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">Lookup lookup=
 =3D Utilities.actionsGlobalContext();</span></div>
<div><br>
</div>
<div>Collection&lt;? extends DataObject&gt; list =3D lookup.lookupAll(DataO=
bject.class);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>for (D=
ataObject dataObject : list) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(dataObject);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><br>
</div>
<div>If i use &quot;<span style=3D"color:rgb(0,0,0); font-family:Calibri,He=
lvetica,sans-serif; font-size:16px; font-style:normal">Lookup.getDefault().=
lookupAll&quot;, looupAll() even returns nothing. Please help</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal"><br>
</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal">Thanks</span></div>
<div>FromPeter</div>
<br>
</div>
</div>
</div>
</div>
</body>
</html>

--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_--