How to create method by script?
hi
I want to create one class and add one method in it, and then invoke it from terminal. I find this
http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY…
But there is only examples for adding property.
Anyone can help me? Thanks
Comments
Do ##class(%Dictionary.ClassDefinition).%DeleteId("Test.Class")Set ClassDef=##class(%Dictionary.ClassDefinition).%New()Set ClassDef.Name="Test.Class"Set ClassDef.Super="%Persistent"Set ClassDef.ProcedureBlock=1Set MethDef=##class(%Dictionary.MethodDefinition).%New()Set MethDef.Name="Name"Set MethDef.ReturnType="%String"; Uncomment for ClassMethod; Set MethDef.ClassMethod=1Set MethDef.FormalSpec="First:%Boolean,Second:%String=""Default"""Do MethDef.Implementation.WriteLine($Char(9)_"If First Return 1")Do MethDef.Implementation.WriteLine($Char(9)_"Return Second")Set MethDef.parent=ClassDefDo ClassDef.Methods.Insert(MethDef)Do ClassDef.%Save()Thanks
Please note that $$$TAB macro is actually a whitespace:
ClassMethod GetTab() [ CodeMode = expression ]
{
$$$TAB
}
Call to GetTab returns:
USER>zzdump ##class(Utils.Persistent).GetTab() 0000: 20
I usually use either $c(9) or user-defined macro for tabulation.
#include %systemInclude
#include %occErrors
#include %occCPTJSgen
CreateClass() public {
#dim ex As %Exception.AbstractException
try {
$$$AddAllRoleTemporaryInTry
new $namespace
s className="demo.test"
d:##class(%Dictionary.ClassDefinition).%ExistsId(className) ##class(%Dictionary.ClassDefinition).%DeleteId(className)
s newClass=##class(%Dictionary.ClassDefinition).%New()
s newClass.Name=className
s newClass.ProcedureBlock=$$$YES
s newClass.Super="%RegisteredObject"
s newProp=##class(%Dictionary.PropertyDefinition).%New()
s newProp.parent=newClass
s newProp.Name="p1"
s newProp.Type="%String"
s newMethod=##class(%Dictionary.MethodDefinition).%New()
s newMethod.parent=newClass
s newMethod.Name="Main"
s newMethod.ReturnType="%Status"
s newMethod.FormalSpec=$$$FormatText("a:%String=%1",$$$quote("Hello, World!"))
d newMethod.Implementation.WriteLine($$$TAB_"w a,!")
d newMethod.Implementation.WriteLine($$$TAB_"q $$$OK")
$$$ThrowOnError(newClass.%Save())
d $system.OBJ.Compile(className,"cu/multicompile=1")
}catch(ex) {
w "Error ", ex.DisplayString(),!
}
}Result:Class demo.test Extends %RegisteredObject [ ProcedureBlock ]{
</FONT><FONT COLOR="#000080">Property </FONT><FONT COLOR="#000000">p1 </FONT><FONT COLOR="#000080">As %String</FONT><FONT COLOR="#000000">;
</FONT><FONT COLOR="#000080">Method </FONT><FONT COLOR="#000000">Main(</FONT><FONT COLOR="#ff00ff">a </FONT><FONT COLOR="#000080">As %String </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#800080">"Hello, World!"</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Status </FONT><FONT COLOR="#000000">{ </FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#800000">a</FONT><FONT COLOR="#000000">,! </FONT><FONT COLOR="#0000ff">q $$$OK </FONT><FONT COLOR="#000000">}
}</FONT>
Thanks, fixed the code. Now <FONT COLOR="#0000ff">$$$TAB </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(9)</FONT>
I didn't know that
#include %occCPTJSgen
Redefines $$$TAB. Seems useful. Thanks.