Autmatically renaming methods

Forums for specific tips, techniques and example code
allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Autmatically renaming methods

Postby allistar » Thu Oct 07, 2010 12:15 pm

I've developed a source code parser (in Jade) that takes a schema (or class, or method) and parses it into an abstract syntax tree which you can then manipulate. This can then be output as a .scm file and reimported. We use it to do bulk changes to code that would otherwise take too many man-months to do by hand. (It also makes simple peer reviews easier, such as checking "are all iterators deleted in the epilog"). The only problem at the moment is that it gobbles up comments, which I'm currently working on fixing. This would achieve what you want but unfortunately it's not mine to give away. It goes to show that this in indeed possible, and the primary mechanism is to do schema export/import.

If the method names are unique enough, could you use standard shell tools like grep, sed and awk on the scm file?

Lorcan
Posts: 12
Joined: Thu Oct 07, 2010 8:51 am

Re: Autmatically renaming methods

Postby Lorcan » Thu Oct 07, 2010 2:11 pm

Thanks allistar,

Yeah, I know there are ways of shoe-horning it in.... and that is maybe what we will do if it is decided that the effort is worthwhile. Again, as in my previous post, my only worry would be in maintaining the integrity of the meta-data if we go using undocumented methods....or if we go down the search-and-replace avenue we may either miss something or replace something that doesnt need replacing....

Thanks for all your help and hope you are enjoying the emailed virtual-chocolate bar :)

Lorcan

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Autmatically renaming methods

Postby BeeJay » Thu Oct 07, 2010 2:35 pm

I'll bite. Why wouldn't you just rename them in your development environment, where you do have IDE access, and then simply include these "method rename" changes, and associated referencing methods, in your next release/deployment to your production system?

Cheers,
BeeJay.

Lorcan
Posts: 12
Joined: Thu Oct 07, 2010 8:51 am

Re: Autmatically renaming methods

Postby Lorcan » Thu Oct 07, 2010 2:45 pm

Hey, Beej and welcome.... hope all fine and dandy in your world..... :)

We are very (very!) careful about testing every released method....so...

a) we cannot get near the IDE for the production environment so cannot rename there.
b) if we change in the dev environment and release the affected calling methods then it is not failsafe that the code is not changed - the dev environment is after all dev and not a copy of prod - and our procedures would mean we would ahve to business test all areas that these - possibly different - methods touch and that is a no-no.....

Can you tell me why there isn't (or maybe there is and it's top-secret-keep-away-from-the-plebs :) ) a method called Method::renameMyselfAndCompileAllTheCallingMethds() or Class::renameMethod (OldName, NewName). After all, the IDE does it?

Another virtual chocolate bar on offer :)

Lorcan

Lorcan
Posts: 12
Joined: Thu Oct 07, 2010 8:51 am

Re: Autmatically renaming methods

Postby Lorcan » Thu Oct 07, 2010 2:46 pm

Damn - too many smileys - must be more serious

alanvl
Posts: 29
Joined: Fri Aug 14, 2009 9:16 pm

Re: Autmatically renaming methods

Postby alanvl » Thu Oct 07, 2010 9:04 pm

HI Lorcan

I'll have a stab as well (anything for the promise of a chocolate bar :) ) - try a vesrion the following 2 methods (apologies for my naming stds) defined on the JadeMethod class - haven't tried for anything complex but seems to be ok ...

Code: Select all

setMyName(pName: String):String updating; vars lSource : String; lUsage : FeatureUsage; lJadeMethod : JadeMethod; lScript : Script; lUsedIn : ObjectArray; lObj : Object; begin if pName = name or sourceMissing = true then return 'cannot rename - no source'; endif; // change calling methods create lUsedIn transient; foreach lUsage in scriptRefs do lScript := lUsage.getSchemaScript(); if lScript.isKindOf(JadeMethod) = false then write 'Error cannot change script:' & lScript._fullyQualifiedNameAndOid; continue; endif; lJadeMethod := lScript.JadeMethod; if lJadeMethod.renameCalledMethod(name, pName, lUsage.getPropertyValue('textPosition').Integer) = false then write 'Error cannot change method:' & lJadeMethod.qualifiedName; elseif lUsedIn.includes(lJadeMethod) = false then lUsedIn.add(lJadeMethod); endif; endforeach; // update current source lSource := source; lSource := pName & lSource[name.length+1: end]; name := pName; source := lSource; _compile; // recompile changed calling methods foreach lObj in lUsedIn do lJadeMethod := lObj.JadeMethod; lJadeMethod._compile; endforeach; epilog delete lUsedIn; end;

Code: Select all

renameCalledMethod( pOldName : String; pNewName : String; pPos : Integer ) : Boolean updating; vars lSource : String; lPos : Integer; begin if source.length < pPos+pOldName.length then return false; endif; lSource := source; lPos := lSource.pos(pOldName, pPos); lSource := lSource[1:lPos-1] & pNewName & lSource[lPos+pOldName.length:end]; source := lSource; return true; epilog end;

Lorcan
Posts: 12
Joined: Thu Oct 07, 2010 8:51 am

Re: Autmatically renaming methods

Postby Lorcan » Fri Oct 08, 2010 8:23 am

Alan - good to hear from you mate.....

Seems like you win the coveted chocolate bar.... want it emailed or fax'd?

This is the solution that I (although it works fine) was hoping I could avoid..... it is very elegent and easier than I had thought (i wouldn't have known about the collection of calling methods).... but can we be certain that it does everything that the JADE IDE rename does? is there something we are missing? I am pretty sure this is all ok but I wouldnt like to sign up to this being sufficient to use in a production environment....

But many thanks ... where are you now? - always up for a beer and a catch-up.....

Lorcan

craigp
Posts: 1
Joined: Mon Oct 11, 2010 7:51 am

Re: Autmatically renaming methods

Postby craigp » Mon Oct 11, 2010 7:55 am

Hi Lorcan
Just one question, call me nosy ;) , but why don't or can't you use a JCF?

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Autmatically renaming methods

Postby BeeJay » Mon Oct 11, 2010 8:14 am

b) if we change in the dev environment and release the affected calling methods then it is not failsafe that the code is not changed - the dev environment is after all dev and not a copy of prod - and our procedures would mean we would ahve to business test all areas that these - possibly different - methods touch and that is a no-no.....
Lorcan
Do you not keep a "current release source" environment which you use for creating any "HotFixes" for your production environment, in which you can guarantee the only changes are those made for a "HotFix"? You could then do your rename in both your "current release source" environment and your main development environment but deploy the change to production using your “current release source” environment, in which you’re guaranteed there is only the method rename changes in the associated calling methods. Another possible alternative is to do the rename in your current development source, but in production deploy the "new named" methods and a new version of the old method changed to a one-liner that simply calls the new method. Eventually, the old method will get deleted when you do your next full schema release. Alternatively you could just wait for your next full release and include the method renames there and just live with the old names for now in production. (Or do you never do full schema releases of your source?)
Can you tell me why there isn't (or maybe there is and it's top-secret-keep-away-from-the-plebs :) ) a method called Method::renameMyselfAndCompileAllTheCallingMethds() or Class::renameMethod (OldName, NewName). After all, the IDE does it?
Lorcan
Sorry, I'm not in the JADE Plant so can't really answer this question. ;)

Cheers,
BeeJay.

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Autmatically renaming methods

Postby BeeJay » Mon Oct 11, 2010 8:16 am

Hi Lorcan
Just one question, call me nosy ;) , but why don't or can't you use a JCF?
Becase there is no rename method construct available to use in a JCF file.

Cheers,
BeeJay.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 22 guests

cron