How to obtain a class's formal parameters?

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

How to obtain a class's formal parameters?

Postby thomas.beale@openehr.org » Fri Feb 22, 2019 6:51 am

Similar to my last query, I want to obtain the formal parameters of a templated class in a model, e.g. say I have a class 'Dictionary<V,K>', I want to obtain and cycle through the V, K list.

Understanding where anything is in the UML meta-model is always a challenge (it's a masterpiece of incomprehensibility, and I speak as someone who builds meta-models), but my best guess (based on the UML 2.5 spec, and sites like the OMG validator site http://validator.omg.org/se-interop/class-view?class=type*UML%202.5*TemplateParameter) is something like this:

Code: Select all
    com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class aClass = ... (passed in);

    String className = aClass.getName();

    // check for template parts
    TemplateSignature tplSig = aClass.getOwnedTemplateSignature();
    if (tplSig != null) {
           String tplParamsStr = tplSig.getOwnedParameter().stream()
                .map(t -> t.getDefault().getXXXX)  // WHAT TO USE HERE?
                .collect(Collectors.joining(","));
        className = className + '<' + tplParamsStr + '>';
    }


But what association as to be following to just obtain the name of the TemplateParameter in the marked line? Is there no way to just obtain the formal type name (not jst class name) of a class in one go?

Some examples of how to use the openAPI to do normal things like this would be very helpful.

thanks

- thomas
thomas.beale@openehr.org
Forum Newbie
Forum Newbie
 
Posts: 3
Posts Rating:0
Joined: Wed Oct 12, 2016 11:19 am

Re: How to obtain a class's formal parameters?

Postby thomas.beale@openehr.org » Fri Feb 22, 2019 7:32 am

A bit more experimentation which has partial success.

Code: Select all
        TemplateSignature tplSig = element.getOwnedTemplateSignature();
        if (tplSig != null) {
            // List<TemplateParameter>
            String tplParamsStr = tplSig.getOwnedParameter().stream()
                    .map(t -> t.getParameteredElement().getHumanName())
                    .collect(Collectors.joining(","));
            className = className + '<' + tplParamsStr + '>';
        }


This generates className values like 'Interval<class T>'. The function getHumanName() apparently generates the 'class T', but there is no getName() to just get the actual name. Painful.

- thomas
thomas.beale@openehr.org
Forum Newbie
Forum Newbie
 
Posts: 3
Posts Rating:0
Joined: Wed Oct 12, 2016 11:19 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 1 guest