private static <T> void mergeStagedChainInner(List<CtClass<T>> chain) {
   if (chain.size() == 1) return;
   reverse(chain);
   CtClass<T> toMerge = chain.get(0);
   for (int i = 1; i < chain.size(); i++) {
     CtClass<T> mergeInto = chain.get(i);
     replaceInstantiatedTypeParams(toMerge, mergeInto);
     toMerge
         .getAnnotations()
         .stream()
         .forEach(
             (CtAnnotation<? extends Annotation> a) -> {
               if (mergeInto.getAnnotation(a.getActualAnnotation().getClass()) == null)
                 add(mergeInto, a, mergeInto::addAnnotation);
             });
     toMerge.getSuperInterfaces().forEach(mergeInto::addSuperInterface);
     toMerge
         .getAnonymousExecutables()
         .forEach(b -> add(mergeInto, b, mergeInto::addAnonymousExecutable));
     toMerge.getNestedTypes().forEach(nt -> add(mergeInto, nt, mergeInto::addNestedType));
     toMerge.getFields().forEach(f -> add(mergeInto, f, mergeInto::addField));
     for (CtMethod<?> methodToMerge : toMerge.getMethods()) {
       processMethod(mergeInto, toMerge, methodToMerge);
     }
     final CtClass<T> finalToMerge = toMerge;
     mergeInto.getConstructors().forEach(c -> processConstructor(c, finalToMerge));
     mergeInto.setSuperclass(toMerge.getSuperclass());
     toMerge = mergeInto;
   }
 }
  private static <T> void replaceInstantiatedTypeParams(CtClass<T> toMerge, CtClass<T> mergeInto) {
    List<CtTypeReference<?>> typeArgs = mergeInto.getSuperclass().getActualTypeArguments();
    typeArgs
        .stream()
        .filter(ta -> !(ta instanceof CtTypeParameterReference))
        .forEach(
            ta -> {
              int instantiatedParamIndex = typeArgs.indexOf(ta);
              CtTypeReference<?> instantiatedTypeParam =
                  toMerge.getFormalTypeParameters().get(instantiatedParamIndex);

              toMerge.accept(
                  new CtScanner() {

                    @Override
                    public void scan(CtReference ref) {
                      if (ref instanceof CtGenericElementReference) {
                        CtGenericElementReference gRef = (CtGenericElementReference) ref;
                        replaceInList(ref.getFactory(), gRef.getActualTypeArguments());
                      }
                      if (ref instanceof CtTypeParameterReference) {
                        replaceInList(
                            ref.getFactory(), ((CtTypeParameterReference) ref).getBounds());
                      }
                      super.scan(ref);
                    }

                    @Override
                    public void scan(CtElement element) {
                      if (element instanceof CtTypedElement) {
                        CtTypedElement typed = (CtTypedElement) element;
                        CtTypeReference type = typed.getType();
                        if (type != null
                            && instantiatedTypeParam.getSimpleName().equals(type.getSimpleName())) {
                          typed.setType(element.getFactory().Core().clone(ta));
                        }
                      }
                      if (element instanceof CtExpression) {
                        replaceInList(
                            element.getFactory(), ((CtExpression) element).getTypeCasts());
                      }
                      super.scan(element);
                    }

                    private void replaceInList(Factory f, List<CtTypeReference<?>> types) {
                      for (int i = 0; i < types.size(); i++) {
                        CtTypeReference<?> arg = types.get(i);
                        if (instantiatedTypeParam.getSimpleName().equals(arg.getSimpleName())) {
                          types.set(i, f.Core().clone(ta));
                        }
                      }
                    }
                  });
            });
  }