From 2a57d574039b38e83849c466e717273976179144 Mon Sep 17 00:00:00 2001 From: Shuai Li Date: Mon, 18 Jan 2016 17:38:08 +0100 Subject: Bug 486047 - [Codegen C++] Shared aggregation pointer - When computing types to include, we skip shared aggregation attribute types - When computing types to declare, we add shared aggregation attribute types (unless they are already included, this hasn't changed) - Fix double pointer when an attribute is of shared aggregation kind, and it is also stereotyped <> Change-Id: I580c25ee304861498522b437128f1329208ffe4d Signed-off-by: Shuai Li --- .../papyrus/cpp/codegen/utils/ClassUtils.java | 44 ++++++------ .../papyrus/cpp/codegen/utils/Modifier.java | 13 ++-- .../org/eclipse/papyrus/codegen/base/GenUtils.java | 78 ++++++++++++++++++++-- 3 files changed, 104 insertions(+), 31 deletions(-) (limited to 'extraplugins') diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/ClassUtils.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/ClassUtils.java index 6450635c0f6..8a37ce5cc46 100644 --- a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/ClassUtils.java +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/ClassUtils.java @@ -11,6 +11,7 @@ package org.eclipse.papyrus.cpp.codegen.utils; +import java.util.Iterator; import java.util.List; import org.eclipse.emf.common.util.BasicEList; @@ -22,6 +23,7 @@ import org.eclipse.papyrus.C_Cpp.NoCodeGen; import org.eclipse.papyrus.C_Cpp.Ptr; import org.eclipse.papyrus.C_Cpp.Ref; import org.eclipse.papyrus.codegen.base.GenUtils; +import org.eclipse.uml2.uml.AggregationKind; import org.eclipse.uml2.uml.Classifier; import org.eclipse.uml2.uml.Element; import org.eclipse.uml2.uml.Enumeration; @@ -29,6 +31,8 @@ import org.eclipse.uml2.uml.Interface; import org.eclipse.uml2.uml.Operation; import org.eclipse.uml2.uml.Package; import org.eclipse.uml2.uml.PrimitiveType; +import org.eclipse.uml2.uml.Property; +import org.eclipse.uml2.uml.Type; /** * A set of utility functions related to classes. @@ -96,7 +100,7 @@ public class ClassUtils { noCodeGenStereotypes.add(NoCodeGen.class); // class attributes dependencies (non-ptr and non-ref) - usedClasses.addAll(GenUtils.getTypesViaAttributes(currentClass, ptrRefStereotypes, null, true)); + usedClasses.addAll(GenUtils.getTypesViaAttributes(currentClass, ptrRefStereotypes, null, true, true)); addEnumerationsAndPrimitiveTypes(usedClasses, GenUtils.getTypesViaAttributes(currentClass)); // class inline operation parameters dependencies (non-ptr and non-ref) @@ -104,7 +108,7 @@ public class ClassUtils { addEnumerationsAndPrimitiveTypes(usedClasses, GenUtils.getTypesViaOperations(currentClass)); // inner classifier attribute dependencies (non-ptr and non-ref) - usedClasses.addAll(GenUtils.getInnerClassifierTypesViaAttributes(currentClass, ptrRefStereotypes, null, true)); + usedClasses.addAll(GenUtils.getInnerClassifierTypesViaAttributes(currentClass, ptrRefStereotypes, null, true, true)); addEnumerationsAndPrimitiveTypes(usedClasses, GenUtils.getInnerClassifierTypesViaAttributes(currentClass)); // inner classifier operation parameters dependencies (non-ptr and non-ref) @@ -169,19 +173,6 @@ public class ClassUtils { return usedClasses; } - private static void addEnumerationsAndPrimitiveTypes(List usedClasses, List unfilteredClasses) { - if (usedClasses != null && unfilteredClasses != null) { - for (Classifier classifier : unfilteredClasses) { - if ((classifier instanceof Enumeration) || (classifier instanceof PrimitiveType)) { - if (classifier.getOwner() instanceof Package) { - usedClasses.add(classifier); - } - } - } - } - - } - /** * Calculate the list of classifiers that needs to be declared in a header file * @@ -202,16 +193,18 @@ public class ClassUtils { EList> noCodeGenStereotypes = new BasicEList>(); noCodeGenStereotypes.add(NoCodeGen.class); - // class attributes dependencies (only ptr and ref) - usedClasses.addAll(GenUtils.getTypesViaAttributes(currentClass, null, ptrRefStereotypes, true)); + // class attributes dependencies (only ptr and ref and shared aggregation) + usedClasses.addAll(GenUtils.getTypesViaAttributes(currentClass, null, ptrRefStereotypes, true, false)); + usedClasses.addAll(GenUtils.getTypesViaSharedAggregationAttributes(currentClass)); // operation parameters dependencies usedClasses.addAll(GenUtils.getTypesViaOperations(currentClass)); usedClasses.removeAll(GenUtils.getTypesViaOperations(currentClass, noCodeGenStereotypes, inlineStereotypes, ptrRefStereotypes, null, false)); // Remove inline operation parameter types that have been included previously // no-specification opaque behavior dependencies usedClasses.addAll(GenUtils.getTypesViaOpaqueBehaviors(currentClass)); - // inner classifier attribute dependencies (only ptr and ref) - usedClasses.addAll(GenUtils.getInnerClassifierTypesViaAttributes(currentClass, null, ptrRefStereotypes, false)); + // inner classifier attribute dependencies (only ptr and ref and shared aggregation) + usedClasses.addAll(GenUtils.getInnerClassifierTypesViaAttributes(currentClass, null, ptrRefStereotypes, false, false)); + usedClasses.addAll(GenUtils.getInnerClassifierTypesViaSharedAggregationAttributes(currentClass)); // inner classifier parameters dependencies usedClasses.addAll(GenUtils.getInnerClassifierTypesViaOperations(currentClass)); usedClasses.removeAll(GenUtils.getInnerClassifierTypesViaOperations(currentClass, noCodeGenStereotypes, inlineStereotypes, ptrRefStereotypes, null, false)); // Remove inner classifier inline operation parameter types that have been included previously @@ -256,4 +249,17 @@ public class ClassUtils { return nestedOperations; } + + private static void addEnumerationsAndPrimitiveTypes(List usedClasses, List unfilteredClasses) { + if (usedClasses != null && unfilteredClasses != null) { + for (Classifier classifier : unfilteredClasses) { + if ((classifier instanceof Enumeration) || (classifier instanceof PrimitiveType)) { + if (classifier.getOwner() instanceof Package) { + usedClasses.add(classifier); + } + } + } + } + + } } diff --git a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/Modifier.java b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/Modifier.java index a55f4f6c0a0..05daf9e3965 100644 --- a/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/Modifier.java +++ b/extraplugins/qompass/codegen/cpp/org.eclipse.papyrus.cpp.codegen/src/org/eclipse/papyrus/cpp/codegen/utils/Modifier.java @@ -41,18 +41,19 @@ public class Modifier { public static String modPtr(Element propertyOrParameter) { // Pointer - String ptr = ""; //$NON-NLS-1$ + String ptr; //$NON-NLS-1$ Ptr cppPtr = UMLUtil.getStereotypeApplication(propertyOrParameter, Ptr.class); if (cppPtr != null) { ptr = (cppPtr.getDeclaration() != null) ? cppPtr.getDeclaration() : "*"; //$NON-NLS-1$ } else { - ptr = ""; //$NON-NLS-1$ - } - if (propertyOrParameter instanceof Property) { - if (((Property) propertyOrParameter).getAggregation() == AggregationKind.SHARED_LITERAL) { - ptr += "*"; //$NON-NLS-1$ + if (propertyOrParameter instanceof Property + && ((Property) propertyOrParameter).getAggregation() == AggregationKind.SHARED_LITERAL) { + ptr = "*"; //$NON-NLS-1$ + } else { + ptr = ""; //$NON-NLS-1$ } } + boolean ptrOrRef = GenUtils.hasStereotype(propertyOrParameter, Ref.class) || GenUtils.hasStereotype(propertyOrParameter, Ptr.class); diff --git a/extraplugins/qompass/codegen/org.eclipse.papyrus.codegen.base/src/org/eclipse/papyrus/codegen/base/GenUtils.java b/extraplugins/qompass/codegen/org.eclipse.papyrus.codegen.base/src/org/eclipse/papyrus/codegen/base/GenUtils.java index 426db4fe8de..93b1c43a059 100644 --- a/extraplugins/qompass/codegen/org.eclipse.papyrus.codegen.base/src/org/eclipse/papyrus/codegen/base/GenUtils.java +++ b/extraplugins/qompass/codegen/org.eclipse.papyrus.codegen.base/src/org/eclipse/papyrus/codegen/base/GenUtils.java @@ -22,6 +22,7 @@ import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.UniqueEList; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; +import org.eclipse.uml2.uml.AggregationKind; import org.eclipse.uml2.uml.Behavior; import org.eclipse.uml2.uml.Classifier; import org.eclipse.uml2.uml.Comment; @@ -174,9 +175,13 @@ public class GenUtils { * List of ALL stereotypes that must no be applied * @param includedStereotypes * List of ANY stereotype that must no be applied (at least one) + * @param bypassForInnerClassifiers + * Always include inner classifier types or not + * @param noSharedAggregation + * Always exclude attributes with a shared aggregation kind * @return Collection of classifiers which are the types of the attributes */ - public static EList getTypesViaAttributes(Classifier current, EList> excludedStereotypes, EList> includedStereotypes, boolean bypassForInnerClassifiers) { + public static EList getTypesViaAttributes(Classifier current, EList> excludedStereotypes, EList> includedStereotypes, boolean bypassForInnerClassifiers, boolean noSharedAggregation) { EList result = new UniqueEList(); Iterator attributes; @@ -186,14 +191,24 @@ public class GenUtils { Type type = currentAttribute.getType(); if (type != null) { + // Check type is inner classifier if (bypassForInnerClassifiers && !(type.getOwner() instanceof Package)) { // if we force inner class types to be processed addFarthestOwnerType(type, result); } else { - // Check stereotypes + // Check other conditions boolean ok = true; - if (excludedStereotypes != null && GenUtils.hasAnyStereotype(currentAttribute, excludedStereotypes)) { + + // Check attribute is shared aggregation + if (noSharedAggregation && currentAttribute.getAggregation() == AggregationKind.SHARED_LITERAL) { ok = false; } + // Check attribute does not have an excluded stereotype + if (ok) { + if (excludedStereotypes != null && GenUtils.hasAnyStereotype(currentAttribute, excludedStereotypes)) { + ok = false; + } + } + // Check attribute has an included stereotype if (ok) { if (includedStereotypes != null && !GenUtils.hasAnyStereotype(currentAttribute, includedStereotypes)) { ok = false; @@ -211,6 +226,32 @@ public class GenUtils { return result; } + /** + * Retrieve a list of types of attributes, with SHARED aggregation, that belong to the current classifier. + * + * @param current + * Class on which the attributes are searched + * @return Collection of classifiers which are the types of the attributes + */ + public static EList getTypesViaSharedAggregationAttributes(Classifier current) { + EList result = new UniqueEList(); + + Iterator attributes; + attributes = current.getAttributes().iterator(); + while (attributes.hasNext()) { + Property currentAttribute = attributes.next(); + Type type = currentAttribute.getType(); + + if (type != null) { + if (currentAttribute.getAggregation() == AggregationKind.SHARED_LITERAL) { + addFarthestOwnerType(type, result); + } + } + } + + return result; + } + /** * Retrieve the operations in the current classifier. For each * operation, collect types of its parameters. @@ -249,6 +290,8 @@ public class GenUtils { * List of ALL stereotypes that must not be applied to a parameter * @param includedParameterStereotypes * List of ANY stereotype that must be applied to a parameter (at least one) + * @param bypassForInnerClassifiers + * Always include types that are inner classifiers * @return Collection of classifiers which are the types of the operation parameters */ public static EList getTypesViaOperations(Classifier current, @@ -342,6 +385,8 @@ public class GenUtils { * List of ALL stereotypes that must not be applied to a parameter * @param includedParameterStereotypes * List of ANY stereotype that must be applied to a parameter (at least one) + * @param bypassForInnerClassifiers + * Always include types that are inner classifiers * @return Collection of classifiers which are the types of the opaque behavior parameters */ public static EList getTypesViaOpaqueBehaviors(Classifier current, @@ -437,13 +482,34 @@ public class GenUtils { * List of ALL stereotypes that must no be applied * @param includedStereotypes * List of ANY stereotype that must no be applied (at least one) + * @param bypassForInnerClassifiers + * Always include types that are inner classifiers + * @param noSharedAggregation + * Always exclude attributes with a shared aggregation kind + * @return Collection of classifiers which are the types of the attributes of inner classifiers + */ + public static EList getInnerClassifierTypesViaAttributes(Classifier current, EList> excludedStereotypes, EList> includedStereotypes, boolean bypassForInnerClassifiers, boolean noSharedAggregation) { + EList result = new UniqueEList(); + for (Element ownedElement : current.allOwnedElements()) { + if (ownedElement instanceof Classifier) { + result.addAll(getTypesViaAttributes((Classifier) ownedElement, excludedStereotypes, includedStereotypes, bypassForInnerClassifiers, noSharedAggregation)); + } + } + return result; + } + + /** + * Retrieve a list of types of attributes, of shared aggregation, of inner classifiers that belong to the current classifier. + * + * @param current + * Class on which the attributes are searched * @return Collection of classifiers which are the types of the attributes of inner classifiers */ - public static EList getInnerClassifierTypesViaAttributes(Classifier current, EList> excludedStereotypes, EList> includedStereotypes, boolean bypassForInnerClassifiers) { + public static EList getInnerClassifierTypesViaSharedAggregationAttributes(Classifier current) { EList result = new UniqueEList(); for (Element ownedElement : current.allOwnedElements()) { if (ownedElement instanceof Classifier) { - result.addAll(getTypesViaAttributes((Classifier) ownedElement, excludedStereotypes, includedStereotypes, bypassForInnerClassifiers)); + result.addAll(getTypesViaSharedAggregationAttributes((Classifier) ownedElement)); } } return result; @@ -712,7 +778,7 @@ public class GenUtils { * @param classifier * @return */ - private static void addFarthestOwnerType(Element element, EList result) { + public static void addFarthestOwnerType(Element element, EList result) { if (element == null || result == null) { return; } -- cgit v1.2.3