Search code examples
refactoringsmalltalkpharosqueak

How to add an instance variable using Refactoring Browser?


I want to add an instance variable programatically to an existing class using Refactoring Browser:

| theClass className |
className := #MyClass.
theClass := (RBClass existingNamed: className)
                model: (RBNamespace new classNamed: className; yourself);
                yourself.
theClass addInstanceVariable: 'testIVar'

but the class is not modified with the new instance variable, what I am missing?


Solution

  • You forgot to execute your refactoring. Try this

    | model className theClass iVarName |
    className := #MyClass.
    iVarName := 'testIVar'.
    model := RBNamespace new classNamed: className; yourself.
    theClass := (RBClass existingNamed: className)
                    model: model;
                    yourself.
    (RBAddInstanceVariableRefactoring 
            model: model
            variable: iVarName
            class: theClass) execute.
    

    you may want to add authomatic accession methods (getter and setter) for your new instance variable

    (RBCreateAccessorsForVariableRefactoring 
            model: model
            variable: iVarName
            class: theClass 
            classVariable: false) execute