@Override public EOperation define(EcoreEnvironment env) { EOperation eOperation = EcoreFactory.eINSTANCE.createEOperation(); eOperation.setName(fName); int pos = 0; for (EClassifier cls : fParamTypes) { EParameter eParam = EcoreFactory.eINSTANCE.createEParameter(); String paramName = cls.getName(); if (fParamNames != null) { paramName = fParamNames[pos++]; } eParam.setName(paramName); eParam.setEType(cls); eOperation.getEParameters().add(eParam); } eOperation.setEType(fReturnType); assert fContextType instanceof EClass; ((EClass) fContextType).getEOperations().add(eOperation); CallHandlerAdapter.attach(eOperation, fDispatcher); return eOperation; }
public void propagateEOperations(JavaResource resource, GenClass genClass) { GenPackage genPackage = genClass.getGenPackage(); EPackage ePackage = genPackage.getEcorePackage(); if (resource.getContents().isEmpty() || !(resource.getContents().get(0) instanceof CompilationUnit)) { return; } CompilationUnit cu = (CompilationUnit) resource.getContents().get(0); Class customClass = (Class) cu.getClassifiers().get(0); EClass eClass = genClass.getEcoreClass(); if (eClass == null) { return; } Set<Method> annotatedMethods = getAnnotatedMethods(customClass); for (Method method : annotatedMethods) { for (AnnotationInstanceOrModifier modifier : method.getAnnotationsAndModifiers()) { if (modifier instanceof Public) { EOperation newEOperation = EcoreFactory.eINSTANCE.createEOperation(); newEOperation.setName(method.getName()); Type opType = method.getTypeReference().getTarget(); newEOperation.setEType( eClassifierForCustomClass(opType, method.getTypeReference(), ePackage)); if (isMulti(opType)) { newEOperation.setUpperBound(-1); } for (Parameter parameter : method.getParameters()) { EParameter newEParameter = EcoreFactory.eINSTANCE.createEParameter(); newEParameter.setName(parameter.getName()); Type paramType = parameter.getTypeReference().getTarget(); newEParameter.setEType( eClassifierForCustomClass(paramType, parameter.getTypeReference(), ePackage)); // TODO generics, ... newEOperation.getEParameters().add(newEParameter); } // TODO @jendrik: why is that needed? // for (AnnotationInstanceOrModifier annotationInstance : // method.getAnnotationsAndModifiers()) { // if (annotationInstance instanceof AnnotationInstance) { // Classifier javaAnnotation = ((AnnotationInstance) // annotationInstance).getAnnotation(); // if (javaAnnotation.eIsProxy()) { // continue; // } // EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); // eAnnotation.setSource(javaAnnotation.getContainingCompilationUnit( // ).getNamespacesAsString() + javaAnnotation.getName()); // newEOperation.getEAnnotations().add(eAnnotation); // } // } boolean operationAlreadyExists = false; List<EOperation> operations = eClass.getEOperations(); List<EOperation> existingOperations = new ArrayList<EOperation>(operations); // must be done here already for ensuring that compared operations have the same parent eClass.getEOperations().add(newEOperation); for (EOperation existingOperation : existingOperations) { boolean removed = removeAnnotation(existingOperation); if (EcoreUtil.equals(existingOperation, newEOperation)) { operationAlreadyExists = true; removeAnnotation(existingOperation); annotateAsGenerated(existingOperation); break; } if (removed) { annotateAsGenerated(existingOperation); } } if (!operationAlreadyExists) { annotateAsGenerated(newEOperation); } else { eClass.getEOperations().remove(newEOperation); } break; } } } try { Resource ecoreResource = ePackage.eResource(); URI originalURI = ecoreResource.getURI(); if (originalURI.isFile()) { String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString(); URI platformURI = URI.createPlatformResourceURI( originalURI.toFileString().substring(workspacePath.length()), true); ecoreResource.setURI(platformURI); } new ResourceSaver().saveResource(ecoreResource); ecoreResource.setURI(originalURI); } catch (IOException e) { e.printStackTrace(); } }