private static void changeVisibility(JetChangeInfo changeInfo, PsiElement element) {
    JetModifierKeywordToken newVisibilityToken =
        JetRefactoringUtil.getVisibilityToken(changeInfo.getNewVisibility());

    if (element instanceof JetFunction) {
      ((JetFunction) element).addModifier(newVisibilityToken);
    } else {
      JetPrimaryConstructor constructor = ((JetClass) element).getPrimaryConstructor();
      assert constructor != null
          : "Primary constructor should be created before changing visibility";
      constructor.addModifier(newVisibilityToken);
    }
  }
예제 #2
0
  private void checkPrimaryConstructor(
      JetClassOrObject classOrObject, ClassDescriptor classDescriptor) {
    ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
    JetPrimaryConstructor declaration = classOrObject.getPrimaryConstructor();
    if (primaryConstructor == null || declaration == null) return;

    for (JetParameter parameter : declaration.getValueParameters()) {
      PropertyDescriptor propertyDescriptor =
          trace.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
      if (propertyDescriptor != null) {
        modifiersChecker.checkModifiersForDeclaration(parameter, propertyDescriptor);
      }
    }

    if (declaration.getModifierList() != null && !declaration.hasConstructorKeyword()) {
      trace.report(MISSING_CONSTRUCTOR_KEYWORD.on(declaration.getModifierList()));
    }

    if (!(classOrObject instanceof JetClass)) {
      trace.report(CONSTRUCTOR_IN_OBJECT.on(declaration));
    }

    checkConstructorDeclaration(primaryConstructor, declaration);
  }
  private void processParameterListWithStructuralChanges(
      JetChangeInfo changeInfo,
      PsiElement element,
      JetParameterList parameterList,
      JetPsiFactory psiFactory) {
    int parametersCount = changeInfo.getNonReceiverParametersCount();
    boolean isLambda = element instanceof JetFunctionLiteral;
    boolean canReplaceEntireList = false;

    JetParameterList newParameterList = null;
    if (isLambda) {
      if (parametersCount == 0 && ((JetFunctionLiteral) element).getTypeReference() == null) {
        if (parameterList != null) {
          parameterList.delete();
          ASTNode arrowNode = ((JetFunctionLiteral) element).getArrowNode();
          if (arrowNode != null) {
            arrowNode.getPsi().delete();
          }
          parameterList = null;
        }
      } else {
        newParameterList =
            psiFactory.createFunctionLiteralParameterList(
                changeInfo.getNewParametersSignature(
                    (JetFunctionDefinitionUsage<PsiElement>) this));
        canReplaceEntireList = true;
      }
    } else {
      newParameterList =
          psiFactory.createParameterList(
              changeInfo.getNewParametersSignature((JetFunctionDefinitionUsage<PsiElement>) this));
    }

    if (newParameterList == null) return;

    if (parameterList != null) {
      if (canReplaceEntireList) {
        newParameterList = (JetParameterList) parameterList.replace(newParameterList);
      } else {
        newParameterList = replaceParameterListAndKeepDelimiters(parameterList, newParameterList);
      }
    } else {
      if (element instanceof JetClass) {
        PsiElement anchor = ((JetClass) element).getTypeParameterList();

        if (anchor == null) {
          anchor = ((JetClass) element).getNameIdentifier();
        }
        if (anchor != null) {
          JetPrimaryConstructor constructor =
              (JetPrimaryConstructor)
                  element.addAfter(psiFactory.createPrimaryConstructor(), anchor);
          JetParameterList oldParameterList = constructor.getValueParameterList();
          assert oldParameterList != null : "primary constructor from factory has parameter list";
          newParameterList = (JetParameterList) oldParameterList.replace(newParameterList);
        }
      } else if (isLambda) {
        //noinspection ConstantConditions
        JetFunctionLiteral functionLiteral = (JetFunctionLiteral) element;
        PsiElement anchor = functionLiteral.getLBrace();
        newParameterList = (JetParameterList) element.addAfter(newParameterList, anchor);
        if (functionLiteral.getArrowNode() == null) {
          Pair<PsiElement, PsiElement> whitespaceAndArrow = psiFactory.createWhitespaceAndArrow();
          element.addRangeAfter(
              whitespaceAndArrow.getFirst(), whitespaceAndArrow.getSecond(), newParameterList);
        }
      }
    }

    if (newParameterList != null) {
      ShortenPackage.addToShorteningWaitSet(newParameterList, Options.DEFAULT);
    }
  }