[metaas-dev] Changing Field names in a parsed AS3 source file
Dave Newton
dave at kitkat.f9.co.uk
Sat Mar 15 13:04:03 UTC 2008
David Holroyd wrote:
>> I've got to convert a lot of old code in lots of files to
>> as3, and i've been looking for a way to automate some of it, and metaas
>> looks ideal. I've been playing around with it, and tried various things
>> - i'm probably doing something really dumb! I can change the name of a
>> field, but when i try to save it, it never works. I've tried allsorts of
>> ways and still haven't found one that works - i'm not sure if it metaas,
>> java or most likely just me. Should the following work? (it writes the
>> new file out, but without the change).
>>
>> ActionScriptFactory fact = new ActionScriptFactory();
>> FileInputStream in = new FileInputStream("dungeon.as");
>> InputStreamReader reader = new InputStreamReader(in);
>> ActionScriptParser parser = fact.newParser();
>> ASCompilationUnit unit = parser.parse(reader);
>>
>> //look for variable tileColour
>> //ASPackage pkg = unit.getPackage();
>> ASClassType asclass = (ASClassType)unit.getType();
>> ASField field = asclass.getField("tileColour1");
>> System.out.println(field.getName());
>> field.setName("wallTileColour");
>> System.out.println(field.getName());
>>
>> //check to see if it's been updated
>> field =
>> ((ASClassType)unit.getType()).getField("wallTileColour");
>> System.out.println(field.getName());
>>
>> //ok, now add compilation unit to current project
>> ActionScriptProject proj = fact.newEmptyASProject(".");
>> proj.addCompilationUnit(unit);
>> proj.setOutputLocation("fixed\\");
>> proj.writeAll();
>>
>
> Looks like it should work, so probably metaas has a bug.
>
> Is the input actually an AS2 file, or has it already had the basic
> transformation to AS3 done?
>
>
Thanks for the reply, and the previous help with my email address! Silly
me!! The code should be AS3 already, probably some form of automatic
basic transform, then manual fixes to make compile.
>> I also checked out the cvs version and compiled that, but just kept give
>> me a "NoViableAltException antlr" error but it's probably me done
>> something wrong - are there many important updates in the cvs?
>>
>
> I think it might help to see the input file that you are trying to
> process. Can you send that too?
>
> You mentioned in another email that you've used antlrworks to compile
> the grammars included in the codebase. Did you try to use Maven to
> build the project? This is the preferred way :)
>
>
I figured out what the xml's were for the other day and it compiled
fine, and worked correctly so the cvs version is fixed :). I may have to
go higher up in metaas to achive what i want though - here's an example
lets say I have a class like this
public var CTile {
public var tile1:Tile1;
public var tile11:Tile11;
public function DrawTile1(tile1:Tile1):void {
public var tile1Name:String = tile1.name;
trace("Drawn Tile1");
}
public function DrawTile11(tile11:Tile11):void {
public var tile11Name:String = tile11.name;
trace("Drawn Tile11");
}
}
and i want to change "Tile1" to "WallTile" then i can't easily search
replace, but if i could access through a tokenized version like the
following
public var $0 {
public var $1:$2;
public var $3:$4;
public function $5($6:$2):void {
public var $7:String = $6.$8;
trace("Drawn Tile1");
}
public function $9($10:$4):void {
public var $11:String = $10.$12;
trace("Drawn Tile11");
}
}
then i could just replace all $2 tokens with "WallTile" in the generated
ouput. How hard do you think it would be to add the functionality to
metaas to change all the occurances of a token (variable, field, method,
class) in a project (multiple source files)? Or maybe theres an
alternative like just getting a list of places where the token is used,
or just a list of tokens and then i could walk through and modify the
script as necessary? I can't really see anything similar in the api -
and i've not delved deeper into metaas yet.
Anyway - thanks for all your help so far :)
Dave
More information about the metaas-dev
mailing list