Re: Access a private variable?
Re: Access a private variable?
- Subject: Re: Access a private variable?
- From: Alex Johnson <email@hidden>
- Date: Fri, 15 Jul 2005 10:54:13 -0500
Hi everyone,
I'm posting my solution here for the record. I ended up subclassing WODisplayGroup, and adding this code:
<x-tad-bigger> </x-tad-bigger><x-tad-bigger>private</x-tad-bigger><x-tad-bigger> </x-tad-bigger><x-tad-bigger>static</x-tad-bigger><x-tad-bigger> java.lang.reflect.Field _qualifiedObjectsAccessor;
</x-tad-bigger><x-tad-bigger>/**
* Gets the array of qualifiedObjects. That is, the subset of allObjects that
* was created in updateDisplayedObjects, by either the delegate or the display
* group (using its qualifier). Unlike displayedObjects(), this method is not
* affected by numberOfObjectsPerBatch.
*
* <b>Implementation Note:</b> This method is somewhat unclean. It uses reflection
* to break the encapsulation of WODisplayGroup's private _displayObjects field
* when numberOfObjectsPerBatch is non-zero. If that is not possible, due to the
* security policy, this method falls back on an implementation that temporarily
* sets numberOfObjectsPerBatch to 0, and then calls displayedObjects().
*/</x-tad-bigger><x-tad-bigger>
</x-tad-bigger><x-tad-bigger>public</x-tad-bigger><x-tad-bigger> NSArray qualifiedObjects() {
NSArray qualifiedObjects;
</x-tad-bigger><x-tad-bigger>if</x-tad-bigger><x-tad-bigger> ( numberOfObjectsPerBatch() == </x-tad-bigger><x-tad-bigger>0</x-tad-bigger><x-tad-bigger> ) {
</x-tad-bigger><x-tad-bigger>// If numberOfObjectsPerBatch is 0, then displayedObjects() is the array of qualified objects:</x-tad-bigger><x-tad-bigger>
qualifiedObjects = displayedObjects();
} </x-tad-bigger><x-tad-bigger>else</x-tad-bigger><x-tad-bigger> {
</x-tad-bigger><x-tad-bigger>try</x-tad-bigger><x-tad-bigger> {
</x-tad-bigger><x-tad-bigger>// Try to extract the field using reflection:</x-tad-bigger><x-tad-bigger>
</x-tad-bigger><x-tad-bigger>if</x-tad-bigger><x-tad-bigger> ( _qualifiedObjectsAccessor == </x-tad-bigger><x-tad-bigger>null</x-tad-bigger><x-tad-bigger> ) {
_qualifiedObjectsAccessor = WODisplayGroup.</x-tad-bigger><x-tad-bigger>"_displayedObjects"</x-tad-bigger><x-tad-bigger>);
_qualifiedObjectsAccessor.setAccessible( </x-tad-bigger><x-tad-bigger>true</x-tad-bigger><x-tad-bigger> );
}
qualifiedObjects = (NSArray)_qualifiedObjectsAccessor.get(</x-tad-bigger><x-tad-bigger>this</x-tad-bigger><x-tad-bigger>);
}
</x-tad-bigger><x-tad-bigger>catch</x-tad-bigger><x-tad-bigger>( SecurityException se ) {
NSLog.debug.appendln( se );
</x-tad-bigger><x-tad-bigger>// For whatever reason, the SecurityManager refused to let us access the field directly.</x-tad-bigger><x-tad-bigger>
</x-tad-bigger><x-tad-bigger>// Get the value by temporarily setting numberOfObjectsPerBatch to 0:</x-tad-bigger><x-tad-bigger>
</x-tad-bigger><x-tad-bigger>int</x-tad-bigger><x-tad-bigger> pp = numberOfObjectsPerBatch();
</x-tad-bigger><x-tad-bigger>int</x-tad-bigger><x-tad-bigger> pg = currentBatchIndex();
NSArray sel = selectedObjects();
setNumberOfObjectsPerBatch(</x-tad-bigger><x-tad-bigger>0</x-tad-bigger><x-tad-bigger>);
qualifiedObjects = displayedObjects();
setNumberOfObjectsPerBatch( pp );
setCurrentBatchIndex( pg );
setSelectedObjects( sel );
}
}
</x-tad-bigger><x-tad-bigger>return</x-tad-bigger><x-tad-bigger> qualifiedObjects;
}
</x-tad-bigger>
I'm fairly certain that the "fall-back" implementation covers all of the side-effects of altering the
<x-tad-bigger>numberOfObjectsPerBatch</x-tad-bigger> (namely,
<x-tad-bigger>currentBatchIndex</x-tad-bigger> and
<x-tad-bigger>selectedObjects</x-tad-bigger>). It's possible, though, that there is some other side-effect that I am not protecting against (hence my desire to access the field directly).
Thanks again to everyone who helped me out.
Alex Johnson
|
| |
|||
|| | |
||
| | |
|| || |
||
\\ SITE 9 \\ SITE9.COM \\ 312.670.8469 \\
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden