Re: lookup confusing me

Peter Cheung <[email protected]> Sat, 23 Sep 2017 18:01:37 +0000
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <ME1PR01MB0260B9B7595E2EDDF9C7BA6EB5640@ME1PR01MB0260.ausprd01.prod.outlook.com>
--_000_ME1PR01MB0260B9B7595E2EDDF9C7BA6EB5640ME1PR01MB0260ausp_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Super Thanks Ingleby for your detailed reply.


________________________________
From: Ingleby, Graeme <[email protected]>
Sent: Friday, September 15, 2017 1:39 PM
To: [email protected]
Subject: [platform-dev] Re: lookup confusing me

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.
DevFaqGetOpenEditorWindows - NetBeans Wiki<http://wiki.netbeans.org/DevFaqG=
etOpenEditorWindows>
wiki.netbeans.org
How can I get a list of open editor windows? To obtain a reference to the c=
urrently selected editor Node[] arr =3D TopComponent.getRegistry().getCurre=
ntNodes(); for ...



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_ME1PR01MB0260B9B7595E2EDDF9C7BA6EB5640ME1PR01MB0260ausp_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;font=
-family:Calibri,Helvetica,sans-serif;" dir=3D"ltr">
<p>Super Thanks Ingleby for your detailed reply.</p>
<br>
<br>
<div style=3D"color: rgb(0, 0, 0);">
<hr tabindex=3D"-1" style=3D"display:inline-block; width:98%">
<div id=3D"divRplyFwdMsg" dir=3D"ltr"><font face=3D"Calibri, sans-serif" co=
lor=3D"#000000" style=3D"font-size:11pt"><b>From:</b> Ingleby, Graeme &lt;G=
[email protected]&gt;<br>
<b>Sent:</b> Friday, September 15, 2017 1:39 PM<br>
<b>To:</b> [email protected]<br>
<b>Subject:</b> [platform-dev] Re: lookup confusing me</font>
<div>&nbsp;</div>
</div>
<div>
<div style=3D"direction:ltr; font-family:Tahoma; color:#000000; font-size:1=
0pt">
<div><span style=3D"font-size:10pt">It's not clear exactly what you are try=
ing to achieve when you say &quot;all opened files&quot;. &nbsp;</span></di=
v>
<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 kno=
w that the global Lookup does not allow you to find all instances of a part=
icular 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:10p=
t; font-family:&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif" id=3D"LPlnk=
392383" previewremoved=3D"true">http://wiki.netbeans.org/DevFaqGetOpenEdito=
rWindows</a><span style=3D"font-family:&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 id=3D"LPBorder_GT_15061896831870.7402896174103939" style=3D"margin-bot=
tom: 20px; overflow: auto; width: 100%; text-indent: 0px;">
<table id=3D"LPContainer_15061896831850.426470304963956" role=3D"presentati=
on" cellspacing=3D"0" style=3D"width: 90%; background-color: rgb(255, 255, =
255); position: relative; overflow: auto; padding-top: 20px; padding-bottom=
: 20px; margin-top: 20px; border-top: 1px dotted rgb(200, 200, 200); border=
-bottom: 1px dotted rgb(200, 200, 200);">
<tbody>
<tr valign=3D"top" style=3D"border-spacing: 0px;">
<td id=3D"TextCell_15061896831860.43632081890432795" colspan=3D"2" style=3D=
"vertical-align: top; position: relative; padding: 0px; display: table-cell=
;">
<div id=3D"LPRemovePreviewContainer_15061896831860.34146968595369565"></div=
>
<div id=3D"LPTitle_15061896831860.5818357083844887" style=3D"top: 0px; colo=
r: rgb(93, 178, 255); font-weight: normal; font-size: 21px; font-family: wf=
_segoe-ui_light, &quot;Segoe UI Light&quot;, &quot;Segoe WP Light&quot;, &q=
uot;Segoe UI&quot;, &quot;Segoe WP&quot;, Tahoma, Arial, sans-serif; line-h=
eight: 21px;">
<a id=3D"LPUrlAnchor_15061896831860.6024042233004838" href=3D"http://wiki.n=
etbeans.org/DevFaqGetOpenEditorWindows" target=3D"_blank" style=3D"text-dec=
oration: none;">DevFaqGetOpenEditorWindows - NetBeans Wiki</a></div>
<div id=3D"LPMetadata_15061896831860.11794091569879783" style=3D"margin: 10=
px 0px 16px; color: rgb(102, 102, 102); font-weight: normal; font-family: w=
f_segoe-ui_normal, &quot;Segoe UI&quot;, &quot;Segoe WP&quot;, Tahoma, Aria=
l, sans-serif; font-size: 14px; line-height: 14px;">
wiki.netbeans.org</div>
<div id=3D"LPDescription_15061896831870.1959443473246396" style=3D"display:=
 block; color: rgb(102, 102, 102); font-weight: normal; font-family: wf_seg=
oe-ui_normal, &quot;Segoe UI&quot;, &quot;Segoe WP&quot;, Tahoma, Arial, sa=
ns-serif; font-size: 14px; line-height: 20px; max-height: 100px; overflow: =
hidden;">
How can I get a list of open editor windows? To obtain a reference to the c=
urrently selected editor Node[] arr =3D TopComponent.getRegistry().getCurre=
ntNodes(); for ...</div>
</td>
</tr>
</tbody>
</table>
</div>
<br>
<div><span style=3D"font-size:10pt"><br>
</span></div>
<div><span style=3D"font-size:10pt">That however would not find other DataO=
bjects that were opened using the FileSystem/DataSystems API's directly e.g=
.&nbsp;</span><span style=3D"font-size:10pt">&nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; &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.toFileObje=
ct(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 i=
t is registered in a DataObjectPool unfortunately this pool is not public. =
&nbsp;You could however get the DataObjects registered in the pool using re=
flection. &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 plugin=
s 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 r=
eflection used within NB sources)</span><span style=3D"font-size:10pt">. &n=
bsp;</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 the=
 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" size=
=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>
</div>
</div>
</div>
</body>
</html>

--_000_ME1PR01MB0260B9B7595E2EDDF9C7BA6EB5640ME1PR01MB0260ausp_--