Article
· Nov 30, 2023 3m read

How to programmatically retrieve a list of user-created classes

InterSystems FAQ rubric

Class definitions created by users are stored in class definition classes. They can be used to obtain a list of class definitions from a program.

Note: Class definition classes refer to all classes contained in the %Dictionary package.

In the sample code below, a list of class definitions is obtained using the query Summary of the class %Dictionary.ClassDefinitionQuery.

Class ISJ.Utils
{
ClassMethod ClassInfo()
{
    #dim ex As %Exception.AbstractException
    try {
        set currentNS=$NAMESPACE
        while (1) {
            read "Please specify namespace: ",x
            if x'=""  quit
        }
        set $NAMESPACE=x 
        write !!
        Set statement = ##class(%SQL.Statement).%New()
        Do statement.%PrepareClassQuery("%Dictionary.ClassDefinitionQuery","Summary")
        set rs = statement.%Execute()
        while rs.%Next() {
            set name=rs.%Get("Name")
            if name["%" continue            // Skip the class with % in the name
            if $extract(name,1,3)="csp" continue  // skip csp.*
            if $extract(name,1,3)="csr" continue  // skip csr.*
            write name,!
        }
        set $NAMESPACE=currentNS
    }
    catch ex {
        write "Error occured: ",ex.DisplayString(),!
        set $NAMESPACE=$get(currentNS)
    }
}
}

An example of execution is as follows.

When you run the class method, you will be asked to specify a namespace, so please specify the namespace name you want to reference.

USER>do ##class(ISJ.Utils).ClassInfo()
Please specify namespace : USER

CSPX.Dashboard.BarChart
CSPX.Dashboard.Chart
CSPX.Dashboard.ChartSeries
CSPX.Dashboard.FuelGauge

<skip>

INFORMATION.SCHEMA.VIEWTABLEUSAGE
ISJ.Utils
Test.JSONTest
Test.Person
Test.REST
Test.VSCode.REST

USER>

Related article: Getting the list of routines programmatically

Discussion (2)0
Log in or sign up to continue