@Nullable public static ClassDescriptor getContainingClass(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containing = descriptor.getContainingDeclaration(); while (containing != null) { if (containing instanceof ClassDescriptor && !isCompanionObject(containing)) { return (ClassDescriptor) containing; } containing = containing.getContainingDeclaration(); } return null; }
private static String getJvmInternalFQNameImpl( BindingTrace bindingTrace, DeclarationDescriptor descriptor) { if (descriptor instanceof FunctionDescriptor) { throw new IllegalStateException("requested fq name for function: " + descriptor); } if (descriptor.getContainingDeclaration() instanceof ModuleDescriptor || descriptor instanceof ScriptDescriptor) { return ""; } if (descriptor instanceof ModuleDescriptor) { throw new IllegalStateException("missed something"); } if (descriptor instanceof ClassDescriptor) { ClassDescriptor klass = (ClassDescriptor) descriptor; if (klass.getKind() == ClassKind.OBJECT || klass.getKind() == ClassKind.CLASS_OBJECT) { if (klass.getContainingDeclaration() instanceof ClassDescriptor) { ClassDescriptor containingKlass = (ClassDescriptor) klass.getContainingDeclaration(); if (containingKlass.getKind() == ClassKind.ENUM_CLASS) { return getJvmInternalName(bindingTrace, containingKlass).getInternalName(); } else { return getJvmInternalName(bindingTrace, containingKlass).getInternalName() + JvmAbi.CLASS_OBJECT_SUFFIX; } } } JvmClassName name = bindingTrace.getBindingContext().get(FQN, descriptor); if (name != null) { return name.getInternalName(); } } DeclarationDescriptor container = descriptor.getContainingDeclaration(); if (container == null) { throw new IllegalStateException("descriptor has no container: " + descriptor); } Name name = descriptor.getName(); String baseName = getJvmInternalName(bindingTrace, container).getInternalName(); if (!baseName.isEmpty()) { return baseName + (container instanceof NamespaceDescriptor ? "/" : "$") + name.getIdentifier(); } return name.getIdentifier(); }
public static boolean isAncestor( @Nullable DeclarationDescriptor ancestor, @NotNull DeclarationDescriptor declarationDescriptor, boolean strict) { if (ancestor == null) return false; DeclarationDescriptor descriptor = strict ? declarationDescriptor.getContainingDeclaration() : declarationDescriptor; while (descriptor != null) { if (ancestor == descriptor) return true; descriptor = descriptor.getContainingDeclaration(); } return false; }
public static CodegenContext getContext(DeclarationDescriptor descriptor, GenerationState state) { if (descriptor instanceof PackageFragmentDescriptor) { return new PackageContext( (PackageFragmentDescriptor) descriptor, state.getRootContext(), null); } CodegenContext parent = getContext(descriptor.getContainingDeclaration(), state); if (descriptor instanceof ClassDescriptor) { OwnerKind kind = DescriptorUtils.isInterface(descriptor) ? OwnerKind.DEFAULT_IMPLS : OwnerKind.IMPLEMENTATION; return parent.intoClass((ClassDescriptor) descriptor, kind, state); } else if (descriptor instanceof ScriptDescriptor) { ClassDescriptor classDescriptorForScript = state.getBindingContext().get(CLASS_FOR_SCRIPT, (ScriptDescriptor) descriptor); assert classDescriptorForScript != null : "Can't find class for script: " + descriptor; List<ScriptDescriptor> earlierScripts = state.getEarlierScriptsForReplInterpreter(); return parent.intoScript( (ScriptDescriptor) descriptor, earlierScripts == null ? Collections.emptyList() : earlierScripts, classDescriptorForScript); } else if (descriptor instanceof FunctionDescriptor) { return parent.intoFunction((FunctionDescriptor) descriptor); } throw new IllegalStateException("Couldn't build context for " + descriptorName(descriptor)); }
private String renderDefaultType(JetType type, boolean shortNamesOnly) { StringBuilder sb = new StringBuilder(); ClassifierDescriptor cd = type.getConstructor().getDeclarationDescriptor(); Object typeNameObject; if (cd == null || cd instanceof TypeParameterDescriptor) { typeNameObject = type.getConstructor(); } else { if (shortNamesOnly) { // for nested classes qualified name should be used typeNameObject = cd.getName(); DeclarationDescriptor parent = cd.getContainingDeclaration(); while (parent instanceof ClassDescriptor) { typeNameObject = parent.getName() + "." + typeNameObject; parent = parent.getContainingDeclaration(); } } else { typeNameObject = DescriptorUtils.getFQName(cd); } } sb.append(typeNameObject); if (!type.getArguments().isEmpty()) { sb.append("<"); appendTypeProjections(sb, type.getArguments(), shortNamesOnly); sb.append(">"); } if (type.isNullable()) { sb.append("?"); } return sb.toString(); }
@Nullable @SuppressWarnings("unchecked") public static <D extends DeclarationDescriptor> D getParentOfType( @Nullable DeclarationDescriptor descriptor, @NotNull Class<D> aClass, boolean strict) { if (descriptor == null) return null; if (strict) { descriptor = descriptor.getContainingDeclaration(); } while (descriptor != null) { if (aClass.isInstance(descriptor)) { return (D) descriptor; } descriptor = descriptor.getContainingDeclaration(); } return null; }
@NotNull private static FqNameUnsafe getFqNameUnsafe(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); assert containingDeclaration != null : "Not package/module descriptor doesn't have containing declaration: " + descriptor; return getFqName(containingDeclaration).child(descriptor.getName()); }
private static boolean isIllegalNestedClass(@NotNull DeclarationDescriptor descriptor) { if (!(descriptor instanceof ClassDescriptor)) return false; DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); if (!(containingDeclaration instanceof ClassDescriptor)) return false; ClassDescriptor containingClass = (ClassDescriptor) containingDeclaration; return containingClass.isInner() || containingClass.getContainingDeclaration() instanceof FunctionDescriptor; }
@NotNull public static FqName getFqNameFromTopLevelClass(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); Name name = descriptor.getName(); if (!(containingDeclaration instanceof ClassDescriptor)) { return FqName.topLevel(name); } return getFqNameFromTopLevelClass(containingDeclaration).child(name); }
/** Descriptor may be local itself or have a local ancestor */ public static boolean isLocal(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor current = descriptor; while (current != null) { if (isAnonymousObject(current) || isDescriptorWithLocalVisibility(current)) { return true; } current = current.getContainingDeclaration(); } return false; }
/* METHODS FOR ALL KINDS OF DESCRIPTORS */ private void appendDefinedIn( @NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) { if (descriptor instanceof ModuleDescriptor) { builder.append(" is a module"); return; } builder.append(" ").append(renderMessage("defined in")).append(" "); DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); if (containingDeclaration != null) { FqNameUnsafe fqName = DescriptorUtils.getFQName(containingDeclaration); builder.append(FqName.ROOT.equalsTo(fqName) ? "root package" : renderFqName(fqName)); } }
private void appendDescriptor(DeclarationDescriptor descriptor, String indent) { int startOffset = myBuilder.length(); myBuilder.append(DescriptorRenderer.COMPACT.render(descriptor)); int endOffset = myBuilder.length(); if (descriptor instanceof FunctionDescriptor || descriptor instanceof PropertyDescriptor) { if (((CallableMemberDescriptor) descriptor).getModality() != Modality.ABSTRACT) { if (descriptor instanceof FunctionDescriptor) { myBuilder.append(" { ").append(DECOMPILED_COMMENT).append(" }"); endOffset = myBuilder.length(); } else { // descriptor instanceof PropertyDescriptor if (((PropertyDescriptor) descriptor).getModality() != Modality.ABSTRACT) { myBuilder.append(" ").append(DECOMPILED_COMMENT); } } } } else if (descriptor instanceof ClassDescriptor) { myBuilder.append(" {\n"); ClassDescriptor classDescriptor = (ClassDescriptor) descriptor; boolean firstPassed = false; String subindent = indent + " "; if (classDescriptor.getClassObjectDescriptor() != null) { firstPassed = true; myBuilder.append(subindent).append("class "); appendDescriptor(classDescriptor.getClassObjectDescriptor(), subindent); } for (DeclarationDescriptor member : sortDeclarations(classDescriptor.getDefaultType().getMemberScope().getAllDescriptors())) { if (member.getContainingDeclaration() == descriptor) { if (firstPassed) { myBuilder.append("\n"); } else { firstPassed = true; } myBuilder.append(subindent); appendDescriptor(member, subindent); } } myBuilder.append(indent).append("}"); endOffset = myBuilder.length(); } myBuilder.append("\n"); PsiElement clsMember = myBindingContext.get(BindingContext.DESCRIPTOR_TO_DECLARATION, descriptor); if (clsMember != null) { myClsMembersToRanges.put(clsMember, new TextRange(startOffset, endOffset)); } }
@Nullable public static ModuleDescriptor getContainingModuleOrNull( @NotNull DeclarationDescriptor descriptor) { while (descriptor != null) { if (descriptor instanceof ModuleDescriptor) { return (ModuleDescriptor) descriptor; } if (descriptor instanceof PackageViewDescriptor) { return ((PackageViewDescriptor) descriptor).getModule(); } //noinspection ConstantConditions descriptor = descriptor.getContainingDeclaration(); } return null; }
@NotNull private String renderClassName(@NotNull ClassDescriptor klass) { if (ErrorUtils.isError(klass)) { return klass.getTypeConstructor().toString(); } if (shortNames) { List<Name> qualifiedNameElements = Lists.newArrayList(); // for nested classes qualified name should be used DeclarationDescriptor current = klass; do { if (((ClassDescriptor) current).getKind() != ClassKind.CLASS_OBJECT) { qualifiedNameElements.add(current.getName()); } current = current.getContainingDeclaration(); } while (current instanceof ClassDescriptor); Collections.reverse(qualifiedNameElements); return renderFqName(qualifiedNameElements); } return renderFqName(DescriptorUtils.getFQName(klass)); }
/*descriptor is null for captured vars*/ public boolean shouldPutValue( @NotNull Type type, @Nullable StackValue stackValue, @Nullable ValueParameterDescriptor descriptor) { if (stackValue == null) { // default or vararg return true; } // remap only inline functions (and maybe non primitives) // TODO - clean asserion and remapping logic if (isPrimitive(type) != isPrimitive(stackValue.type)) { // don't remap boxing/unboxing primitives - lost identity and perfomance return true; } if (stackValue instanceof StackValue.Local) { return false; } StackValue field = stackValue; if (stackValue instanceof StackValue.FieldForSharedVar) { field = ((StackValue.FieldForSharedVar) stackValue).receiver; } // check that value corresponds to captured inlining parameter if (field instanceof StackValue.Field) { DeclarationDescriptor varDescriptor = ((StackValue.Field) field).descriptor; // check that variable is inline function parameter return !(varDescriptor instanceof ParameterDescriptor && InlineUtil.isInlineLambdaParameter((ParameterDescriptor) varDescriptor) && InlineUtil.isInline(varDescriptor.getContainingDeclaration())); } return true; }
/** * @return true if descriptor is a class inside another class and does not have access to the * outer class */ public static boolean isStaticNestedClass(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containing = descriptor.getContainingDeclaration(); return descriptor instanceof ClassDescriptor && containing instanceof ClassDescriptor && !((ClassDescriptor) descriptor).isInner(); }
public static boolean isTopLevelDeclaration(@Nullable DeclarationDescriptor descriptor) { return descriptor != null && descriptor.getContainingDeclaration() instanceof PackageFragmentDescriptor; }
private void checkSupertypeList( @NotNull ClassDescriptor supertypeOwner, @NotNull Map<JetTypeReference, JetType> supertypes, @NotNull JetClassOrObject jetClass) { Set<TypeConstructor> allowedFinalSupertypes = getAllowedFinalSupertypes(supertypeOwner, jetClass); Set<TypeConstructor> typeConstructors = Sets.newHashSet(); boolean classAppeared = false; for (Map.Entry<JetTypeReference, JetType> entry : supertypes.entrySet()) { JetTypeReference typeReference = entry.getKey(); JetType supertype = entry.getValue(); boolean addSupertype = true; ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype); if (classDescriptor != null) { if (ErrorUtils.isError(classDescriptor)) continue; if (classDescriptor.getKind() != ClassKind.INTERFACE) { if (supertypeOwner.getKind() == ClassKind.ENUM_CLASS) { trace.report(CLASS_IN_SUPERTYPE_FOR_ENUM.on(typeReference)); addSupertype = false; } else if (supertypeOwner.getKind() == ClassKind.INTERFACE && !classAppeared && !TypesPackage.isDynamic(supertype) /* avoid duplicate diagnostics */) { trace.report(TRAIT_WITH_SUPERCLASS.on(typeReference)); addSupertype = false; } if (classAppeared) { trace.report(MANY_CLASSES_IN_SUPERTYPE_LIST.on(typeReference)); } else { classAppeared = true; } } } else { trace.report(SUPERTYPE_NOT_A_CLASS_OR_TRAIT.on(typeReference)); } TypeConstructor constructor = supertype.getConstructor(); if (addSupertype && !typeConstructors.add(constructor)) { trace.report(SUPERTYPE_APPEARS_TWICE.on(typeReference)); } if (DescriptorUtils.isSingleton(classDescriptor)) { trace.report(SINGLETON_IN_SUPERTYPE.on(typeReference)); } else if (constructor.isFinal() && !allowedFinalSupertypes.contains(constructor)) { if (classDescriptor.getModality() == Modality.SEALED) { DeclarationDescriptor containingDescriptor = supertypeOwner.getContainingDeclaration(); while (containingDescriptor != null && containingDescriptor != classDescriptor) { containingDescriptor = containingDescriptor.getContainingDeclaration(); } if (containingDescriptor == null) { trace.report(SEALED_SUPERTYPE.on(typeReference)); } else { trace.report(SEALED_SUPERTYPE_IN_LOCAL_CLASS.on(typeReference)); } } else { trace.report(FINAL_SUPERTYPE.on(typeReference)); } } } }
public static boolean isError(@NotNull DeclarationDescriptor candidate) { return candidate == getErrorClass() || candidate.getContainingDeclaration() == getErrorClass() || candidate == ERROR_MODULE; }
public boolean isMemberOfAny(@NotNull DeclarationDescriptor descriptor) { return descriptor.getContainingDeclaration() == getAny(); }
private static boolean isRootNs(DeclarationDescriptor ns) { return ns instanceof NamespaceDescriptor && ns.getContainingDeclaration() instanceof ModuleDescriptor; }