Question
· May 3, 2023

Best Way to Split Class into Multiple Classes

Hello, I need to determine the best way to split a class containing multiple methods(about 20), into multiple classes in which each method is in its own class. I am wondering if there is another way other than manually creating the different classes, and copying each method into each class. Doing this in Intersystems Studio.

Product version: IRIS 2021.1
Discussion (10)5
Log in or sign up to continue

For a handful of methods the manual method is likely the fastest... in case, you have a real big bunch of methods to copy put this "short" method into a class and let it run...

/// Transfer all or selected methods from a class into
/// individual classes
/// cls : the donor class
/// list: list of methods to be transfered: "method1,method2,..."
///       or empty to transfer all methods
ClassMethod Transfer(cls, list = "")
{
    s old=##class(%Dictionary.ClassDefinition).%OpenId(cls)
    s:list]"" list=$lfs(list)
    
    i old {
        f i=old.Methods.Count():-1:1 {
            s met=old.Methods.GetAt(i)				// grab the next method
            i list]"",'$lf(list,met.Name) continue	// skip if not to copy
            
            s new=old.%ConstructClone()				// duplicate old class
            s tmp=old.Name							// grab the old classname
            s $p(tmp,".",*)="Parts."_met.Name		// create a new classname
            s new.Name=tmp
            s new.Abstract=1						// make the class abstract
            s new.Super=""							// remove all superclasses
            
            s dup=met.%ConstructClone()				// duplicate the old method
            s dup.Name=met.Name						// but keep the old name
            d new.Properties.Clear()					// remove all properties
            d new.Parameters.Clear()				// remove all parameters			
            d new.Methods.Clear()					// remove all methods
            d new.Methods.Insert(dup)				// insert the copied method only
            d old.Methods.RemoveAt(i)				// Remove this method from old class
            i old.Super="" { s d="" } else { s d="," }
            s old.Super=old.Super_d_new.Name		// add the new class to extends-list
            s st=new.%Save()						// save the new class
            
            w new.Name,"-->",$s(st:"OK",1:$system.Status.GetOneErrorText(st)),!
            // Possibly compile the class: do $system.OBJ.Compile(...)
            
        }
        s st=old.%Save()							// save the old class
        w old.Name,"-->",$s(st:"OK",1:$system.Status.GetOneErrorText(st)),!
        // Possibly compile...
    }