Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 320253 - [General] Generic API for stereotype application retrieval (static profiles)
Summary: [General] Generic API for stereotype application retrieval (static profiles)
Status: RESOLVED FIXED
Alias: None
Product: Papyrus
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Yann Tanguy CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-19 09:03 EDT by Yann Tanguy CLA
Modified: 2010-07-19 09:05 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.