/**
   * Tests whether the refactoring
   *
   * @throws Exception In case of an unexpected exception.
   */
  @Test
  public void testRefactorCaseMethodAddDifferentParam() throws Exception {
    VariationPoint vp = RefactoringTestUtil.getMethodAddDifferentParamCase(VariabilityType.OPTXOR);
    IfStaticConfigClassMethod refactoring = new IfStaticConfigClassMethod();
    refactoring.refactor(vp, null);

    // location has two methods
    Class vpLocation = (Class) ((JaMoPPJavaSoftwareElement) vp.getLocation()).getJamoppElement();
    assertThat(vpLocation.getMethods().size(), equalTo(2));

    Method firstMethod = vpLocation.getMethods().get(0);
    Method secondMethod = vpLocation.getMethods().get(1);

    // assert number of parameters
    assertThat(firstMethod.getParameters().size(), anyOf(equalTo(1), equalTo(2)));
    assertThat(secondMethod.getParameters().size(), anyOf(equalTo(1), equalTo(2)));
    assertThat(
        firstMethod.getParameters().size(), not(equalTo(secondMethod.getParameters().size())));

    // assert method names and return types
    assertThat(firstMethod.getName(), equalTo("someMethod"));
    assertThat(secondMethod.getName(), equalTo("someMethod"));
    assertThat(firstMethod.getTypeReference(), instanceOf(Void.class));
    assertThat(secondMethod.getTypeReference(), instanceOf(Void.class));

    // verify correct VPM
    RefactoringTestUtil.assertValidVPM(vp);
  }
예제 #2
0
  public Set<Method> getAnnotatedMethods(Class customClass) {
    Set<Method> annotatedMethods = new HashSet<Method>();

    List<Method> methods = customClass.getMethods();
    for (Method method : methods) {
      List<String> comments = method.getComments();
      if (comments != null && comments.size() > 0) {
        for (String comment : comments) {
          String[] lines = comment.split("[\\r\\n]+");
          for (String line : lines) {
            String deleteWhitespace = StringUtils.deleteWhitespace(line);
            if (StringUtils.endsWith(deleteWhitespace, MODEL_ANNOTATION)) {
              String difference = StringUtils.removeEnd(deleteWhitespace, MODEL_ANNOTATION);
              if (StringUtils.containsOnly(difference, VALID_PREFIX_CHARACTERS)
                  || difference.isEmpty()) {
                annotatedMethods.add(method);
              }
            }
          }
        }
      }
    }
    return annotatedMethods;
  }
예제 #3
0
  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();
    }
  }