Sunday, April 29, 2007

How To.Hack the "internal" namespace

If you're looking for for information on mx_internal go see mx.core.mx_internal.

This isn't so much undocumented as it is a hack. I wouldn't do it, but it's still fun.

Actually this technique seems pretty obvious once it occurs to you. Since something under the internal namespace can only be accessed from within the same package, just put your own class with proxy functions in that package.

So the obvious question becomes, what if the the internal function you want to access is in an swc? If this is a case you can't just insert a class into it. But you can create mimic the package structure in your own project and put the class there. This will work fine.

So let's say you want to call form.invalidateLabelWidth() which happens to be marked internal. In your own project simply create the directory structure, mx.containers. In this new containers folder put create a class "FormInternalProxy", and then create the function:

public static function invalidateLabelWidth(form:Form):void {

It's that simple. I'm still looking into if something like this can be used to access private functions, but so far class naming conflicts seem to prevent it.


Brian said...

Exactly the trick I'm looking for! Thank you!

Nisse said...

Looking for a way to modify private variables runtime from outside the private scope?
- Only works in debugplayer.
- Crashes on read only variables (add try..catch to prevent)
- Crashes on int, boolean (non-object objects)

trace(getLocalVariableFromObject(myObject, "myPrivateVariable"));

(you can also use this to call private functions)

public static function getLocalVariableFromObject(object:Object, variableName:String):Object
var members:Array = getMemberNames(object,false);
for each (var member:QName in members)
if (member.localName == varaiableName)
return this[object];

return null;

"Flex", "ActionScript" and possibly "MXML" are probably trademarks of Adobe Systems Incorporated.
"Adobe" is a trademark of Adobe Systems Incorporated.
This site is in no way endorsed or sponsored by Adobe Systems Incorporated.
Content Copyright © 2007 Daniel Freiman.
Site Design Copyright by its copyright holder.
The Flex Non-Docs reserves the right to remove comments for any reason.
All ActionScript and MXML code (and ONLY ActionScript and MXML code) on this website is available under the MIT License.