private static void collectOverriddenDeclarations( @NotNull CallableMemberDescriptor descriptor, @NotNull Set<CallableMemberDescriptor> result) { if (descriptor.getKind().isReal()) { result.add(descriptor); } else { if (descriptor.getOverriddenDescriptors().isEmpty()) { throw new IllegalStateException( "No overridden descriptors found for (fake override) " + descriptor); } for (CallableMemberDescriptor overridden : descriptor.getOverriddenDescriptors()) { collectOverriddenDeclarations(overridden, result); } } }
private void renderOverride( @NotNull CallableMemberDescriptor callableMember, @NotNull StringBuilder builder) { if (!modifiers.contains(Modifier.OVERRIDE)) return; if (overridesSomething(callableMember)) { if (overrideRenderingPolicy != OverrideRenderingPolicy.RENDER_OPEN) { builder.append("override "); if (verbose) { builder .append("/*") .append(callableMember.getOverriddenDescriptors().size()) .append("*/ "); } } } }
private void checkDeclaredTypeInPublicMember( JetNamedDeclaration member, CallableMemberDescriptor memberDescriptor) { boolean hasDeferredType; if (member instanceof JetProperty) { hasDeferredType = ((JetProperty) member).getTypeReference() == null && DescriptorResolver.hasBody((JetProperty) member); } else { assert member instanceof JetFunction; JetFunction function = (JetFunction) member; hasDeferredType = function.getTypeReference() == null && function.hasBody() && !function.hasBlockBody(); } if ((memberDescriptor.getVisibility().isPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) { trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member)); } }
public static void resolveUnknownVisibilityForMember( @NotNull CallableMemberDescriptor memberDescriptor, @Nullable Function1<CallableMemberDescriptor, Unit> cannotInferVisibility) { for (CallableMemberDescriptor descriptor : memberDescriptor.getOverriddenDescriptors()) { if (descriptor.getVisibility() == Visibilities.INHERITED) { resolveUnknownVisibilityForMember(descriptor, cannotInferVisibility); } } if (memberDescriptor.getVisibility() != Visibilities.INHERITED) { return; } Visibility maxVisibility = computeVisibilityToInherit(memberDescriptor); Visibility visibilityToInherit; if (maxVisibility == null) { if (cannotInferVisibility != null) { cannotInferVisibility.invoke(memberDescriptor); } visibilityToInherit = Visibilities.PUBLIC; } else { visibilityToInherit = maxVisibility; } if (memberDescriptor instanceof PropertyDescriptorImpl) { ((PropertyDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit); for (PropertyAccessorDescriptor accessor : ((PropertyDescriptor) memberDescriptor).getAccessors()) { // If we couldn't infer visibility for property, the diagnostic is already reported, no need // to report it again on accessors resolveUnknownVisibilityForMember( accessor, maxVisibility == null ? null : cannotInferVisibility); } } else if (memberDescriptor instanceof FunctionDescriptorImpl) { ((FunctionDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit); } else { assert memberDescriptor instanceof PropertyAccessorDescriptorImpl; ((PropertyAccessorDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit); } }
@Nullable private static Visibility computeVisibilityToInherit( @NotNull CallableMemberDescriptor memberDescriptor) { Collection<? extends CallableMemberDescriptor> overriddenDescriptors = memberDescriptor.getOverriddenDescriptors(); Visibility maxVisibility = findMaxVisibility(overriddenDescriptors); if (maxVisibility == null) { return null; } if (memberDescriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { for (CallableMemberDescriptor overridden : overriddenDescriptors) { // An implementation (a non-abstract overridden member) of a fake override should have the // maximum possible visibility if (overridden.getModality() != Modality.ABSTRACT && !overridden.getVisibility().equals(maxVisibility)) { return null; } } return maxVisibility; } return maxVisibility.normalize(); }
private static void createAndBindFakeOverride( @NotNull Collection<CallableMemberDescriptor> overridables, @NotNull ClassDescriptor current, @NotNull OverridingStrategy strategy) { Collection<CallableMemberDescriptor> visibleOverridables = filterVisibleFakeOverrides(current, overridables); boolean allInvisible = visibleOverridables.isEmpty(); Collection<CallableMemberDescriptor> effectiveOverridden = allInvisible ? overridables : visibleOverridables; Modality modality = determineModality(effectiveOverridden); Visibility visibility = allInvisible ? Visibilities.INVISIBLE_FAKE : Visibilities.INHERITED; // FIXME doesn't work as expected for flexible types: should create a refined signature. // Current algorithm produces bad results in presence of annotated Java signatures such as: // J: foo(s: String!): String -- @NotNull String foo(String s); // K: foo(s: String): String? // --> 'foo(s: String!): String' as an inherited signature with most specific return type. // This is bad because it can be overridden by 'foo(s: String?): String', which is not // override-equivalent with K::foo above. // Should be 'foo(s: String): String'. CallableMemberDescriptor mostSpecific = selectMostSpecificMember( effectiveOverridden, new Function1<CallableMemberDescriptor, CallableDescriptor>() { @Override public CallableMemberDescriptor invoke(CallableMemberDescriptor descriptor) { return descriptor; } }); CallableMemberDescriptor fakeOverride = mostSpecific.copy( current, modality, visibility, CallableMemberDescriptor.Kind.FAKE_OVERRIDE, false); strategy.setOverriddenDescriptors(fakeOverride, effectiveOverridden); assert !fakeOverride.getOverriddenDescriptors().isEmpty() : "Overridden descriptors should be set for " + CallableMemberDescriptor.Kind.FAKE_OVERRIDE; strategy.addFakeOverride(fakeOverride); }
public static boolean isOverride(@NotNull CallableMemberDescriptor descriptor) { return !descriptor.getOverriddenDescriptors().isEmpty(); }
private boolean overridesSomething(CallableMemberDescriptor callable) { return !callable.getOverriddenDescriptors().isEmpty(); }