Identifying Unused Classes

Forums for specific tips, techniques and example code
User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Identifying Unused Classes

Postby andy-b » Mon Jul 05, 2010 2:13 pm

Hello

I am trying to create a script that will loop through all classes in a given schema and tell me if there are any references to it. Essentially this is the same functionality as right-clicking on a class in the Jade environment and selecting references. The purpose is to identify all classes that no longer have references and therefore MAY be obsolete.

I have looked for methods on the Schema and Class object but can only find methods that find references to methods that are in use and not the actual class.

At a basic level the script will be something like...

Code: Select all

allClasses := currentSchema.getAllClasses(false); foreach class in allClasses discreteLock do if class.someMethodThatCountsNumberOfReferencesThisClassHas = 0 then write class.name; endif; endforeach;
Any help will be appreciated
Thanks

User avatar
Jade Support
Posts: 103
Joined: Mon Aug 17, 2009 10:27 am
Location: Jade Software Corporation, Christchurch

Re: Identifying Unused Classes

Postby Jade Support » Mon Jul 05, 2010 5:32 pm

Hi Andy and welcome to the JADE forums.

Unfortunately there isn't a single method or property on the Class class that defines whether the receiver is 'referenced'. Classes can be referenced in all manner of ways, either in your application code, relational views, in an exported package, etc. There is an NFS (New Feature Suggestion) open against JADE to provide such a feature, but as yet it's not available.

One (inelegant) option might be to parse the .scm file of a full schema exact looking for exported package definitions, user code and other references to the classes. Any class that isn't found at least once could then be investigated further.

Regards,
Jade Support
Jade Software Corporation Ltd

Email: jadesupport@jadeworld.com
Web: http://www.jadeworld.com

Jade Software – complex business problems solved beautifully.

User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Re: Identifying Unused Classes

Postby andy-b » Tue Jul 06, 2010 10:09 am

Thanks for the speedy reply :D
One (inelegant) option might be to parse the .scm file of a full schema exact looking for exported package definitions, user code and other references to the classes.
Yeah I thought something like that was going to be my next option although my approach was slightly different. I notice that when you do a References search on a Class in the JADE environment it separates the results into Collections, References and Methods so my first thought was I could do the same and split my search into three parts - where non of the parts return any results then the Class could be flagged as possibly obsolete.

Starting for searching for references in Methods I found getMethods on Class which can return a collection of Methods and then for each of those I could search through the source for any text that matches a Classname which would be a crude attempt and trying to see if the Class was referenced in that Method. Of course I would have to look through every method and for every Class in every method which might take quite a while (even if I did optimise the search somehow).

Next I could focus on how to search for references to Classes that are References and then references to Classes that are in Collections (haven't thought that far ahead yet).

Your suggestion although you state is inelegant might be more elegant than my approach!

The JADE environment references command seems pretty quick - I don't suppose you know how that is implemented??

Thanks

Dennis
Posts: 10
Joined: Mon Aug 31, 2009 10:15 am
Location: New Plymouth

Re: Identifying Unused Classes

Postby Dennis » Wed Jul 07, 2010 10:14 am

There are some unpublished methods on the Type class that might be of use. I found them by
executing Type.inspectModal in a workspace and clicking the methods entry.

Assuming you have a new class called TestClass, try the following code before and after you have used it in
a method or class reference.

write TestClass.numCollClassRefs();
write TestClass.numForeignPropertyRefs();
write TestClass.numPropertyRefs();
write TestClass.numScriptRefs();

User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Re: Identifying Unused Classes

Postby andy-b » Wed Jul 07, 2010 11:54 am

Dennis you have just made my day!!! :D

Those are the exact methods I am looking for. I have had an initial play and they look to function pretty well, so will move on to build a proper Obsolete Class Annihilator :twisted: (well Annihilator might be going too far but sounds cooler).

Thanks very much for the help

Dennis
Posts: 10
Joined: Mon Aug 31, 2009 10:15 am
Location: New Plymouth

Re: Identifying Unused Classes

Postby Dennis » Wed Jul 07, 2010 11:57 am

No problem. I forgot to add you will need to check subclasses as well before you delete anything.

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

Re: Identifying Unused Classes

Postby BeeJay » Thu Jul 08, 2010 9:32 am

Don't forget that the classes could be being referenced indirectly via the various Schema::getClass methods using string literals and get/setProperty values that use string literals. Also where is the xref information stored for some of the Jade features that don't seem to be covered by those methods, based on the method names, such as but not limited to RPS mapping references, Package references, WebService Exposure references, etc.

What the supplied methods will help you to do is identify candidate classes that you can then visit in the IDE and do a references on to make sure they're truly not used. You would probably also want to follow that up with a Global Find on the class name across all your schemas to rule out some reprobate still using a String Literal rather than the :: MetaData notation to reference classes, including via "peer-schema" style lookups.

Cheers,
BeeJay.

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Identifying Unused Classes

Postby murray » Thu Jul 08, 2010 9:55 am

The Global search may still not find class names where the class names are being constructed algorithmically.
e.g.

Code: Select all

clas := currentSchema.getClass("S_" & ispec.initCaps);
In this case the classes are referenced and processed polymorphically as their common superclass, so there may not be any direct references. You'd just have to hope that these cases are found via the other means suggested above.
Murray (N.Z.)

User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Re: Identifying Unused Classes

Postby andy-b » Thu Jul 08, 2010 11:29 am

OK cool, duly noted that one script will not be the Magic Bullet for identifying 100% if a class is no longer used.
The intention is to be an initial step to get Classes 'on the radar' so to speak as possible candidates for deletion and each would need to be investigated further rather than just blatantly removed :oops:

Dennis
Posts: 10
Joined: Mon Aug 31, 2009 10:15 am
Location: New Plymouth

Re: Identifying Unused Classes

Postby Dennis » Thu Jul 08, 2010 11:49 am

Probably the best thing to do is add a Boolean-returning method onto the Class class so you can use some of the
unpublished properties - perhaps something like this;

Code: Select all

return numScriptRefs() > 0 or numCollClassRefs() > 0 or numPropertyRefs() > 0 or usedInWebService or not exposedClassRefs.isEmpty() or not schemaViewRefs.isEmpty() or not exportedClassRefs.isEmpty() or not rpsClassMaps.isEmpty();
And from there you will have to do a manual analysis of your system to see where Schema::getClass() or
create <object> as <class> is used. I have not tested "usedInWebService" or "not rpsClassMaps.isEmpty()"
so not sure if it will return the desired result.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 19 guests

cron