private void resolveUpperBoundsFromWhereClause(Set<JetType> upperBounds) { JetClassOrObject classOrObject = JetStubbedPsiUtil.getPsiOrStubParent(jetTypeParameter, JetClassOrObject.class, true); if (classOrObject instanceof JetClass) { JetClass jetClass = (JetClass) classOrObject; for (JetTypeConstraint jetTypeConstraint : jetClass.getTypeConstraints()) { DescriptorResolver.reportUnsupportedClassObjectConstraint( resolveSession.getTrace(), jetTypeConstraint); JetSimpleNameExpression constrainedParameterName = jetTypeConstraint.getSubjectTypeParameterName(); if (constrainedParameterName != null) { if (getName().equals(constrainedParameterName.getReferencedNameAsName())) { resolveSession .getTrace() .record(BindingContext.REFERENCE_TARGET, constrainedParameterName, this); JetTypeReference boundTypeReference = jetTypeConstraint.getBoundTypeReference(); if (boundTypeReference != null) { JetType boundType = resolveBoundType(boundTypeReference); if (!jetTypeConstraint.isClassObjectConstraint()) { upperBounds.add(boundType); } } } } } } }
private void processPrimaryConstructor(MutableClassDescriptor classDescriptor, JetClass klass) { if (classDescriptor.getKind() == ClassKind.TRAIT) { JetParameterList primaryConstructorParameterList = klass.getPrimaryConstructorParameterList(); if (primaryConstructorParameterList != null) { context.getTrace().report(CONSTRUCTOR_IN_TRAIT.on(primaryConstructorParameterList)); } if (!klass.hasPrimaryConstructor()) return; } // TODO : not all the parameters are real properties JetScope memberScope = classDescriptor.getScopeForSupertypeResolution(); ConstructorDescriptor constructorDescriptor = context .getDescriptorResolver() .resolvePrimaryConstructorDescriptor(memberScope, classDescriptor, klass); for (JetParameter parameter : klass.getPrimaryConstructorParameters()) { if (parameter.getValOrVarNode() != null) { PropertyDescriptor propertyDescriptor = context .getDescriptorResolver() .resolvePrimaryConstructorParameterToAProperty( classDescriptor, memberScope, parameter); classDescriptor.addPropertyDescriptor(propertyDescriptor); context.getPrimaryConstructorParameterProperties().add(propertyDescriptor); } } if (constructorDescriptor != null) { classDescriptor.setPrimaryConstructor(constructorDescriptor, context.getTrace()); } }
@NotNull private MutableClassDescriptor createClassDescriptorForClass( @NotNull JetClass klass, @NotNull DeclarationDescriptor containingDeclaration) { ClassKind kind = getClassKind(klass); // Kind check is needed in order to not consider enums as inner in any case // (otherwise it would be impossible to create a class object in the enum) boolean isInner = kind == ClassKind.CLASS && klass.isInner(); MutableClassDescriptor mutableClassDescriptor = new MutableClassDescriptor( containingDeclaration, outerScope, kind, isInner, JetPsiUtil.safeName(klass.getName())); c.getClasses().put(klass, mutableClassDescriptor); trace.record( FQNAME_TO_CLASS_DESCRIPTOR, JetPsiUtil.getUnsafeFQName(klass), mutableClassDescriptor); createClassObjectForEnumClass(mutableClassDescriptor); JetScope classScope = mutableClassDescriptor.getScopeForMemberDeclarationResolution(); prepareForDeferredCall(classScope, mutableClassDescriptor, klass); return mutableClassDescriptor; }
@NotNull private static ClassKind getClassKind(@NotNull JetClass jetClass) { if (jetClass.isTrait()) return ClassKind.TRAIT; if (jetClass.isAnnotation()) return ClassKind.ANNOTATION_CLASS; if (jetClass.isEnum()) return ClassKind.ENUM_CLASS; return ClassKind.CLASS; }
private void resolveConstructorHeaders() { for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { JetClass jetClass = entry.getKey(); MutableClassDescriptor classDescriptor = entry.getValue(); processPrimaryConstructor(classDescriptor, jetClass); for (JetSecondaryConstructor jetConstructor : jetClass.getSecondaryConstructors()) { processSecondaryConstructor(classDescriptor, jetConstructor); } } }
private void resolvePropertyDeclarationBodies() { // Member properties Set<JetProperty> processed = Sets.newHashSet(); for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { JetClass jetClass = entry.getKey(); if (!context.completeAnalysisNeeded(jetClass)) continue; MutableClassDescriptor classDescriptor = entry.getValue(); for (JetProperty property : jetClass.getProperties()) { final PropertyDescriptor propertyDescriptor = this.context.getProperties().get(property); assert propertyDescriptor != null; computeDeferredType(propertyDescriptor.getReturnType()); JetExpression initializer = property.getInitializer(); if (initializer != null) { ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor(); if (primaryConstructor != null) { JetScope declaringScopeForPropertyInitializer = this.context.getDeclaringScopes().get(property); resolvePropertyInitializer( property, propertyDescriptor, initializer, declaringScopeForPropertyInitializer); } } resolvePropertyAccessors(property, propertyDescriptor); processed.add(property); } } // Top-level properties & properties of objects for (Map.Entry<JetProperty, PropertyDescriptor> entry : this.context.getProperties().entrySet()) { JetProperty property = entry.getKey(); if (!context.completeAnalysisNeeded(property)) continue; if (processed.contains(property)) continue; final PropertyDescriptor propertyDescriptor = entry.getValue(); computeDeferredType(propertyDescriptor.getReturnType()); JetScope declaringScope = this.context.getDeclaringScopes().get(property); JetExpression initializer = property.getInitializer(); if (initializer != null) { resolvePropertyInitializer(property, propertyDescriptor, initializer, declaringScope); } resolvePropertyAccessors(property, propertyDescriptor); } }
@Override public Icon getIcon(@NotNull PsiElement psiElement, int flags) { if (psiElement instanceof JetFile) { JetFile file = (JetFile) psiElement; JetClassOrObject mainClass = getMainClass(file); return mainClass != null ? getIcon(mainClass, flags) : JetIcons.FILE; } if (psiElement instanceof JetNamespaceHeader) { return (flags & Iconable.ICON_FLAG_OPEN) != 0 ? PlatformIcons.PACKAGE_OPEN_ICON : PlatformIcons.PACKAGE_ICON; } if (psiElement instanceof JetNamedFunction) { return PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass ? PlatformIcons.METHOD_ICON : JetIcons.FUNCTION; } if (psiElement instanceof JetClass) { JetClass jetClass = (JetClass) psiElement; if (jetClass.isTrait()) { return JetIcons.TRAIT; } Icon icon = jetClass.hasModifier(JetTokens.ENUM_KEYWORD) ? PlatformIcons.ENUM_ICON : JetIcons.CLASS; if (jetClass instanceof JetEnumEntry) { JetEnumEntry enumEntry = (JetEnumEntry) jetClass; if (enumEntry.getPrimaryConstructorParameterList() == null) { icon = PlatformIcons.ENUM_ICON; } } return icon; } if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) { return JetIcons.OBJECT; } if (psiElement instanceof JetParameter) { JetParameter parameter = (JetParameter) psiElement; if (parameter.getValOrVarNode() != null) { JetParameterList parameterList = PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class); if (parameterList != null && parameterList.getParent() instanceof JetClass) { return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL; } } return JetIcons.PARAMETER; } if (psiElement instanceof JetProperty) { JetProperty property = (JetProperty) psiElement; return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL; } return null; }
private void resolvePrimaryConstructorParameters() { for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { JetClass klass = entry.getKey(); MutableClassDescriptor classDescriptor = entry.getValue(); ConstructorDescriptor unsubstitutedPrimaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor(); if (unsubstitutedPrimaryConstructor != null) { checkDefaultParameterValues( klass.getPrimaryConstructorParameters(), unsubstitutedPrimaryConstructor.getValueParameters(), classDescriptor.getScopeForInitializers()); } } }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { if (!super.isAvailable(project, editor, file)) { return false; } BindingContext context = KotlinCacheManager.getInstance(project).getDeclarationsFromProject().getBindingContext(); AnnotationDescriptor annotationDescriptor = context.get(BindingContext.ANNOTATION, annotationEntry); if (annotationDescriptor == null) { return false; } annotationType = annotationDescriptor.getType(); DeclarationDescriptor declarationDescriptor = annotationType.getConstructor().getDeclarationDescriptor(); if (declarationDescriptor == null) { return false; } PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, declarationDescriptor); if (declaration instanceof JetClass) { annotationClass = (JetClass) declaration; return annotationClass.isWritable(); } return false; }
private void processPrimaryConstructor( @NotNull TopDownAnalysisContext c, @NotNull MutableClassDescriptor classDescriptor, @NotNull JetClass klass) { // TODO : not all the parameters are real properties JetScope memberScope = classDescriptor.getScopeForClassHeaderResolution(); ConstructorDescriptor constructorDescriptor = descriptorResolver.resolvePrimaryConstructorDescriptor( memberScope, classDescriptor, klass, trace); if (constructorDescriptor != null) { List<ValueParameterDescriptor> valueParameterDescriptors = constructorDescriptor.getValueParameters(); List<JetParameter> primaryConstructorParameters = klass.getPrimaryConstructorParameters(); assert valueParameterDescriptors.size() == primaryConstructorParameters.size(); List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>(); for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) { JetParameter parameter = primaryConstructorParameters.get(valueParameterDescriptor.getIndex()); if (parameter.getValOrVarNode() != null) { PropertyDescriptor propertyDescriptor = descriptorResolver.resolvePrimaryConstructorParameterToAProperty( classDescriptor, valueParameterDescriptor, memberScope, parameter, trace); classDescriptor.getBuilder().addPropertyDescriptor(propertyDescriptor); c.getPrimaryConstructorParameterProperties().put(parameter, propertyDescriptor); } else { notProperties.add(valueParameterDescriptor); } } if (classDescriptor.getKind() != ClassKind.TRAIT) { classDescriptor.setPrimaryConstructor(constructorDescriptor); classDescriptor.addConstructorParametersToInitializersScope(notProperties); } } }
private void resolveFunctionAndPropertyHeaders() { for (Map.Entry<JetFile, WritableScope> entry : context.getNamespaceScopes().entrySet()) { JetFile namespace = entry.getKey(); WritableScope namespaceScope = entry.getValue(); NamespaceLike namespaceDescriptor = context.getNamespaceDescriptors().get(namespace); resolveFunctionAndPropertyHeaders( namespace.getDeclarations(), namespaceScope, namespaceScope, namespaceScope, namespaceDescriptor); } for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) { JetClass jetClass = entry.getKey(); MutableClassDescriptor classDescriptor = entry.getValue(); resolveFunctionAndPropertyHeaders( jetClass.getDeclarations(), classDescriptor.getScopeForMemberResolution(), classDescriptor.getScopeForInitializers(), classDescriptor.getScopeForMemberResolution(), classDescriptor); // processPrimaryConstructor(classDescriptor, jetClass); // for (JetSecondaryConstructor jetConstructor : // jetClass.getSecondaryConstructors()) { // processSecondaryConstructor(classDescriptor, jetConstructor); // } } for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) { JetObjectDeclaration object = entry.getKey(); MutableClassDescriptor classDescriptor = entry.getValue(); resolveFunctionAndPropertyHeaders( object.getDeclarations(), classDescriptor.getScopeForMemberResolution(), classDescriptor.getScopeForInitializers(), classDescriptor.getScopeForMemberResolution(), classDescriptor); } // TODO : Extensions }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { annotationClass.replace( AddModifierFix.addModifier(annotationClass, ANNOTATION_KEYWORD, null, project, false)); }
public static Icon getBaseIcon(PsiElement psiElement) { if (psiElement instanceof JetNamespaceHeader) { return PlatformIcons.PACKAGE_ICON; } if (psiElement instanceof KotlinLightClassForPackage) { return JetIcons.FILE; } if (psiElement instanceof KotlinLightClassForExplicitDeclaration) { psiElement = psiElement.getNavigationElement(); } if (psiElement instanceof JetNamedFunction) { if (((JetFunction) psiElement).getReceiverTypeRef() != null) { return JetIcons.EXTENSION_FUNCTION; } if (PsiTreeUtil.getParentOfType(psiElement, JetNamedDeclaration.class) instanceof JetClass) { if (JetPsiUtil.isAbstract((JetFunction) psiElement)) { return PlatformIcons.ABSTRACT_METHOD_ICON; } else { return PlatformIcons.METHOD_ICON; } } else { return JetIcons.FUNCTION; } } if (psiElement instanceof JetClass) { JetClass jetClass = (JetClass) psiElement; if (jetClass.isTrait()) { return JetIcons.TRAIT; } Icon icon = jetClass.isEnum() ? PlatformIcons.ENUM_ICON : JetIcons.CLASS; if (jetClass instanceof JetEnumEntry) { JetEnumEntry enumEntry = (JetEnumEntry) jetClass; if (enumEntry.getPrimaryConstructorParameterList() == null) { icon = PlatformIcons.ENUM_ICON; } } return icon; } if (psiElement instanceof JetObjectDeclaration || psiElement instanceof JetClassObject) { return JetIcons.OBJECT; } if (psiElement instanceof JetParameter) { JetParameter parameter = (JetParameter) psiElement; if (parameter.getValOrVarNode() != null) { JetParameterList parameterList = PsiTreeUtil.getParentOfType(psiElement, JetParameterList.class); if (parameterList != null && parameterList.getParent() instanceof JetClass) { return parameter.isMutable() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL; } } return JetIcons.PARAMETER; } if (psiElement instanceof JetProperty) { JetProperty property = (JetProperty) psiElement; return property.isVar() ? JetIcons.FIELD_VAR : JetIcons.FIELD_VAL; } return null; }