下面列出了怎么用org.eclipse.emf.ecore.EAnnotation的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void handleAnnotations(XModelElement xModelElement, EModelElement eModelElement) {
super.handleAnnotations(xModelElement, eModelElement);
// the following special handling only applies to EPackages
if (!(eModelElement instanceof EPackage)) {
return;
}
// obtain annotation that was created based on the automatic inference of copyright headers
Optional<EAnnotation> inferredAnnotations = eModelElement.getEAnnotations().stream()
.filter(a -> a.getSource().equals(GenModelPackage.eNS_URI))
// find annotation that is added automatically by inferring copyright header
.filter(a -> a.getDetails().containsKey("documentation") && a.getDetails().size() == 1)
.findFirst();
// delete @Ecore annotation as it is not present in an incremental build
Optional<EAnnotation> ecoreAnnotation = eModelElement.getEAnnotations().stream()
.filter(a -> a.getSource().equals(EcorePackage.eNS_URI))
.findFirst();
// removes annotation from container
inferredAnnotations.ifPresent(a -> eModelElement.getEAnnotations().remove(a));
ecoreAnnotation.ifPresent(a -> eModelElement.getEAnnotations().remove(a));
}
@Override
protected void handleAnnotations(XModelElement xModelElement, EModelElement eModelElement) {
super.handleAnnotations(xModelElement, eModelElement);
// the following special handling only applies to EPackages
if (!(eModelElement instanceof EPackage)) {
return;
}
// obtain annotation that was created based on the automatic inference of copyright headers
Optional<EAnnotation> inferredAnnotations = eModelElement.getEAnnotations().stream()
.filter(a -> a.getSource().equals(GenModelPackage.eNS_URI))
// find annotation that is added automatically by inferring copyright header
.filter(a -> a.getDetails().containsKey("documentation") && a.getDetails().size() == 1)
.findFirst();
// removes annotation from container
inferredAnnotations.ifPresent(a -> eModelElement.getEAnnotations().remove(a));
}
/**
* Finds the annotation even on supertypes
*
* @param target
* the object to check
* @return the annotation or <code>null</code> the object or its supertypes
* don't have the annotation
*/
public static EAnnotation findAnnotation ( final EObject target )
{
EAnnotation annotation;
annotation = target.eClass ().getEAnnotation ( SOURCE_NAME );
if ( annotation != null )
{
logger.debug ( "Found direct annotation - target: {}, annotation: {}", target, annotation );
return annotation;
}
for ( final EClass clazz : target.eClass ().getEAllSuperTypes () )
{
logger.debug ( "Checking supertype: {}", clazz );
annotation = clazz.getEAnnotation ( SOURCE_NAME );
if ( annotation != null )
{
logger.debug ( "Found annotation - target: {}, superclass: {}, annotation: {}", target, clazz, annotation );
return annotation;
}
}
logger.debug ( "Annotation on {} not found", target );
return null;
}
/**
* Make a set of group ids present in the object collection
*
* @param objects
* the objects to check
* @return the group set, never <code>null</code>
*/
public static Set<String> makeGroupIds ( final Collection<? extends EObject> objects )
{
final Set<String> result = new HashSet<> ();
for ( final EObject obj : objects )
{
final EAnnotation annotation = findAnnotation ( obj );
if ( annotation == null )
{
continue;
}
final String groupId = annotation.getDetails ().get ( VALUE_GROUP_ID );
if ( groupId == null )
{
continue;
}
result.add ( groupId );
}
return result;
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
return getGEFWrapper(cmd.reduce());
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyChildNodesCommand(cmd);
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
return getGEFWrapper(cmd.reduce());
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
return getGEFWrapper(cmd.reduce());
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
return getGEFWrapper(cmd.reduce());
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
return getGEFWrapper(cmd.reduce());
}
public static void setTreeNodesPositionAnnotation(List<View> viewElements) {
if (viewElements != null) {
for (int index = 0; index < viewElements.size(); index++) {
final View view = viewElements.get(index);
EAnnotation xmiIdAnnotation = view
.getEAnnotation(TREE_LAYOUT_ANNOTATION);
if (xmiIdAnnotation == null) {
xmiIdAnnotation = EcoreFactory.eINSTANCE
.createEAnnotation();
xmiIdAnnotation.setSource(TREE_LAYOUT_ANNOTATION);
}
xmiIdAnnotation.getDetails().put(TREE_NODE_POSITION,
Integer.toString(index));
xmiIdAnnotation.setEModelElement(view);
}
}
}
public String getAttributeName(EStructuralFeature feature,
Class<? extends EntityWithId> entityClazz){
String ret = feature.getName();
EAnnotation mappingAnnotation =
feature.getEAnnotation(IModelService.EANNOTATION_ENTITY_ATTRIBUTE_MAPPING);
if (mappingAnnotation != null) {
// test class specific first
ret = mappingAnnotation.getDetails().get(entityClazz.getSimpleName() + "#"
+ IModelService.EANNOTATION_ENTITY_ATTRIBUTE_MAPPING_NAME);
if (ret == null) {
// fallback to direct mapping
ret = mappingAnnotation.getDetails()
.get(IModelService.EANNOTATION_ENTITY_ATTRIBUTE_MAPPING_NAME);
}
}
return ret;
}
/**
* Remove exclusive groups from a collection of objects
*
* @param objects
* the objects to process
* @param groupIds
* the groups to remove
*/
public static void removeGroups ( final Collection<? extends EObject> objects, final Set<String> groupIds )
{
if ( groupIds == null || groupIds.isEmpty () )
{
return;
}
for ( final Iterator<? extends EObject> i = objects.iterator (); i.hasNext (); )
{
final EObject obj = i.next ();
final EAnnotation annotation = findAnnotation ( obj );
if ( annotation == null )
{
continue;
}
final String groupId = annotation.getDetails ().get ( VALUE_GROUP_ID );
if ( groupId == null )
{
continue;
}
if ( groupIds.contains ( groupId ) )
{
i.remove ();
}
}
}
/**
* Group objects by groupId
* <p>
* Note that object without are group are not returned
* </p>
*
* @param objects
* the object to group
* @return the grouped objects
*/
public static Map<String, Set<EObject>> aggregateGroups ( final EList<? extends EObject> objects )
{
final Map<String, Set<EObject>> map = new HashMap<> ();
for ( final EObject obj : objects )
{
final EAnnotation annotation = findAnnotation ( obj );
if ( annotation == null )
{
continue;
}
final String groupId = annotation.getDetails ().get ( VALUE_GROUP_ID );
if ( groupId == null )
{
continue;
}
Set<EObject> set = map.get ( groupId );
if ( set == null )
{
set = new HashSet<> ();
map.put ( groupId, set );
}
set.add ( obj );
}
return map;
}
private NodeElementProcessor createProcessor ( final EObject element, final World world, final ApplicationNode applicationNode ) throws CoreException
{
final EAnnotation an = element.eClass ().getEAnnotation ( "http://eclipse.org/SCADA/Configuration/World" );
if ( an != null && Boolean.parseBoolean ( an.getDetails ().get ( "ignore" ) ) )
{
return new NodeElementProcessor () {
@Override
public void process ( final String phase, final IFolder baseDir, final IProgressMonitor monitor, final Map<String, String> properties ) throws Exception
{
// no-op
}
};
}
for ( final IConfigurationElement ele : Platform.getExtensionRegistry ().getConfigurationElementsFor ( Activator.EXTP_GENERATOR ) )
{
if ( !ele.getName ().equals ( ELE_NODE_ELEMENT_PROCESSOR ) )
{
continue;
}
if ( isMatch ( Activator.getDefault ().getBundle ().getBundleContext (), ele, element ) )
{
final NodeElementProcessorFactory factory = (NodeElementProcessorFactory)ele.createExecutableExtension ( "factoryClass" );
return factory.createProcessor ( element, world, applicationNode );
}
}
throw new IllegalStateException ( String.format ( "No processor found for element: %s", element ) );
}
/**
* @generated
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
for (Iterator it = myDescriptors.iterator(); it.hasNext();) {
CreateViewRequest.ViewDescriptor nextDescriptor = (CreateViewRequest.ViewDescriptor) it.next();
View view = (View) nextDescriptor.getAdapter(View.class);
if (view != null && view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
EAnnotation shortcutAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
shortcutAnnotation.setSource("Shortcut"); //$NON-NLS-1$
shortcutAnnotation.getDetails().put("modelID", WorkflowEditPart.MODEL_ID); //$NON-NLS-1$
view.getEAnnotations().add(shortcutAnnotation);
}
}
return CommandResult.newOKCommandResult();
}
/**
* @generated
*/
public boolean test(Object receiver, String method, Object[] args, Object expectedValue) {
if (false == receiver instanceof View) {
return false;
}
View view = (View) receiver;
if (SHORTCUT_PROPERTY.equals(method)) {
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation != null) {
return WorkflowEditPart.MODEL_ID.equals(annotation.getDetails().get("modelID")); //$NON-NLS-1$
}
}
return false;
}
/**
* @generated
*/
private void stampShortcut(View containerView, Node target) {
if (!WorkflowEditPart.MODEL_ID.equals(CrossflowVisualIDRegistry.getModelID(containerView))) {
EAnnotation shortcutAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
shortcutAnnotation.setSource("Shortcut"); //$NON-NLS-1$
shortcutAnnotation.getDetails().put("modelID", WorkflowEditPart.MODEL_ID); //$NON-NLS-1$
target.getEAnnotations().add(shortcutAnnotation);
}
}
/**
* @generated
*/
public void createDecorators(IDecoratorTarget decoratorTarget) {
View view = (View) decoratorTarget.getAdapter(View.class);
if (view != null) {
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation != null) {
decoratorTarget.installDecorator(SHORTCUTS_DECORATOR_ID, new ShortcutsDecorator(decoratorTarget));
}
}
}
/**
* Returns the documentation of an element.
*
* @param element
* @return
*/
String documentation(EPackage element) {
for (EAnnotation annotation : element.getEAnnotations()) {
if (annotation.getSource().equals(ECORE_SOURCE)) {
return annotation.getDetails().get(DOC_KEY);
}
}
return "";
}
/**
* Returns the constraints of an EClass.
*
* @param element
* @return
*/
String documentation(EClass eClass) {
for (EAnnotation annotation : eClass.getEAnnotations()) {
if (annotation.getSource().equals(ECORE_SOURCE)) {
return annotation.getDetails().get(CONSTRAINT_KEY);
}
}
return "";
}
protected EAnnotation getMeasurementUnitAnnotation(List views) {
View firstView = (View) views.get(0);
Diagram dgrm = firstView.getDiagram();
EAnnotation measureUnitAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
measureUnitAnnotation.setSource(dgrm.getMeasurementUnit().getName());
return measureUnitAnnotation;
}
/**
* Returns the parent view of a tree node from the parent view annotation.
*
* @param editPart
* @return
*/
public static View getTreeNodeParentView(View view) {
final EAnnotation xmiIdAnnotation = view
.getEAnnotation(TREE_LAYOUT_ANNOTATION);
if (xmiIdAnnotation != null) {
return (View) view.eResource().getEObject(
xmiIdAnnotation.getDetails().get(TREE_NODE_PARENT_URI));
}
return null;
}
/**
* Returns the index of a tree node from the view position annotation.
*
* @param editPart
* @return
*/
public static int getTreeNodePosition(IGraphicalEditPart editPart) {
final EAnnotation xmiIdAnnotation = editPart.getNotationView()
.getEAnnotation(TREE_LAYOUT_ANNOTATION);
if (xmiIdAnnotation != null) {
final String pos = xmiIdAnnotation.getDetails().get(
TREE_NODE_POSITION);
if (pos != null) {
return Integer.parseInt(pos);
}
}
return getConstrainedTreeNodePosition(editPart.getFigure());
}
public static void setTreeNodeParentAnnotation(View child, View parent) {
EAnnotation xmiIdAnnotation = child
.getEAnnotation(TREE_LAYOUT_ANNOTATION);
if (xmiIdAnnotation == null) {
xmiIdAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
xmiIdAnnotation.setSource(TREE_LAYOUT_ANNOTATION);
}
xmiIdAnnotation.getDetails().put(TREE_NODE_PARENT_URI,
parent.eResource().getURIFragment(parent));
xmiIdAnnotation.setEModelElement(child);
}
public static void markGenerated(Package package_) {
if (MDDUtil.isGenerated(package_))
return;
final Package root = getRootPackage(package_);
EAnnotation annotation = root.createEAnnotation(MDDUtil.GENERATED);
EMap<String, String> details = annotation.getDetails();
details.put("dateCreated", new SimpleDateFormat("yyyy/MM/dd hh:mm:ss SSS Z").format(new Date()));
details.put("version", getMetamodelVersion());
}
public static String getGeneratedTimestamp(Package package_) {
final Package root = getRootPackage(package_);
EAnnotation eAnnotation = root.getEAnnotation(MDDUtil.GENERATED);
if (eAnnotation == null)
return null;
return eAnnotation.getDetails().get("dateCreated");
}
public void testSourceInfo() throws CoreException {
String source = "";
source += "model someModel;\n";
source += "import base;\n";
source += "class SomeClassifier\n";
source += "end;\n";
source += "end.";
parseAndCheck(source);
Classifier classifier = (Classifier) getRepository().findNamedElement("someModel::SomeClassifier",
Literals.CLASS, null);
EAnnotation unitInfo = classifier.getEAnnotation(MDDUtil.UNIT);
assertNotNull(unitInfo);
assertNotNull(unitInfo.getDetails());
assertEquals("foo0." + fixtureHelper.getExtension(), unitInfo.getDetails().get("name"));
}
public void addIndex(EStructuralFeature eStructuralFeature) {
for (EAnnotation eAnnotation : eStructuralFeature.getEAnnotations()) {
if (eAnnotation.getSource().equals("singleindex")) {
throw new RuntimeException(eStructuralFeature + " already has an index");
}
}
changes.add(new AddIndexChange(this, eStructuralFeature));
EAnnotation index = EcoreFactory.eINSTANCE.createEAnnotation();
index.setSource("singleindex");
eStructuralFeature.getEAnnotations().add(index);
}
/**
* Validate OCL constraints and return true if OCL constraint holds, false
* otherwise.
*/
private boolean validate_EveryInvariant(Instance instance,
DiagnosticChain diagnostics, Map<Object, Object> context) {
final EClass eClass = instance.getEClass();
final EAnnotation annotation = eClass.getEAnnotation(OCL_SOURCE_URI);
if (annotation != null) {
for (final Entry<String, String> entry : annotation.getDetails()) {
try {
final String expression = entry.getValue();
if (!(Boolean) evaluate(instance, expression)) {
if (diagnostics != null) {
diagnostics.add(new BasicDiagnostic(
Diagnostic.ERROR, DIAGNOSTIC_SOURCE, 0,
EcorePlugin.INSTANCE.getString(
"_UI_GenericConstraint_diagnostic", //$NON-NLS-1$
new Object[] {
entry.getKey(),
getObjectLabel(
(EObject) instance,
context) }),
new Object[] { instance }));
}
return false;
}
} catch (final ParserException e) {
System.out.println(e);
}
}
}
return true;
}
/**
* @generated
*/
protected Command getDestroyElementCommand(DestroyElementRequest req) {
View view = (View) getHost().getModel();
CompositeTransactionalCommand cmd = new CompositeTransactionalCommand(getEditingDomain(), null);
cmd.setTransactionNestingEnabled(false);
for (Iterator<?> it = view.getTargetEdges().iterator(); it.hasNext();) {
Edge incomingLink = (Edge) it.next();
if (ProcessVisualIDRegistry.getVisualID(incomingLink) == TextAnnotationAttachmentEditPart.VISUAL_ID) {
DestroyElementRequest r = new DestroyElementRequest(incomingLink.getElement(), false);
cmd.add(new DestroyElementCommand(r));
cmd.add(new DeleteCommand(getEditingDomain(), incomingLink));
continue;
}
}
EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
if (annotation == null) {
// there are indirectly referenced children, need extra commands: false
addDestroyChildNodesCommand(cmd);
addDestroyShortcutsCommand(cmd, view);
// delete host element
cmd.add(new DestroyElementCommand(req));
} else {
cmd.add(new DeleteCommand(getEditingDomain(), view));
}
final EObject pool = req.getElementToDestroy();
if (pool instanceof Pool) {
for (MessageFlow f : ModelHelper.getMainProcess(pool).getMessageConnections()) {
if (pool.equals(ModelHelper.getParentProcess(f.getSource()))) {
cmd.add(new DestroyElementCommand(new DestroyElementRequest(f, false)));
}
}
}
return getGEFWrapper(cmd.reduce());
}