From ab4a5ee51c77f82994232b3b217e324b83f1be4a Mon Sep 17 00:00:00 2001 From: aradermache Date: Sat, 29 Jun 2013 21:30:28 +0000 Subject: - Fixed Acceleo problems: - incomplete file instead of "null" in case of build errors (additional log entry instead of exception) - Deactivate use of absolute path --- .../org/eclipse/papyrus/acceleo/AcceleoDriver.java | 67 +-- .../src/org/eclipse/papyrus/acceleo/GenUtils.java | 448 --------------------- .../eclipse/papyrus/acceleo/GenericGenUtils.java | 448 +++++++++++++++++++++ .../codegen/ui/handler/GenerateCodeHandler.java | 12 +- .../org.eclipse.papyrus.cpp.codegen/.classpath | 2 +- .../META-INF/MANIFEST.MF | 14 +- 6 files changed, 512 insertions(+), 479 deletions(-) delete mode 100644 extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenUtils.java create mode 100644 extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenericGenUtils.java (limited to 'extraplugins/codegen') diff --git a/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/AcceleoDriver.java b/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/AcceleoDriver.java index 3d8ca42d8e3..e669f023ec3 100644 --- a/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/AcceleoDriver.java +++ b/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/AcceleoDriver.java @@ -12,7 +12,6 @@ package org.eclipse.papyrus.acceleo; import java.io.File; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -30,6 +29,7 @@ import org.eclipse.acceleo.parser.AcceleoSourceBuffer; import org.eclipse.acceleo.parser.cst.ModuleImportsValue; import org.eclipse.core.runtime.ILogListener; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.emf.common.util.BasicEList; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.URI; @@ -50,13 +50,15 @@ public class AcceleoDriver { public static void init() { engine = new AcceleoEngine(); parser = new AcceleoParser(); + hasErrors = false; + lastErrors = false; // some errors are silently captured inside the evaluation query (@see AcceleoEvaluationVisiton.visitExpression). Yet they produce // an event in the Acceleo log which is captured via this listener. AcceleoEnginePlugin.getDefault().getLog().addLogListener(new ILogListener() { public void logging(IStatus status, String plugin) { - logEntries.add(status); - + hasErrors = true; + lastErrors = true; } }); acceleoResourceSet = new AcceleoResourceSetImpl(); @@ -111,8 +113,12 @@ public class AcceleoDriver { Resource r = acceleoResourceSet.getResource(uri, true); if(r != null) { // use absolute path, if possible, i.e. if file exists at absolute path - // this is required, since Acceleo references dependent file using a relative - // path + // this is required, since Acceleo references dependent files using a relative + // path. + // This workaround is no longer required. In the contrary, it is harmful for binary + // build + + /* String absoluteFileName = Utils.getAbsoluteFN(uri.toString()); if(absoluteFileName != null) { File fileCandidate = new File(absoluteFileName); @@ -122,6 +128,7 @@ public class AcceleoDriver { return URI.createFileURI(fileCandidate.getAbsolutePath()); } } + */ return uri; } } catch (Exception e) { @@ -238,7 +245,7 @@ public class AcceleoDriver { try { EList depURIs = new BasicEList(); - logEntries.clear(); + lastErrors = false; // deps.add(outputURI); // check, if imports are already in resource set @@ -310,7 +317,12 @@ public class AcceleoDriver { } try { // return evaluateURI(URI.createURI("org.eclipse.papyrus.cpp.codegen" + "/" + uriStr.replace(".", "/") + "." + IAcceleoConstants.EMTL_FILE_EXTENSION), element); - return evaluateURI(findFileInPlugin(moduleName), templateName, element); + URI uri = findFileInPlugin(moduleName); + if (uri == null) { + throw new RuntimeException("Cannot find Acceleo module " + moduleName + //$NON-NLS-1$ + ". Check whether the .emtl file exists and whether it is exported"); //$NON-NLS-1$ + } + return evaluateURI(uri, templateName, element); } catch (AcceleoException e) { } return null; @@ -352,22 +364,20 @@ public class AcceleoDriver { boolean dontCheck = (module.getOwnedModuleElement().size() == 1); for(ModuleElement me : module.getOwnedModuleElement()) { if((me instanceof Template) && (dontCheck || me.getName().equals(templateName))) { - logEntries.clear(); + lastErrors = false; Object stringResult = engine.evaluate((Template)me, arguments, new PreviewStrategy(), null); - if(logEntries.size() > 0) { - IStatus e = logEntries.get(0); - Throwable exception = e.getException(); - String message = exception.getMessage(); - - if(exception.getCause() instanceof InvocationTargetException) { - Throwable targetException = ((InvocationTargetException)exception.getCause()).getTargetException(); - if(targetException != null) { - message = targetException.getMessage(); - } + if (lastErrors) { + // we do not throw an exception, since it would imply that the evaluation result is + // lost. For a users, the (partially) evaluated result might be more useful to locate + // the problem than the actual error message. + // log additional info about template, which are not present in the acceleo log + String message = "an acceleo error occurred during the evaluation of template <" + //$NON-NLS-1$ + templateName + ">. Check previous errors in the log."; //$NON-NLS-1$ + if (templateStr.length() > 0) { + message += "Template contents:\n" + templateStr; //$NON-NLS-1$ } - throw new AcceleoException("Acceleo evaluation problems (showing only first): " + message + - "\n\nTemplate: " + templateName + "\n" + templateStr); - + AcceleoEnginePlugin.getDefault().getLog().log( + new Status(Status.INFO, AcceleoEnginePlugin.PLUGIN_ID, message)); } if(stringResult instanceof String) { return (String)stringResult; @@ -379,8 +389,19 @@ public class AcceleoDriver { throw new AcceleoEvaluationException("Template " + templateName + " not found"); } - protected static EList logEntries = new BasicEList(); - + public static void clearErrors() { + hasErrors = false; + lastErrors = false; + } + + public static boolean hasErrors() { + return hasErrors; + } + + protected static boolean hasErrors; + + protected static boolean lastErrors; + protected static AcceleoEngine engine = null; protected static ResourceSet acceleoResourceSet; diff --git a/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenUtils.java b/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenUtils.java deleted file mode 100644 index a8db150a582..00000000000 --- a/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenUtils.java +++ /dev/null @@ -1,448 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006 - 2012 CEA LIST. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * CEA LIST - initial API and implementation - *******************************************************************************/ - -package org.eclipse.papyrus.acceleo; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -import org.eclipse.emf.common.util.BasicEList; -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.Behavior; -import org.eclipse.uml2.uml.Class; -import org.eclipse.uml2.uml.Classifier; -import org.eclipse.uml2.uml.Comment; -import org.eclipse.uml2.uml.Dependency; -import org.eclipse.uml2.uml.DirectedRelationship; -import org.eclipse.uml2.uml.Element; -import org.eclipse.uml2.uml.NamedElement; -import org.eclipse.uml2.uml.OpaqueBehavior; -import org.eclipse.uml2.uml.Operation; -import org.eclipse.uml2.uml.Package; -import org.eclipse.uml2.uml.Parameter; -import org.eclipse.uml2.uml.ParameterableElement; -import org.eclipse.uml2.uml.Property; -import org.eclipse.uml2.uml.TemplateBinding; -import org.eclipse.uml2.uml.TemplateParameter; -import org.eclipse.uml2.uml.TemplateSignature; -import org.eclipse.uml2.uml.Type; - - -/** - * Some utilities: a set of static methods for Acceleo based code generation - * - * @author wassim - * - */ -public class GenUtils { - - /** - * Retrieve template bindings for the class passed as a Parameter - * only one template binding can exist for an element - * - * @param current - * Class on which the template binding is searched - * @return the template binding of current Class - */ - public static TemplateBinding getTemplateBindings(Class current) { - TemplateBinding binding = null; - if(current.getTemplateBindings().size() == 1) { - binding = current.getTemplateBindings().get(0); - } - - return binding; - } - - /** - * Check whether the passed classifier has a template binding with itself as bound element - * - * @param cl - * @return - */ - public static boolean isTemplateBoundElement(Classifier cl) { - boolean result = false; - EList tbs = cl.getTemplateBindings(); - if(tbs.size() > 0) { - for (TemplateBinding tb : tbs) { - // TODO: will only work for single element in template binding list - result = tb.getBoundElement() == cl; - } - } - return result; - } - - - /** - * - * @param classifier - * @return - */ - public static Collection getTemplateParameters(Classifier classifier) { - - Collection params = new ArrayList(); - TemplateSignature ts = classifier.getOwnedTemplateSignature(); - if(ts != null) { - params.addAll(ts.getOwnedParameters()); - } - - return params; - } - - /** - * - * @param classifier - * @return - */ - public static Collection getTemplateParameteredElements(Classifier classifier) { - - Collection params = new ArrayList(); - TemplateSignature ts = classifier.getOwnedTemplateSignature(); - if(ts != null) { - for(TemplateParameter tp : ts.getOwnedParameters()) { - if(tp != null) { - params.add(tp.getParameteredElement()); - } - } - } - return params; - } - - - /** - * Retrieve a list of types that belong to by a classifier in the current class - * - * @param current - * Class on which the attributes are searched - * @return collection of classes which are the type of the attributes - */ - public static EList getTypesViaAttributesk(Classifier current) { - EList result = new UniqueEList(); - - for (Property currentAttribute : current.getAttributes()) { - Type type = currentAttribute.getType(); - if(type instanceof Classifier) { - result.add((Classifier) type); - } - } - return result; - } - - /** - * Retrieve the operations in the current class then for each - * operation it finds the parameters that have a class type - * - * @param current - * Class on which the attributes are searched - * @return collection of classes which are the types of the operations parameters - */ - public static EList getTypesViaOperations(Classifier current) { - EList result = new UniqueEList(); - for(Operation operation : current.getOperations()) { - for (Parameter param : operation.getOwnedParameters()) { - Type type = param.getType(); - if(type instanceof Classifier) { - Classifier paramType = (Classifier)type; - result.add(paramType); - } - } - } - return result; - } - - /** - * Return a list of classifiers that are referenced by relationships, i.e. - * dependencies or associations - * - * @param current - * @return - */ - public static EList getTypesViaRelationships(Classifier current) { - EList classifiers = new UniqueEList(); - - for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { - - if(relationship.getTargets().size() > 0) { - // there should always be at least one element in the target - // list and it should be a classifier, but better check. - Element element = relationship.getTargets().get(0); - if(element instanceof Classifier) { - classifiers.add((Classifier)element); - } - } - } - return classifiers; - } - - /** - * Return a list of classifiers that are referenced via dependencies - * - * @param current - * @return - */ - public static EList getTypesViaDependencies(Classifier current) { - EList classifiers = new UniqueEList(); - - for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { - if(relationship instanceof Dependency) { - if(relationship.getTargets().size() > 0) { - // there should always be at least one element in the target - // list and it should be a classifier, but better check. - Element element = relationship.getTargets().get(0); - if(element instanceof Classifier) { - classifiers.add((Classifier)element); - } - } - } - } - return classifiers; - } - - /** - * Return a list of classifiers that are referenced via all kinds of relations except - * dependencies - * - * @param current - * @return - */ - public static EList getTypesViaRelationshipsNoDeps(Classifier current) { - EList classifiers = new UniqueEList(); - - for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { - if(!(relationship instanceof Dependency)) { - if(relationship.getTargets().size() > 0) { - // there should always be at least one element in the target - // list and it should be a classifier, but better check. - Element element = relationship.getTargets().get(0); - if(element instanceof Classifier) { - classifiers.add((Classifier)element); - } - } - } - } - return classifiers; - } - - /** - * Return the qualified name of a named element, but use "_" instead of "::" as separator - * - * @param ne - * a named element - * @return the fully qualified name with "_" as separator character - */ - public static String getFullName(NamedElement ne) { - return ne.getQualifiedName().replace("::", "_"); //$NON-NLS-1$ //$NON-NLS-2$ - } - - /** - * return the full name in upper case - * - * @param ne - * @return - */ - public static String getFullNameUC(NamedElement ne) { - return ne.getQualifiedName().replace("::", "_").toUpperCase(); //$NON-NLS-1$ //$NON-NLS-2$ - } - - - /** - * Retrieve the comments associated with an element - * TODO: check whether comment's annotated element link belongs to element in question - * @param element - * @return - */ - public static String getComments(Element element) { - String commentText = ""; //$NON-NLS-1$ - for(Comment comment : element.getOwnedComments()) { - // remove eventual CRs (avoid confusion in Acceleo template which adds " *" after line breaks) - commentText += cleanCR(comment.getBody()); - } - return commentText; - } - - /** - * Return a list of dependent package (the list of dependent - * elements filtered for packages) - * - * @param pkg - * @return - */ - public static EList getUsedPackages(Package pkg) { - EList result = new UniqueEList(); - for(Element depElement : pkg.getClientDependencies()) { - if(depElement instanceof Package) { - result.add((Package)depElement); - } - } - return result; - } - - /** - * Return a list of dependent classifiers (the list of dependent - * elements filtered for classifiers) - * - * @param pkg - * @return - */ - public static EList getUsedClassifiers(Classifier cls) { - EList result = new BasicEList(); - for(Element depElement : cls.getClientDependencies()) { - if(depElement instanceof Classifier) { - result.add((Classifier)depElement); - } - } - return result; - } - - /** - * Return the qualified name of a package, but use "/" instead of "::" as separator - * - * @param pkg - * @return - */ - public static String getFullPath(Package pkg) { - return pkg.getQualifiedName().replace("::", "/"); //$NON-NLS-1$//$NON-NLS-2$ - } - - /** - * Is a certain stereotype applied? - * - * @param element - * @param stereotype - * fully qualified stereotype name - * @return - */ - public static boolean hasStereotype(Element element, String stereotName) { - return element.getAppliedStereotype(stereotName) != null; - } - - /** - * Is a certain stereotype applied? - * - * @param element - * @param stereotype - * fully qualified stereotype name - * @return - */ - public static boolean hasStereotype(Element element, java.lang.Class clazz) { - for(EObject stereoApplication : element.getStereotypeApplications()) { - // check whether the stereotype is a super-class of the passed parameter clazz - if(clazz.isAssignableFrom(stereoApplication.getClass())) { - return true; - } - } - return false; - } - - - /** - * Is a certain stereotype applied? - * - * @param element - * @param eClass The eClass associated with the stereotype name - * @return - */ - public static boolean hasStereotypeA(Element element, EClass eClass) { - if(element == null) { - // make query more robust - return false; - } - for(EObject stereoApplication : element.getStereotypeApplications()) { - // check whether the stereotype application has the right eClass - if(stereoApplication.eClass() == eClass) { - return true; - } - } - return false; - } - - /** - * Return the stereotype application by passing an element of the static profile - * - * @param element - * the 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 getApplication(Element element, java.lang.Class 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; - } - - public static EObject getApplicationA(Element element, EClass eClass) { - for(EObject stereoApplication : element.getStereotypeApplications()) { - // check whether the stereotype is an instance of the passed parameter clazz - if(stereoApplication.eClass() == eClass) { - return stereoApplication; - } - } - return null; - } - - - /** - * @param operation - * the operation - * @param selectedLanguage - * the selected language - * @return Return the first body of a selected language that is provided by - * one of the operation's methods - */ - public static String getBody(Operation operation, String selectedLanguage) { - for(Behavior behavior : operation.getMethods()) { - if(behavior instanceof OpaqueBehavior) { - OpaqueBehavior ob = (OpaqueBehavior)behavior; - Iterator bodies = ob.getBodies().iterator(); - for(String language : ob.getLanguages()) { - String body = bodies.next(); - if(language.equals(selectedLanguage)) { - // additional "\r" confuses Acceleo - return cleanCR(body); - } - } - } - } - return ""; //$NON-NLS-1$ - } - - /** - * Remove from a String. These confuse Acceleo's indentation - * @param str - * @return - */ - public static String cleanCR(String str) { - return str.replace("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$ - } - - - /** - * Avoid null strings, i.e. replace null strings by empty strings - * - * @param str - * @return - */ - public static String maskNull(String str) { - if(str == null) { - return ""; //$NON-NLS-1$ - } - return str; - } -} diff --git a/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenericGenUtils.java b/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenericGenUtils.java new file mode 100644 index 00000000000..5bf3d9df19a --- /dev/null +++ b/extraplugins/codegen/org.eclipse.papyrus.acceleo/src/org/eclipse/papyrus/acceleo/GenericGenUtils.java @@ -0,0 +1,448 @@ +/******************************************************************************* + * Copyright (c) 2006 - 2012 CEA LIST. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * CEA LIST - initial API and implementation + *******************************************************************************/ + +package org.eclipse.papyrus.acceleo; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import org.eclipse.emf.common.util.BasicEList; +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.Behavior; +import org.eclipse.uml2.uml.Class; +import org.eclipse.uml2.uml.Classifier; +import org.eclipse.uml2.uml.Comment; +import org.eclipse.uml2.uml.Dependency; +import org.eclipse.uml2.uml.DirectedRelationship; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.OpaqueBehavior; +import org.eclipse.uml2.uml.Operation; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.Parameter; +import org.eclipse.uml2.uml.ParameterableElement; +import org.eclipse.uml2.uml.Property; +import org.eclipse.uml2.uml.TemplateBinding; +import org.eclipse.uml2.uml.TemplateParameter; +import org.eclipse.uml2.uml.TemplateSignature; +import org.eclipse.uml2.uml.Type; + + +/** + * Some utilities: a set of static methods for Acceleo based code generation + * + * @author wassim + * + */ +public class GenericGenUtils { + + /** + * Retrieve template bindings for the class passed as a Parameter + * only one template binding can exist for an element + * + * @param current + * Class on which the template binding is searched + * @return the template binding of current Class + */ + public static TemplateBinding getTemplateBindings(Class current) { + TemplateBinding binding = null; + if(current.getTemplateBindings().size() == 1) { + binding = current.getTemplateBindings().get(0); + } + + return binding; + } + + /** + * Check whether the passed classifier has a template binding with itself as bound element + * + * @param cl + * @return + */ + public static boolean isTemplateBoundElement(Classifier cl) { + boolean result = false; + EList tbs = cl.getTemplateBindings(); + if(tbs.size() > 0) { + for (TemplateBinding tb : tbs) { + // TODO: will only work for single element in template binding list + result = tb.getBoundElement() == cl; + } + } + return result; + } + + + /** + * + * @param classifier + * @return + */ + public static Collection getTemplateParameters(Classifier classifier) { + + Collection params = new ArrayList(); + TemplateSignature ts = classifier.getOwnedTemplateSignature(); + if(ts != null) { + params.addAll(ts.getOwnedParameters()); + } + + return params; + } + + /** + * + * @param classifier + * @return + */ + public static Collection getTemplateParameteredElements(Classifier classifier) { + + Collection params = new ArrayList(); + TemplateSignature ts = classifier.getOwnedTemplateSignature(); + if(ts != null) { + for(TemplateParameter tp : ts.getOwnedParameters()) { + if(tp != null) { + params.add(tp.getParameteredElement()); + } + } + } + return params; + } + + + /** + * Retrieve a list of types that belong to by a classifier in the current class + * + * @param current + * Class on which the attributes are searched + * @return collection of classes which are the type of the attributes + */ + public static EList getTypesViaAttributesk(Classifier current) { + EList result = new UniqueEList(); + + for (Property currentAttribute : current.getAttributes()) { + Type type = currentAttribute.getType(); + if(type instanceof Classifier) { + result.add((Classifier) type); + } + } + return result; + } + + /** + * Retrieve the operations in the current class then for each + * operation it finds the parameters that have a class type + * + * @param current + * Class on which the attributes are searched + * @return collection of classes which are the types of the operations parameters + */ + public static EList getTypesViaOperations(Classifier current) { + EList result = new UniqueEList(); + for(Operation operation : current.getOperations()) { + for (Parameter param : operation.getOwnedParameters()) { + Type type = param.getType(); + if(type instanceof Classifier) { + Classifier paramType = (Classifier)type; + result.add(paramType); + } + } + } + return result; + } + + /** + * Return a list of classifiers that are referenced by relationships, i.e. + * dependencies or associations + * + * @param current + * @return + */ + public static EList getTypesViaRelationships(Classifier current) { + EList classifiers = new UniqueEList(); + + for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { + + if(relationship.getTargets().size() > 0) { + // there should always be at least one element in the target + // list and it should be a classifier, but better check. + Element element = relationship.getTargets().get(0); + if(element instanceof Classifier) { + classifiers.add((Classifier)element); + } + } + } + return classifiers; + } + + /** + * Return a list of classifiers that are referenced via dependencies + * + * @param current + * @return + */ + public static EList getTypesViaDependencies(Classifier current) { + EList classifiers = new UniqueEList(); + + for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { + if(relationship instanceof Dependency) { + if(relationship.getTargets().size() > 0) { + // there should always be at least one element in the target + // list and it should be a classifier, but better check. + Element element = relationship.getTargets().get(0); + if(element instanceof Classifier) { + classifiers.add((Classifier)element); + } + } + } + } + return classifiers; + } + + /** + * Return a list of classifiers that are referenced via all kinds of relations except + * dependencies + * + * @param current + * @return + */ + public static EList getTypesViaRelationshipsNoDeps(Classifier current) { + EList classifiers = new UniqueEList(); + + for(DirectedRelationship relationship : current.getSourceDirectedRelationships()) { + if(!(relationship instanceof Dependency)) { + if(relationship.getTargets().size() > 0) { + // there should always be at least one element in the target + // list and it should be a classifier, but better check. + Element element = relationship.getTargets().get(0); + if(element instanceof Classifier) { + classifiers.add((Classifier)element); + } + } + } + } + return classifiers; + } + + /** + * Return the qualified name of a named element, but use "_" instead of "::" as separator + * + * @param ne + * a named element + * @return the fully qualified name with "_" as separator character + */ + public static String getFullName(NamedElement ne) { + return ne.getQualifiedName().replace("::", "_"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + /** + * return the full name in upper case + * + * @param ne + * @return + */ + public static String getFullNameUC(NamedElement ne) { + return ne.getQualifiedName().replace("::", "_").toUpperCase(); //$NON-NLS-1$ //$NON-NLS-2$ + } + + + /** + * Retrieve the comments associated with an element + * TODO: check whether comment's annotated element link belongs to element in question + * @param element + * @return + */ + public static String getComments(Element element) { + String commentText = ""; //$NON-NLS-1$ + for(Comment comment : element.getOwnedComments()) { + // remove eventual CRs (avoid confusion in Acceleo template which adds " *" after line breaks) + commentText += cleanCR(comment.getBody()); + } + return commentText; + } + + /** + * Return a list of dependent package (the list of dependent + * elements filtered for packages) + * + * @param pkg + * @return + */ + public static EList getUsedPackages(Package pkg) { + EList result = new UniqueEList(); + for(Element depElement : pkg.getClientDependencies()) { + if(depElement instanceof Package) { + result.add((Package)depElement); + } + } + return result; + } + + /** + * Return a list of dependent classifiers (the list of dependent + * elements filtered for classifiers) + * + * @param pkg + * @return + */ + public static EList getUsedClassifiers(Classifier cls) { + EList result = new BasicEList(); + for(Element depElement : cls.getClientDependencies()) { + if(depElement instanceof Classifier) { + result.add((Classifier)depElement); + } + } + return result; + } + + /** + * Return the qualified name of a package, but use "/" instead of "::" as separator + * + * @param pkg + * @return + */ + public static String getFullPath(Package pkg) { + return pkg.getQualifiedName().replace("::", "/"); //$NON-NLS-1$//$NON-NLS-2$ + } + + /** + * Is a certain stereotype applied? + * + * @param element + * @param stereotype + * fully qualified stereotype name + * @return + */ + public static boolean hasStereotype(Element element, String stereotName) { + return element.getAppliedStereotype(stereotName) != null; + } + + /** + * Is a certain stereotype applied? + * + * @param element + * @param stereotype + * fully qualified stereotype name + * @return + */ + public static boolean hasStereotype(Element element, java.lang.Class clazz) { + for(EObject stereoApplication : element.getStereotypeApplications()) { + // check whether the stereotype is a super-class of the passed parameter clazz + if(clazz.isAssignableFrom(stereoApplication.getClass())) { + return true; + } + } + return false; + } + + + /** + * Is a certain stereotype applied? + * + * @param element + * @param eClass The eClass associated with the stereotype name + * @return + */ + public static boolean hasStereotypeA(Element element, EClass eClass) { + if(element == null) { + // make query more robust + return false; + } + for(EObject stereoApplication : element.getStereotypeApplications()) { + // check whether the stereotype application has the right eClass + if(stereoApplication.eClass() == eClass) { + return true; + } + } + return false; + } + + /** + * Return the stereotype application by passing an element of the static profile + * + * @param element + * the 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 getApplication(Element element, java.lang.Class 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; + } + + public static EObject getApplicationA(Element element, EClass eClass) { + for(EObject stereoApplication : element.getStereotypeApplications()) { + // check whether the stereotype is an instance of the passed parameter clazz + if(stereoApplication.eClass() == eClass) { + return stereoApplication; + } + } + return null; + } + + + /** + * @param operation + * the operation + * @param selectedLanguage + * the selected language + * @return Return the first body of a selected language that is provided by + * one of the operation's methods + */ + public static String getBody(Operation operation, String selectedLanguage) { + for(Behavior behavior : operation.getMethods()) { + if(behavior instanceof OpaqueBehavior) { + OpaqueBehavior ob = (OpaqueBehavior)behavior; + Iterator bodies = ob.getBodies().iterator(); + for(String language : ob.getLanguages()) { + String body = bodies.next(); + if(language.equals(selectedLanguage)) { + // additional "\r" confuses Acceleo + return cleanCR(body); + } + } + } + } + return ""; //$NON-NLS-1$ + } + + /** + * Remove from a String. These confuse Acceleo's indentation + * @param str + * @return + */ + public static String cleanCR(String str) { + return str.replace("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + + + /** + * Avoid null strings, i.e. replace null strings by empty strings + * + * @param str + * @return + */ + public static String maskNull(String str) { + if(str == null) { + return ""; //$NON-NLS-1$ + } + return str; + } +} diff --git a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen.ui/src/org/eclipse/papyrus/cpp/codegen/ui/handler/GenerateCodeHandler.java b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen.ui/src/org/eclipse/papyrus/cpp/codegen/ui/handler/GenerateCodeHandler.java index 9232fe2c0f7..019afb0fc19 100644 --- a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen.ui/src/org/eclipse/papyrus/cpp/codegen/ui/handler/GenerateCodeHandler.java +++ b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen.ui/src/org/eclipse/papyrus/cpp/codegen/ui/handler/GenerateCodeHandler.java @@ -24,10 +24,13 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.papyrus.acceleo.AcceleoDriver; import org.eclipse.papyrus.cpp.codegen.preferences.CppCodeGenUtils; import org.eclipse.papyrus.cpp.codegen.transformation.CppModelElementsCreator; import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import org.eclipse.uml2.uml.Classifier; @@ -91,13 +94,18 @@ public class GenerateCodeHandler extends AbstractHandler { // get the container for the current element String headerSuffix = CppCodeGenUtils.getHeaderSuffix(); String bodySuffix = CppCodeGenUtils.getBodySuffix(); + AcceleoDriver.clearErrors(); CppModelElementsCreator mec = new CppModelElementsCreator(modelProject); IContainer srcPkg = mec.getContainer(classifier); try { mec.createPackageableElement(srcPkg, null, classifier); - IFile cppFile = srcPkg.getFile(new Path(name + "." + bodySuffix)); - IFile hFile = srcPkg.getFile(new Path(name + "." + headerSuffix)); + if (AcceleoDriver.hasErrors()) { + MessageDialog.openInformation(new Shell(), "Errors during code generation", //$NON-NLS-1$ + "Errors occured during code generation. Please check the error log"); //$NON-NLS-1$ + } + IFile cppFile = srcPkg.getFile(new Path(name + "." + bodySuffix)); //$NON-NLS-1$ + IFile hFile = srcPkg.getFile(new Path(name + "." + headerSuffix)); //$NON-NLS-1$ if(!cppFile.exists()) { return null; } diff --git a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/.classpath b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/.classpath index 64c5e31b7a2..ad32c83a788 100644 --- a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/.classpath +++ b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/.classpath @@ -1,6 +1,6 @@ - + diff --git a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/META-INF/MANIFEST.MF b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/META-INF/MANIFEST.MF index 647ebebb0c1..98b2ab730e2 100644 --- a/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/META-INF/MANIFEST.MF +++ b/extraplugins/codegen/org.eclipse.papyrus.cpp.codegen/META-INF/MANIFEST.MF @@ -16,9 +16,13 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.filesystem;bundle-version="1.3.200", org.eclipse.papyrus.acceleo;bundle-version="0.10.0" Eclipse-LazyStart: true -Export-Package: org.eclipse.papyrus.cpp.codegen, - org.eclipse.papyrus.cpp.codegen.preferences, - org.eclipse.papyrus.cpp.codegen.transformation, - org.eclipse.papyrus.cpp.codegen.utils -Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Export-Package: org.eclipse.papyrus.cpp.codegen;uses:="org.eclipse.ui.plugin,org.osgi.framework,org.eclipse.uml2.uml", + org.eclipse.papyrus.cpp.codegen.preferences;uses:="org.eclipse.jface.preference,org.eclipse.core.runtime.preferences", + org.eclipse.papyrus.cpp.codegen.transformation; + uses:="org.eclipse.core.runtime, + org.eclipse.papyrus.acceleo, + org.eclipse.core.resources, + org.eclipse.uml2.uml", + org.eclipse.papyrus.cpp.codegen.utils;uses:="org.eclipse.emf.ecore,org.eclipse.emf.common.util,org.eclipse.uml2.uml" +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy -- cgit v1.2.3