@NotNull
  public static Collection<JetFile> allFilesInNamespaces(
      BindingContext bindingContext, Collection<JetFile> files) {
    // todo: we use Set and add given files but ignoring other scripts because something non-clear
    // kept in binding
    // for scripts especially in case of REPL

    HashSet<FqName> names = new HashSet<FqName>();
    for (JetFile file : files) {
      if (!file.isScript()) {
        names.add(JetPsiUtil.getFQName(file));
      }
    }

    HashSet<JetFile> answer = new HashSet<JetFile>();
    answer.addAll(files);

    for (FqName name : names) {
      NamespaceDescriptor namespaceDescriptor =
          bindingContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, name);
      Collection<JetFile> jetFiles = bindingContext.get(NAMESPACE_TO_FILES, namespaceDescriptor);
      if (jetFiles != null) answer.addAll(jetFiles);
    }

    List<JetFile> sortedAnswer = new ArrayList<JetFile>(answer);
    Collections.sort(
        sortedAnswer,
        new Comparator<JetFile>() {
          @NotNull
          private String path(JetFile file) {
            VirtualFile virtualFile = file.getVirtualFile();
            assert virtualFile != null : "VirtualFile is null for JetFile: " + file.getName();
            return virtualFile.getPath();
          }

          @Override
          public int compare(JetFile first, JetFile second) {
            return path(first).compareTo(path(second));
          }
        });

    return sortedAnswer;
  }
 /**
  * @see
  *     com.intellij.psi.util.TypeConversionUtil#typeParameterErasure(com.intellij.psi.PsiTypeParameter)
  */
 @Nullable
 private static JavaType typeParameterErasure(
     @NotNull JavaTypeParameter typeParameter,
     @NotNull HashSet<JavaTypeParameter> visited,
     @NotNull JavaTypeSubstitutor substitutor) {
   Collection<JavaClassifierType> upperBounds = typeParameter.getUpperBounds();
   if (!upperBounds.isEmpty()) {
     JavaClassifier classifier = upperBounds.iterator().next().getClassifier();
     if (classifier instanceof JavaTypeParameter && !visited.contains(classifier)) {
       JavaTypeParameter typeParameterBound = (JavaTypeParameter) classifier;
       visited.add(typeParameterBound);
       JavaType substitutedType = substitutor.substitute(typeParameterBound);
       if (substitutedType != null) {
         return erasure(substitutedType);
       }
       return typeParameterErasure(typeParameterBound, visited, substitutor);
     } else if (classifier instanceof JavaClass) {
       return ((JavaClass) classifier).getDefaultType();
     }
   }
   return typeParameter.getTypeProvider().createJavaLangObjectType();
 }