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;
  }
Exemple #2
0
 @SuppressWarnings("unchecked")
 public <A extends Annotation> CtAnnotation<A> getAnnotation(CtTypeReference<A> annotationType) {
   for (CtAnnotation<? extends Annotation> a : getAnnotations()) {
     if (a.getAnnotationType().equals(annotationType)) {
       return (CtAnnotation<A>) a;
     }
   }
   return null;
 }
Exemple #3
0
 @SuppressWarnings("unchecked")
 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
   for (CtAnnotation<? extends Annotation> a : getAnnotations()) {
     if (a.getAnnotationType().toString().equals(annotationType.getName().replace('$', '.'))) {
       return ((CtAnnotation<A>) a).getActualAnnotation();
     }
   }
   return null;
 }
 static <A extends Annotation> void removeAnnotation(CtMethod<?> method, Class<A> annClass) {
   CtAnnotation<?> toRemove = null;
   for (CtAnnotation<? extends Annotation> ctAnnotation : method.getAnnotations()) {
     if (annClass.isAssignableFrom(ctAnnotation.getActualAnnotation().getClass())) {
       toRemove = ctAnnotation;
       break;
     }
   }
   if (toRemove != null) method.removeAnnotation(toRemove);
 }
Exemple #5
0
 public <E extends CtElement> E addAnnotation(CtAnnotation<? extends Annotation> annotation) {
   if ((List<?>) this.annotations == (List<?>) emptyList()) {
     this.annotations =
         new ArrayList<CtAnnotation<? extends Annotation>>(ANNOTATIONS_CONTAINER_DEFAULT_CAPACITY);
   }
   annotation.setParent(this);
   this.annotations.add(annotation);
   return (E) this;
 }
  private IAnnotationModel processAnnotation(CtAnnotation<? extends Annotation> annotation) {

    String simpleName = annotation.getActualAnnotation().annotationType().getSimpleName();

    AnnotationModel annotationModel = new AnnotationModel();
    annotationModel.setName(simpleName);

    Map<String, Object> elementValues = annotation.getElementValues();

    for (Map.Entry<String, Object> entry : elementValues.entrySet()) {

      String key = entry.getKey();
      if (key == null) {
        continue;
      }

      Object value = entry.getValue();
      ArrayList<CtAnnotation<?>> annotationList = toCtAnnotationList(value);
      if (annotationList != null) {
        int size = annotationList.size();
        IAnnotationModel[] annotationModels = new IAnnotationModel[size];
        for (int i = 0; i < size; i++) {
          CtAnnotation<?> subAnnotation = annotationList.get(i);
          IAnnotationModel subAnnotationModel = processAnnotation(subAnnotation);
          annotationModels[i] = subAnnotationModel;
        }
        annotationModel.addValue(key, annotationModels);
      } else if (value instanceof String[]) {
        annotationModel.addValue(key, value);
      } else {
        if (value instanceof CtNewArray<?>) {
          List<?> elements = ((CtNewArray<?>) value).getElements();
          int size = elements.size();
          Object[] arr = new Object[size];
          for (int i = 0; i < size; i++) {
            Object elem = elements.get(i);
            if (elem instanceof CtCodeElement) {
              PartialEvaluator eval = factory.Eval().createPartialEvaluator();
              arr[i] = eval.evaluate(null, (CtCodeElement) elem);
            } else {
              arr[i] = elem;
            }
          }
          value = arr;
        }
        if (value instanceof CtCodeElement) {
          PartialEvaluator eval = factory.Eval().createPartialEvaluator();
          value = eval.evaluate(null, (CtCodeElement) value);
        }

        if (value instanceof CtLiteral<?>) {
          value = ((CtLiteral<?>) value).getValue().toString();
        } else if (value instanceof CtFieldReference<?>) {
          Member member = ((CtFieldReference<?>) value).getActualField();

          // if field references a static final String, use string's value
          if (member instanceof Field) {
            Field field = (Field) member;

            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) && Modifier.isFinal(mod)) {
              field.setAccessible(true);
              try {
                value = field.get(null).toString();
              } catch (Throwable t) {
                value = member.getName();
                // NOOP tolerate any exception/error, and fall back to using name of field below
              }
            }
            // fall back to using name of field reference
            else {
              value = member.getName();
            }
          }

        } else if (value.getClass().isArray()) {
          int length = Array.getLength(value);
          String[] arr = new String[length];
          for (int i = 0; i < length; i++) {

            Object elem = Array.get(value, i);
            String sVal = elem.toString();
            if (elem instanceof CtLiteral<?>) {
              sVal = ((CtLiteral<?>) elem).getValue().toString();
            } else if (elem instanceof CtFieldReference<?>) {
              sVal = ((CtFieldReference<?>) elem).getActualField().getName();
            }
            arr[i] = sVal;
          }
          value = arr;
        } else {
          value = value.toString();
        }
        if (value == null) {
          value = "null";
        }
        annotationModel.addValue(key, value);
      }
    }
    return annotationModel;
  }