/** * Given a fake override, finds any declaration of it in the overridden descriptors. Keep in mind * that there may be many declarations of the fake override in the supertypes, this method finds * just the only one. TODO: probably all call-sites of this method are wrong, they should handle * all super-declarations */ @NotNull @SuppressWarnings("unchecked") public static <D extends CallableMemberDescriptor> D unwrapFakeOverride(@NotNull D descriptor) { while (descriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { Collection<? extends CallableMemberDescriptor> overridden = descriptor.getOverriddenDescriptors(); if (overridden.isEmpty()) { throw new IllegalStateException( "Fake override should have at least one overridden descriptor: " + descriptor); } descriptor = (D) overridden.iterator().next(); } return descriptor; }
@NotNull @SuppressWarnings("unchecked") public static <D extends CallableDescriptor> Set<D> getAllOverriddenDescriptors(@NotNull D f) { Set<D> result = new LinkedHashSet<D>(); collectAllOverriddenDescriptors((D) f.getOriginal(), result); return result; }
private static <D extends CallableDescriptor> void collectAllOverriddenDescriptors( @NotNull D current, @NotNull Set<D> result) { if (result.contains(current)) return; for (CallableDescriptor callableDescriptor : current.getOriginal().getOverriddenDescriptors()) { @SuppressWarnings("unchecked") D descriptor = (D) callableDescriptor; collectAllOverriddenDescriptors(descriptor, result); result.add(descriptor); } }
@NotNull @SuppressWarnings("unchecked") public static <D extends CallableMemberDescriptor> Set<D> getAllOverriddenDeclarations( @NotNull D memberDescriptor) { Set<D> result = new HashSet<D>(); for (CallableMemberDescriptor overriddenDeclaration : memberDescriptor.getOverriddenDescriptors()) { CallableMemberDescriptor.Kind kind = overriddenDeclaration.getKind(); if (kind == DECLARATION) { result.add((D) overriddenDeclaration); } else if (kind == DELEGATION || kind == FAKE_OVERRIDE || kind == SYNTHESIZED) { // do nothing } else { throw new AssertionError("Unexpected callable kind " + kind); } result.addAll(getAllOverriddenDeclarations((D) overriddenDeclaration)); } return result; }