@NotNull public static Modality resolveModalityFromModifiers( @Nullable JetModifierList modifierList, @NotNull Modality defaultModality) { if (modifierList == null) return defaultModality; boolean hasAbstractModifier = modifierList.hasModifier(ABSTRACT_KEYWORD); boolean hasOverrideModifier = modifierList.hasModifier(OVERRIDE_KEYWORD); if (modifierList.hasModifier(SEALED_KEYWORD)) { return Modality.SEALED; } if (modifierList.hasModifier(OPEN_KEYWORD)) { if (hasAbstractModifier || defaultModality == Modality.ABSTRACT) { return Modality.ABSTRACT; } return Modality.OPEN; } if (hasAbstractModifier) { return Modality.ABSTRACT; } boolean hasFinalModifier = modifierList.hasModifier(FINAL_KEYWORD); if (hasOverrideModifier && !hasFinalModifier && !(defaultModality == Modality.ABSTRACT)) { return Modality.OPEN; } if (hasFinalModifier) { return Modality.FINAL; } return defaultModality; }
public static Visibility resolveVisibilityFromModifiers( @Nullable JetModifierList modifierList, @NotNull Visibility defaultVisibility) { if (modifierList == null) return defaultVisibility; if (modifierList.hasModifier(PRIVATE_KEYWORD)) return Visibilities.PRIVATE; if (modifierList.hasModifier(PUBLIC_KEYWORD)) return Visibilities.PUBLIC; if (modifierList.hasModifier(PROTECTED_KEYWORD)) return Visibilities.PROTECTED; if (modifierList.hasModifier(INTERNAL_KEYWORD)) return Visibilities.INTERNAL; return defaultVisibility; }
@NotNull public Map<JetModifierKeywordToken, PsiElement> getTokensCorrespondingToModifiers( @NotNull JetModifierList modifierList, @NotNull Collection<JetModifierKeywordToken> possibleModifiers) { Map<JetModifierKeywordToken, PsiElement> tokens = Maps.newHashMap(); for (JetModifierKeywordToken modifier : possibleModifiers) { if (modifierList.hasModifier(modifier)) { tokens.put(modifier, modifierList.getModifier(modifier)); } } return tokens; }
public static boolean isDeprecated(@NotNull JetModifierListOwner owner) { JetModifierList modifierList = owner.getModifierList(); if (modifierList != null) { List<JetAnnotationEntry> annotationEntries = modifierList.getAnnotationEntries(); for (JetAnnotationEntry annotation : annotationEntries) { Name shortName = getShortName(annotation); if (KotlinBuiltIns.getInstance().getDeprecatedAnnotation().getName().equals(shortName)) { return true; } } } return false; }
@Nullable public static JetModifierList replaceModifierList( @NotNull JetModifierListOwner owner, @Nullable JetModifierList modifierList) { JetModifierList oldModifierList = owner.getModifierList(); if (modifierList == null) { if (oldModifierList != null) oldModifierList.delete(); return null; } else { if (oldModifierList == null) { PsiElement firstChild = owner.getFirstChild(); return (JetModifierList) owner.addBefore(modifierList, firstChild); } else { return (JetModifierList) oldModifierList.replace(modifierList); } } }
private void checkTraitModifiers(JetClass aClass) { reportErrorIfHasIllegalModifier(aClass); JetModifierList modifierList = aClass.getModifierList(); if (modifierList == null) return; if (modifierList.hasModifier(JetTokens.FINAL_KEYWORD)) { trace.report(Errors.TRAIT_CAN_NOT_BE_FINAL.on(aClass)); } if (modifierList.hasModifier(JetTokens.SEALED_KEYWORD)) { trace.report(Errors.TRAIT_CAN_NOT_BE_SEALED.on(aClass)); } if (modifierList.hasModifier(JetTokens.ABSTRACT_KEYWORD)) { trace.report(Errors.ABSTRACT_MODIFIER_IN_TRAIT.on(aClass)); } if (modifierList.hasModifier(JetTokens.OPEN_KEYWORD)) { trace.report(Errors.OPEN_MODIFIER_IN_TRAIT.on(aClass)); } }
private void checkModifiersAndAnnotationsInPackageDirective(JetFile file) { JetPackageDirective packageDirective = file.getPackageDirective(); if (packageDirective == null) return; JetModifierList modifierList = packageDirective.getModifierList(); if (modifierList == null) return; for (JetAnnotationEntry annotationEntry : modifierList.getAnnotationEntries()) { JetConstructorCalleeExpression calleeExpression = annotationEntry.getCalleeExpression(); if (calleeExpression != null) { JetReferenceExpression reference = calleeExpression.getConstructorReferenceExpression(); if (reference != null) { trace.report(UNRESOLVED_REFERENCE.on(reference, reference)); } } } AnnotationTargetChecker.INSTANCE$.check(packageDirective, trace); ModifiersChecker.reportIllegalModifiers( modifierList, Arrays.asList(JetTokens.MODIFIER_KEYWORDS_ARRAY), trace); }
private void checkPropertyAbstractness( @NotNull JetProperty property, @NotNull PropertyDescriptor propertyDescriptor, @NotNull ClassDescriptor classDescriptor) { JetPropertyAccessor getter = property.getGetter(); JetPropertyAccessor setter = property.getSetter(); JetModifierList modifierList = property.getModifierList(); ASTNode abstractNode = modifierList != null ? modifierList.getModifierNode(JetTokens.ABSTRACT_KEYWORD) : null; if (abstractNode != null) { // has abstract modifier if (!classCanHaveAbstractMembers(classDescriptor)) { String name = property.getName(); trace.report( ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS.on( property, name != null ? name : "", classDescriptor)); return; } if (classDescriptor.getKind() == ClassKind.INTERFACE) { trace.report(ABSTRACT_MODIFIER_IN_TRAIT.on(property)); } } if (propertyDescriptor.getModality() == Modality.ABSTRACT) { JetExpression initializer = property.getInitializer(); if (initializer != null) { trace.report(ABSTRACT_PROPERTY_WITH_INITIALIZER.on(initializer)); } JetPropertyDelegate delegate = property.getDelegate(); if (delegate != null) { trace.report(ABSTRACT_DELEGATED_PROPERTY.on(delegate)); } if (getter != null && getter.hasBody()) { trace.report(ABSTRACT_PROPERTY_WITH_GETTER.on(getter)); } if (setter != null && setter.hasBody()) { trace.report(ABSTRACT_PROPERTY_WITH_SETTER.on(setter)); } } }
public static boolean isInnerClass(@Nullable JetModifierList modifierList) { return modifierList != null && modifierList.hasModifier(INNER_KEYWORD); }