How does [RemoteClass] work in Flex Actionscript can I use it for custom data-binding?

15,354

Solution 1

[RemoteClass(alias="com.example.MyClass")] is a Flex shorthand for calling flash.net.registerClassAlias() :

public function registerClassAlias(aliasName:String, classObject:Class):void

To access those registered alias classes at runtime (to write a custom JSON data serialization framework) you can call:

getClassByAlias(aliasName:String):Class Looks up a class that previously had an alias registered through a call to the registerClassAlias() method.

For outgoing encoding from AS to Java you need to retrieve the aliased class name, you can do that by calling flash.utils.describeType() and use "reflection" on your Actionscript object's class to query attributes, properties, methods of the object.

For example the following code snippet for ObjectCodec.as seems to retrieve the alias attribute by using "@":

override protected function encodeComplex(o:Object, b:IBinary, context:IContext=null):void
{
        var desc:XML = describeType(o);
        var classAlias:String = desc.@alias;
        //...
}

Solution 2

[RemoteClass] is only used on the Flex side. All it really does is call the flash.net.registerClassAlias() function to setup a mapping between a local object and a remote class name.

Solution 3

You could use the -keep-generated-actionscript compiler argument to see what code is generated and how it works exactly.

Share:
15,354

Related videos on Youtube

Dougnukem
Author by

Dougnukem

Worked at some game companies and social game startups, working on a new web startup currently. Personal Website

Updated on June 04, 2022

Comments

  • Dougnukem
    Dougnukem almost 2 years

    Actionscript supports a [RemoteClass] metadata tag that is used in BlazeDS to provide data-binding hints for marshalling AMF binary objects from Java to BlazeDS.

    For example:

    Java: package sample;

    public class UserInfo
    {
        private String userName;
    
        public String getUserName()
        {
            return userName;
        }
    
        public void setUserName(String value)
        {
            userName = value;
        }
    }
    

    Actionscript:

    [Bindable]
    [RemoteClass(alias="sample.UserInfo")]
    public class UserInfo
    {
        public var userName:String=”";
    }
    

    How exactly is the [RemoteClass] implemented in the BlazeDS framework and could you override that behavior and provide a custom data-binding remoting framework (e.g. a JSON message passing system) that you could bind to the Actionscript [Bindable], [RemoteClass] class?

  • Dougnukem
    Dougnukem over 14 years
    So is there a way to access the flash.net.registerClassAlias() list. I'm trying to write my own custom remoting using JSON messages so I'm not using Flex's Producer/Consumer or IDataInput, IDataOutput interfaces. I need to be able to lookup a Flex class from a remote java class name and construct it.
  • supervacuo
    supervacuo almost 12 years
    Adobe's site no longer has information on the registerClassAlias method; there's a post on the Adobe forums disuccsing the missing documentation.
  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft almost 11 years