示例#1
0
 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);
 }
示例#2
0
  private static <T> void processOverridden(
      CtClass<?> mergeInto, CtClass<?> toMerge, final CtMethod<T> methodToMerge) {
    List<CtInvocation<T>> superInvocations =
        mergeInto.getElements(
            new Filter<CtInvocation<T>>() {
              @Override
              public boolean matches(CtInvocation<T> invocation) {
                if (!(invocation.getTarget() instanceof CtSuperAccess)) return false;
                CtExecutable<?> m = invocation.getExecutable().getDeclaration();
                return m != null && MethodNode.overrides((CtMethod<?>) m, methodToMerge);
              }
            });

    methodToMerge.setSimpleName(classPrefixedName(toMerge, methodToMerge));
    methodToMerge.setVisibility(PRIVATE);
    removeAnnotation(methodToMerge, Override.class);

    for (CtInvocation<T> superInvocation : superInvocations) {
      superInvocation.setTarget(null);
      superInvocation.setExecutable(methodToMerge.getReference());
    }
    add(mergeInto, methodToMerge, mergeInto::addMethod);
  }
示例#3
0
  private static <T> void processMethod(
      CtClass<?> mergeInto, CtClass<?> toMerge, CtMethod<T> methodToMerge) {
    if (methodToMerge.hasModifier(STATIC)) {
      add(mergeInto, methodToMerge, mergeInto::addMethod);
      return;
    }
    if (methodToMerge.hasModifier(ABSTRACT)) return;

    Optional<CtMethod<?>> overridingMethod =
        mergeInto
            .getMethods()
            .stream()
            .filter(m -> MethodNode.overrides(m, methodToMerge))
            .findFirst();
    if (overridingMethod.isPresent()) {
      @SuppressWarnings("unchecked")
      CtMethod<T> overriding = (CtMethod<T>) overridingMethod.get();
      boolean shouldRemoveAnn = methodToMerge.getAnnotation(Override.class) == null;
      if (!overriding.hasModifier(ABSTRACT)) processOverridden(mergeInto, toMerge, methodToMerge);
      if (shouldRemoveAnn) removeAnnotation(overriding, Override.class);
    } else {
      add(mergeInto, methodToMerge, mergeInto::addMethod);
    }
  }
示例#4
0
 static String classPrefixedName(CtClass<?> ctClass, CtMethod<?> method) {
   return "_" + ctClass.getSimpleName() + "_" + method.getSimpleName();
 }