public static CtMethod cloneMethodTest(CtMethod method, String suffix, int timeOut) {
    CtMethod cloned_method = cloneMethod(method, suffix);
    CtAnnotation testAnnotation =
        cloned_method
            .getAnnotations()
            .stream()
            .filter(annotation -> annotation.toString().contains("Test"))
            .findFirst()
            .orElse(null);

    if (testAnnotation != null) {
      cloned_method.removeAnnotation(testAnnotation);
    }

    testAnnotation = method.getFactory().Core().createAnnotation();
    CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference();
    ref.setSimpleName("Test");

    CtPackageReference refPackage = method.getFactory().Core().createPackageReference();
    refPackage.setSimpleName("org.junit");
    ref.setPackage(refPackage);
    testAnnotation.setAnnotationType(ref);

    Map<String, Object> elementValue = new HashMap<>();
    elementValue.put("timeout", timeOut);
    testAnnotation.setElementValues(elementValue);

    cloned_method.addAnnotation(testAnnotation);

    return cloned_method;
  }
Ejemplo n.º 2
0
 /**
  * Creates a reference to a package.
  *
  * @param name full name of the package to reference
  */
 public CtPackageReference createReference(String name) {
   CtPackageReference ref = factory.Core().createPackageReference();
   ref.setSimpleName(name);
   return ref;
 }