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); }
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); }
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); } }
static String classPrefixedName(CtClass<?> ctClass, CtMethod<?> method) { return "_" + ctClass.getSimpleName() + "_" + method.getSimpleName(); }