Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 320253

Summary: [General] Generic API for stereotype application retrieval (static profiles)
Product: [Modeling] Papyrus Reporter: Yann Tanguy <yann.tanguy>
Component: CoreAssignee: Yann Tanguy <yann.tanguy>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: ansgar.radermacher
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Yann Tanguy CLA 2010-07-19 09:03:05 EDT
Hello,


in eC3M, I often retrieve the stereotype application from a given UML model element for the static profiles MARTE and FCM. I'd like to share
the code below with you. You might already use something similiar, but the small code is quite elegant (I think) and has the advantage that
- Due to the use of Java generics, it is type safe (no cast is required)
- There is no need for a stereotype name which is error prone (might be mispelled, might change in profile)

Ansgar


class StUtils {
...
    /**
     * Return the stereotype application by passing an element of the static profile
     * @param element an UML model element
     * @param clazz the class of an element of a static profile. Compatible sub-types will be returned as well
     * @return the stereotype application or null
     */
    @SuppressWarnings("unchecked")
    public static <T extends EObject> T getApplication (Element element, Class<T> clazz)
    {
        for (EObject stereoApplication : element.getStereotypeApplications ()) {
            // check whether the stereotype is an instance of the passed parameter clazz
            if (clazz.isInstance (stereoApplication)) {
                return (T) stereoApplication;
            }
        }
        return null;
    }
}

Sample use:
ClientServerPort = StUtils.getApplication (umlPort, ClientServerPort.class);
Comment 1 Yann Tanguy CLA 2010-07-19 09:05:14 EDT
Contrib. from Ansgar added in UMLUtils plugin r2407.