Rename class programmatically
How do I rename class programmatically?
This discussion touches on data move which, is not a concern in my case (BPL renames).
I also don't care about external references.
The best I got is:
- Get class text as string
- Replace class name in this string
- Save new string as new class
- Open new class as object
- Delete storage
- Compile class
Are there any better alternatives?
Also found this discussion.
Adapted from @Krishnamuthu Venkatachalam answer
ClassMethod Rename(oldClass, newClass) As %Status { #dim sc As %Status = $$$OK quit:'##class(%Dictionary.ClassDefinition).%ExistsId(oldClass) $$$ERROR($$$GeneralError, "Old class does not exist") quit:##class(%Dictionary.ClassDefinition).%ExistsId(newClass) $$$ERROR($$$GeneralError, "New class already exists") merge ^oddDEF(newClass) = ^oddDEF(oldClass) $$$defClassKeySet(newClass, $$$cCLASSname, newClass) // Set class name $$$defClassKeyKill(newClass, $$$cCLASSstorage) // Kill old storage do UpdClsDef^%occLibrary(newClass) set sc = $system.OBJ.Compile(newClass, "/displaylog=0 /displayerror=0") quit:$$$ISERR(sc) sc set sc = ##class(%Dictionary.ClassDefinition).%DeleteId(oldClass) quit sc }
This deserves Github+Open Exchange+ZPM ObjectScript library
Already in PythonGateway.
That's great. But I meant to have some general module as Math module @Peter Steiwer introduced.
I would somehow try to use solutions out of the box, for example %Compiler.COS.Refactor:ChangeClName().
In this class are many other useful methods for refactoring.