@NotNull
  public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
      @NotNull ResolveSession resolveSession,
      @NotNull FqName fqName,
      boolean includeObjectDeclarations) {
    if (fqName.isRoot()) {
      return Collections.emptyList();
    }

    Collection<ClassDescriptor> classDescriptors = Lists.newArrayList();

    FqName packageFqName = fqName.parent();
    while (true) {
      NamespaceDescriptor packageDescriptor =
          resolveSession.getPackageDescriptorByFqName(packageFqName);
      if (packageDescriptor != null) {
        FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName));
        Collection<ClassDescriptor> descriptors =
            getClassOrObjectDescriptorsByFqName(
                packageDescriptor, classInPackagePath, includeObjectDeclarations);
        classDescriptors.addAll(descriptors);
      }

      if (packageFqName.isRoot()) {
        break;
      } else {
        packageFqName = packageFqName.parent();
      }
    }

    return classDescriptors;
  }
Example #2
0
  public static boolean isImported(@NotNull ImportPath alreadyImported, @NotNull FqName fqName) {
    if (alreadyImported.isAllUnder() && !fqName.isRoot()) {
      return alreadyImported.fqnPart().equals(fqName.parent());
    }

    return alreadyImported.fqnPart().equals(fqName);
  }
 @NotNull
 @Override
 public Collection<FqName> getSubPackagesOf(@NotNull FqName fqName) {
   if (fqName.isRoot()) {
     return Collections.singleton(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME);
   }
   return Collections.emptyList();
 }
Example #4
0
  @NotNull
  public static FqName withoutFirstSegment(@NotNull FqName fqName) {
    if (fqName.isRoot() || fqName.parent().isRoot()) {
      return FqName.ROOT;
    }

    String fqNameStr = fqName.getFqName();
    return new FqName(fqNameStr.substring(fqNameStr.indexOf('.'), fqNameStr.length()));
  }
 @NotNull
 @Override
 public List<PackageFragmentDescriptor> getPackageFragments(@NotNull FqName fqName) {
   if (fqName.isRoot()) {
     return Collections.singletonList(rootPackage);
   } else if (KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME.equals(fqName)) {
     return Collections.<PackageFragmentDescriptor>singletonList(BuiltinsPackageFragment.this);
   }
   return Collections.emptyList();
 }
Example #6
0
  /**
   * Get tail part of the full fqn by subtracting head part.
   *
   * @param headFQN
   * @param fullFQN
   * @return tail fqn. If first part is not a begging of the full fqn, fullFQN will be returned.
   */
  @NotNull
  public static String tail(@NotNull FqName headFQN, @NotNull FqName fullFQN) {
    if (!isSubpackageOf(fullFQN, headFQN) || headFQN.isRoot()) {
      return fullFQN.getFqName();
    }

    return fullFQN.equals(headFQN)
        ? ""
        : fullFQN.getFqName().substring(headFQN.getFqName().length() + 1); // (headFQN + '.').length
  }
Example #7
0
  @Override
  public void indexFile(PsiJetFileStub stub, IndexSink sink) {
    String packageName = stub.getPackageName();
    FqName fqName = new FqName(packageName == null ? "" : packageName);

    sink.occurrence(JetPackageDeclarationIndex.getInstance().getKey(), fqName.getFqName());

    while (true) {
      sink.occurrence(JetAllPackagesIndex.getInstance().getKey(), fqName.getFqName());
      if (fqName.isRoot()) {
        return;
      }
      fqName = fqName.parent();
    }
  }
Example #8
0
  public static boolean isSubpackageOf(
      @NotNull final FqName subpackageName, @NotNull FqName packageName) {
    if (subpackageName.equals(packageName)) {
      return true;
    }

    if (packageName.isRoot()) {
      return true;
    }

    String subpackageNameStr = subpackageName.getFqName();
    String packageNameStr = packageName.getFqName();

    return (subpackageNameStr.startsWith(packageNameStr)
        && subpackageNameStr.charAt(packageNameStr.length()) == '.');
  }
  private static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
      NamespaceDescriptor packageDescriptor, FqName path, boolean includeObjectDeclarations) {
    if (path.isRoot()) {
      return Collections.emptyList();
    }

    Collection<JetScope> scopes = Arrays.asList(packageDescriptor.getMemberScope());

    List<Name> names = path.pathSegments();
    if (names.size() > 1) {
      for (Name subName : path.pathSegments().subList(0, names.size() - 1)) {
        Collection<JetScope> tempScopes = Lists.newArrayList();
        for (JetScope scope : scopes) {
          ClassifierDescriptor classifier = scope.getClassifier(subName);
          if (classifier instanceof ClassDescriptorBase) {
            ClassDescriptorBase classDescriptor = (ClassDescriptorBase) classifier;
            tempScopes.add(classDescriptor.getUnsubstitutedInnerClassesScope());
          }
        }
        scopes = tempScopes;
      }
    }

    Name shortName = path.shortName();
    Collection<ClassDescriptor> resultClassifierDescriptors = Lists.newArrayList();
    for (JetScope scope : scopes) {
      ClassifierDescriptor classifier = scope.getClassifier(shortName);
      if (classifier instanceof ClassDescriptor) {
        resultClassifierDescriptors.add((ClassDescriptor) classifier);
      }
      if (includeObjectDeclarations) {
        ClassDescriptor objectDescriptor = scope.getObjectDescriptor(shortName);
        if (objectDescriptor != null) {
          resultClassifierDescriptors.add(objectDescriptor);
        }
      }
    }

    return resultClassifierDescriptors;
  }
Example #10
0
  public void checkRedeclarationsInPackages(
      @NotNull KotlinCodeAnalyzer resolveSession,
      @NotNull Multimap<FqName, JetElement> topLevelFqNames) {
    for (Map.Entry<FqName, Collection<JetElement>> entry : topLevelFqNames.asMap().entrySet()) {
      FqName fqName = entry.getKey();
      Collection<JetElement> declarationsOrPackageDirectives = entry.getValue();

      if (fqName.isRoot()) continue;

      Set<DeclarationDescriptor> descriptors =
          getTopLevelDescriptorsByFqName(resolveSession, fqName);

      if (descriptors.size() > 1) {
        for (JetElement declarationOrPackageDirective : declarationsOrPackageDirectives) {
          PsiElement reportAt =
              declarationOrPackageDirective instanceof JetNamedDeclaration
                  ? declarationOrPackageDirective
                  : ((JetPackageDirective) declarationOrPackageDirective).getNameIdentifier();
          trace.report(Errors.REDECLARATION.on(reportAt, fqName.shortName().asString()));
        }
      }
    }
  }
 @Nullable
 @Override
 public PackageViewDescriptor getContainingDeclaration() {
   return fqName.isRoot() ? null : module.getPackage(fqName.parent());
 }