예제 #1
0
  private JetDecompiledData build() {
    myBuilder.append(PsiBundle.message("psi.decompiled.text.header"));
    myBuilder.append("\n\n");

    String packageName = myClsFile.getPackageName();
    if (packageName.length() > 0) {
      myBuilder.append("package ").append(packageName).append("\n\n");
    }

    PsiClass psiClass = myClsFile.getClasses()[0];

    if (isKotlinNamespaceClass(psiClass)) {
      NamespaceDescriptor nd =
          myJavaDescriptorResolver.resolveNamespace(
              new FqName(packageName), DescriptorSearchRule.INCLUDE_KOTLIN);

      if (nd != null) {
        for (DeclarationDescriptor member :
            sortDeclarations(nd.getMemberScope().getAllDescriptors())) {
          if (member instanceof ClassDescriptor || member instanceof NamespaceDescriptor) {
            continue;
          }
          appendDescriptor(member, "");
          myBuilder.append("\n");
        }
      }
    } else {
      ClassDescriptor cd =
          myJavaDescriptorResolver.resolveClass(psiClass, DescriptorSearchRule.INCLUDE_KOTLIN);
      if (cd != null) {
        appendDescriptor(cd, "");
      }
    }

    JetFile jetFile =
        JetDummyClassFileViewProvider.createJetFile(
            myClsFile.getManager(), myClsFile.getVirtualFile(), myBuilder.toString());
    for (Map.Entry<PsiElement, TextRange> clsMemberToRange : myClsMembersToRanges.entrySet()) {
      PsiElement clsMember = clsMemberToRange.getKey();
      assert clsMember instanceof ClsElementImpl;

      TextRange range = clsMemberToRange.getValue();
      JetDeclaration jetDeclaration =
          PsiTreeUtil.findElementOfClassAtRange(
              jetFile, range.getStartOffset(), range.getEndOffset(), JetDeclaration.class);
      assert jetDeclaration != null;
      myClsElementsToJetElements.put((ClsElementImpl) clsMember, jetDeclaration);
    }

    return new JetDecompiledData(jetFile, myClsElementsToJetElements);
  }
예제 #2
0
  @NotNull
  public Collection<FunctionDescriptor> getTopLevelFunctionDescriptorsByName(
      @NotNull String name,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope scope) {
    // name parameter can differ from expression.getReferenceName() when expression contains
    // completion suffix
    Name referenceName =
        expression.getIdentifier() == null
            ? JetPsiUtil.getConventionName(expression)
            : Name.identifier(name);
    if (referenceName == null || referenceName.toString().isEmpty()) {
      return Collections.emptyList();
    }

    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression);

    if (jetScope == null) {
      return Collections.emptyList();
    }

    Set<FunctionDescriptor> result = Sets.newHashSet();

    Collection<PsiMethod> topLevelFunctionPrototypes =
        JetFromJavaDescriptorHelper.getTopLevelFunctionPrototypesByName(
            referenceName.getName(), project, scope);
    for (PsiMethod method : topLevelFunctionPrototypes) {
      FqName functionFQN = JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(method);
      if (functionFQN != null) {
        JetImportDirective importDirective =
            JetPsiFactory.createImportDirective(project, new ImportPath(functionFQN, false));
        Collection<? extends DeclarationDescriptor> declarationDescriptors =
            new QualifiedExpressionResolver()
                .analyseImportReference(
                    importDirective,
                    jetScope,
                    new BindingTraceContext(),
                    resolveSession.getModuleConfiguration());
        for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
          if (declarationDescriptor instanceof FunctionDescriptor) {
            result.add((FunctionDescriptor) declarationDescriptor);
          }
        }
      }
    }

    Set<FqName> affectedPackages = Sets.newHashSet();
    Collection<JetNamedFunction> jetNamedFunctions =
        JetShortFunctionNameIndex.getInstance().get(referenceName.getName(), project, scope);
    for (JetNamedFunction jetNamedFunction : jetNamedFunctions) {
      PsiFile containingFile = jetNamedFunction.getContainingFile();
      if (containingFile instanceof JetFile) {
        JetFile jetFile = (JetFile) containingFile;
        String packageName = jetFile.getPackageName();
        if (packageName != null) {
          affectedPackages.add(new FqName(packageName));
        }
      }
    }

    for (FqName affectedPackage : affectedPackages) {
      NamespaceDescriptor packageDescriptor =
          resolveSession.getPackageDescriptorByFqName(affectedPackage);
      assert packageDescriptor != null
          : "There's a function in stub index with invalid package: " + affectedPackage;
      JetScope memberScope = packageDescriptor.getMemberScope();
      result.addAll(memberScope.getFunctions(referenceName));
    }

    return result;
  }