| /******************************************************************************* |
| * Copyright (c) 2013 E.D.Willink and others. |
| * 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: |
| * E.D.Willink - Initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.ocl.examples.build.genmodel; |
| |
| import org.eclipse.emf.codegen.ecore.generator.GeneratorAdapterFactory; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenAnnotation; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenClass; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenModel; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenModelFactory; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage; |
| import org.eclipse.emf.codegen.ecore.genmodel.GenOperation; |
| import org.eclipse.emf.codegen.ecore.genmodel.generator.GenClassGeneratorAdapter; |
| import org.eclipse.emf.common.util.Monitor; |
| import org.eclipse.emf.ecore.EOperation; |
| import org.eclipse.emf.ecore.EcoreFactory; |
| import org.eclipse.emf.ecore.EcorePackage; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| |
| // |
| // Overridden to allow static templates to be invoked standalone. |
| // |
| public class OCLBuildGenClassGeneratorAdapter extends GenClassGeneratorAdapter |
| { |
| private static final JETEmitterDescriptor[] JET_EMITTER_DESCRIPTORS = { |
| new JETEmitterDescriptor("model/Class.javajet", "org.eclipse.ocl.examples.build.templates.model.Class"), |
| new JETEmitterDescriptor("edit/ItemProvider.javajet", "org.eclipse.emf.codegen.ecore.templates.edit.ItemProvider"), |
| new JETEmitterDescriptor("model.tests/TestCase.javajet", "org.eclipse.emf.codegen.ecore.templates.model.tests.TestCase")}; |
| |
| // |
| // Overridden to provide a super toString() wherever GenModel would have autogenerated one. |
| // |
| @Override |
| protected void generateClass(GenClass genClass, Monitor monitor) { |
| GenModel genModel = genClass.getGenModel(); |
| GenAnnotation genAnnotation = genModel.getGenAnnotation(OCLBuildGenModelUtil.OCL_GENMODEL_TO_STRING_URI); |
| if (genAnnotation != null) { |
| if (genModel.isOperationReflection()) { |
| throw new RuntimeException("OperationReflection must be false to use ToString GenAnnotation, since EObject operations are not eInvokeable"); |
| } |
| String interfaceName = genClass.getQualifiedInterfaceName(); |
| String body = genAnnotation.getDetails().get(interfaceName); |
| boolean getsToString = !genClass.getToStringGenFeatures().isEmpty(); |
| boolean hasToString = genClass.hasImplementedToStringGenOperation(); |
| if ((body != null) || (getsToString && !hasToString)) { |
| EOperation eOp = EcoreFactory.eINSTANCE.createEOperation(); |
| eOp.setName("toString"); |
| eOp.setEType(EcorePackage.Literals.ESTRING); |
| EcoreUtil.setAnnotation(eOp, GenModelPackage.eNS_URI, "body", body != null ? body : "return super.toString();"); |
| GenOperation genOp = GenModelFactory.eINSTANCE.createGenOperation(); |
| genOp.setEcoreOperation(eOp); |
| genClass.getGenOperations().add(genOp); |
| EcoreUtil.setSuppressedVisibility(eOp, true); // Don't need toString() in interface |
| } |
| } |
| super.generateClass(genClass, monitor); |
| } |
| |
| /** |
| * Returns the set of <code>JETEmitterDescriptor</code>s used by the |
| * adapter. The contents of the returned array should never be changed. |
| * Rather, subclasses may override this method to return a different array |
| * altogether. |
| */ |
| @Override |
| protected JETEmitterDescriptor[] getJETEmitterDescriptors() { |
| return JET_EMITTER_DESCRIPTORS; |
| } |
| |
| public OCLBuildGenClassGeneratorAdapter(GeneratorAdapterFactory generatorAdapterFactory) { |
| super(generatorAdapterFactory); |
| } |
| } |