private static void forceResolvePackageDeclarations(
      @NotNull Collection<JetFile> files, @NotNull KotlinCodeAnalyzer session) {
    for (JetFile file : files) {
      // SCRIPT: not supported
      if (file.isScript()) continue;

      FqName packageFqName = file.getPackageFqName();

      // make sure we create a package descriptor
      PackageViewDescriptor packageDescriptor =
          session.getModuleDescriptor().getPackage(packageFqName);
      if (packageDescriptor.isEmpty()) {
        LOG.warn(
            "No descriptor found for package "
                + packageFqName
                + " in file "
                + file.getName()
                + "\n"
                + file.getText());
        session.forceResolveAll();
        continue;
      }

      for (JetDeclaration declaration : file.getDeclarations()) {
        if (declaration instanceof JetFunction) {
          JetFunction jetFunction = (JetFunction) declaration;
          Name name = jetFunction.getNameAsSafeName();
          Collection<FunctionDescriptor> functions =
              packageDescriptor.getMemberScope().getFunctions(name, LookupLocation.NO_LOCATION);
          for (FunctionDescriptor descriptor : functions) {
            ForceResolveUtil.forceResolveAllContents(descriptor);
          }
        } else if (declaration instanceof JetProperty) {
          JetProperty jetProperty = (JetProperty) declaration;
          Name name = jetProperty.getNameAsSafeName();
          Collection<VariableDescriptor> properties =
              packageDescriptor.getMemberScope().getProperties(name, LookupLocation.NO_LOCATION);
          for (VariableDescriptor descriptor : properties) {
            ForceResolveUtil.forceResolveAllContents(descriptor);
          }
        } else if (declaration instanceof JetClassOrObject) {
          // Do nothing: we are not interested in classes
        } else {
          LOG.error(
              "Unsupported declaration kind: "
                  + declaration
                  + " in file "
                  + file.getName()
                  + "\n"
                  + file.getText());
        }
      }
    }
  }
  // Source code is taken from org.jetbrains.kotlin.idea.projectView.JetDeclarationTreeNode,
  // updateImple()
  private String getPresentableElement(JetElement declaration) {
    String text = "";
    if (declaration != null) {
      text = declaration.getName();
      if (text == null) return "";
      if (declaration instanceof JetClassInitializer) {
        text = CLASS_INITIALIZER;
      } else if (declaration instanceof JetProperty) {
        JetProperty property = (JetProperty) declaration;
        JetTypeReference ref = property.getTypeReference();
        if (ref != null) {
          text += " ";
          text += ":";
          text += " ";
          text += ref.getText();
        }
      } else if (declaration instanceof JetFunction) {
        JetFunction function = (JetFunction) declaration;
        JetTypeReference receiverTypeRef = function.getReceiverTypeReference();
        if (receiverTypeRef != null) {
          text = receiverTypeRef.getText() + "." + text;
        }
        text += "(";
        List<JetParameter> parameters = function.getValueParameters();
        for (JetParameter parameter : parameters) {
          if (parameter.getName() != null) {
            text += parameter.getName();
            text += " ";
            text += ":";
            text += " ";
          }
          JetTypeReference typeReference = parameter.getTypeReference();
          if (typeReference != null) {
            text += typeReference.getText();
          }
          text += ", ";
        }
        if (parameters.size() > 0) text = text.substring(0, text.length() - 2);
        text += ")";
        JetTypeReference typeReference = function.getTypeReference();
        if (typeReference != null) {
          text += " ";
          text += ":";
          text += " ";
          text += typeReference.getText();
        }
      }
    }

    return text;
  }
Example #3
0
 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));
   }
 }
  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);
    }
  }
  @Override
  public boolean processUsage(JetChangeInfo changeInfo, PsiElement element) {
    JetParameterList parameterList;

    JetPsiFactory psiFactory = JetPsiFactory(element.getProject());
    if (element instanceof JetFunction) {
      JetFunction function = (JetFunction) element;
      parameterList = function.getValueParameterList();

      if (changeInfo.isNameChanged()) {
        PsiElement identifier = function.getNameIdentifier();

        if (identifier != null) {
          identifier.replace(psiFactory.createIdentifier(changeInfo.getNewName()));
        }
      }

      boolean returnTypeIsNeeded =
          (changeInfo.isRefactoringTarget(originalFunctionDescriptor)
                  || !(function instanceof JetFunctionLiteral)
                  || function.getTypeReference() != null)
              && !(function instanceof JetSecondaryConstructor);
      if (changeInfo.isReturnTypeChanged() && returnTypeIsNeeded) {
        function.setTypeReference(null);
        String returnTypeText =
            changeInfo.renderReturnType((JetFunctionDefinitionUsage<PsiElement>) this);

        // TODO use ChangeFunctionReturnTypeFix.invoke when JetTypeCodeFragment.getType() is ready
        if (!KotlinBuiltIns.getInstance().getUnitType().toString().equals(returnTypeText)) {
          ShortenPackage.addToShorteningWaitSet(
              function.setTypeReference(JetPsiFactory(function).createType(returnTypeText)),
              Options.DEFAULT);
        }
      }
    } else {
      parameterList = ((JetClass) element).getPrimaryConstructorParameterList();
    }

    if (changeInfo.isParameterSetOrOrderChanged()) {
      processParameterListWithStructuralChanges(changeInfo, element, parameterList, psiFactory);
    } else if (parameterList != null) {
      int paramIndex = 0;

      for (JetParameter parameter : parameterList.getParameters()) {
        JetParameterInfo parameterInfo = changeInfo.getNewParameters()[paramIndex];
        changeParameter(paramIndex, parameter, parameterInfo);
        paramIndex++;
      }

      ShortenPackage.addToShorteningWaitSet(parameterList, Options.DEFAULT);
    }

    if (element instanceof JetFunction && changeInfo.isReceiverTypeChanged()) {
      //noinspection unchecked
      String receiverTypeText =
          changeInfo.renderReceiverType((JetFunctionDefinitionUsage<PsiElement>) this);
      JetTypeReference receiverTypeRef =
          receiverTypeText != null ? psiFactory.createType(receiverTypeText) : null;
      JetTypeReference newReceiverTypeRef =
          TypeRefHelpersPackage.setReceiverTypeReference((JetFunction) element, receiverTypeRef);
      if (newReceiverTypeRef != null) {
        ShortenPackage.addToShorteningWaitSet(
            newReceiverTypeRef, ShortenReferences.Options.DEFAULT);
      }
    }

    if (changeInfo.isVisibilityChanged() && !JetPsiUtil.isLocal((JetDeclaration) element)) {
      changeVisibility(changeInfo, element);
    }

    return true;
  }