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 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); } }