Ejemplo n.º 1
0
  private static List<FunctionDescriptor> getSuperFunctionsForMethod(
      @NotNull PsiMethodWrapper method,
      @NotNull BindingTrace trace,
      @NotNull ClassDescriptor containingClass) {
    List<FunctionDescriptor> superFunctions = Lists.newArrayList();

    Map<ClassDescriptor, JetType> superclassToSupertype =
        getSuperclassToSupertypeMap(containingClass);

    Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> superclassToFunctions =
        getSuperclassToFunctionsMultimap(method, trace.getBindingContext(), containingClass);

    for (HierarchicalMethodSignature superSignature :
        method.getPsiMethod().getHierarchicalMethodSignature().getSuperSignatures()) {
      PsiMethod superMethod = superSignature.getMethod();

      PsiClass psiClass = superMethod.getContainingClass();
      assert psiClass != null;
      String classFqNameString = psiClass.getQualifiedName();
      assert classFqNameString != null;
      FqName classFqName = new FqName(classFqNameString);

      if (!JavaToKotlinClassMap.getInstance().mapPlatformClass(classFqName).isEmpty()) {
        for (FunctionDescriptor superFun :
            JavaToKotlinMethodMap.INSTANCE.getFunctions(superMethod, containingClass)) {
          superFunctions.add(substituteSuperFunction(superclassToSupertype, superFun));
        }
        continue;
      }

      DeclarationDescriptor superFun =
          superMethod instanceof JetClsMethod
              ? trace.get(
                  BindingContext.DECLARATION_TO_DESCRIPTOR,
                  ((JetClsMethod) superMethod).getOrigin())
              : findSuperFunction(superclassToFunctions.get(classFqName), superMethod);
      if (superFun == null) {
        reportCantFindSuperFunction(method);
        continue;
      }

      assert superFun instanceof FunctionDescriptor : superFun.getClass().getName();

      superFunctions.add(
          substituteSuperFunction(superclassToSupertype, (FunctionDescriptor) superFun));
    }

    // sorting for diagnostic stability
    Collections.sort(
        superFunctions,
        new Comparator<FunctionDescriptor>() {
          @Override
          public int compare(FunctionDescriptor fun1, FunctionDescriptor fun2) {
            FqNameUnsafe fqName1 = getFQName(fun1.getContainingDeclaration());
            FqNameUnsafe fqName2 = getFQName(fun2.getContainingDeclaration());
            return fqName1.getFqName().compareTo(fqName2.getFqName());
          }
        });
    return superFunctions;
  }
Ejemplo n.º 2
0
 private List<DeclarationDescriptor> sortDeclarations(Collection<DeclarationDescriptor> input) {
   ArrayList<DeclarationDescriptor> r = new ArrayList<DeclarationDescriptor>(input);
   Collections.sort(
       r,
       new Comparator<DeclarationDescriptor>() {
         @Override
         public int compare(DeclarationDescriptor o1, DeclarationDescriptor o2) {
           return o1.getName().compareTo(o2.getName());
         }
       });
   return r;
 }
Ejemplo n.º 3
0
  private static String serializedDeclarationSets(Collection<? extends DeclarationDescriptor> ds) {
    List<String> strings = new ArrayList<String>();
    for (DeclarationDescriptor d : ds) {
      StringBuilder sb = new StringBuilder();
      new Serializer(sb).serialize(d);
      strings.add(sb.toString());
    }

    Collections.sort(strings, new MemberComparator());

    StringBuilder r = new StringBuilder();
    for (String string : strings) {
      r.append(string);
      r.append("\n");
    }
    return r.toString();
  }
Ejemplo n.º 4
0
  @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;
  }
Ejemplo n.º 5
0
 public void serialize(TypeParameterDescriptor param) {
   sb.append("/*");
   sb.append(param.getIndex());
   if (param.isReified()) {
     sb.append(",r");
   }
   sb.append("*/ ");
   serialize(param.getVariance());
   sb.append(param.getName());
   if (!param.getUpperBounds().isEmpty()) {
     sb.append(" : ");
     List<String> list = new ArrayList<String>();
     for (JetType upper : param.getUpperBounds()) {
       StringBuilder sb = new StringBuilder();
       new TypeSerializer(sb).serialize(upper);
       list.add(sb.toString());
     }
     Collections.sort(list);
     serializeSeparated(list, " & "); // TODO: use where
   }
   // TODO: lower bounds
 }
Ejemplo n.º 6
0
    public void serialize(ClassDescriptor klass) {

      if (!klass.getAnnotations().isEmpty()) {
        new Serializer(sb).serializeSeparated(klass.getAnnotations(), " ");
        sb.append(" ");
      }
      serialize(klass.getModality());
      sb.append(" ");

      serialize(klass.getKind());
      sb.append(" ");

      new Serializer(sb).serialize(klass);

      if (!klass.getTypeConstructor().getParameters().isEmpty()) {
        sb.append("<");
        serializeCommaSeparated(klass.getTypeConstructor().getParameters());
        sb.append(">");
      }

      if (!klass.getTypeConstructor().getSupertypes().isEmpty()) {
        sb.append(" : ");
        new TypeSerializer(sb)
            .serializeCommaSeparated(
                new ArrayList<JetType>(klass.getTypeConstructor().getSupertypes()));
      }

      sb.append(" {\n");

      List<TypeProjection> typeArguments = new ArrayList<TypeProjection>();
      for (TypeParameterDescriptor param : klass.getTypeConstructor().getParameters()) {
        typeArguments.add(new TypeProjection(Variance.INVARIANT, param.getDefaultType()));
      }

      List<String> memberStrings = new ArrayList<String>();

      for (ConstructorDescriptor constructor : klass.getConstructors()) {
        StringBuilder constructorSb = new StringBuilder();
        new Serializer(constructorSb).serialize(constructor);
        memberStrings.add(constructorSb.toString());
      }

      JetScope memberScope = klass.getMemberScope(typeArguments);
      for (DeclarationDescriptor member : memberScope.getAllDescriptors()) {
        if (!includeObject) {
          if (member
              .getName()
              .matches("equals|hashCode|finalize|wait|notify(All)?|toString|clone|getClass")) {
            continue;
          }
        }
        StringBuilder memberSb = new StringBuilder();
        new FullContentSerialier(memberSb).serialize(member);
        memberStrings.add(memberSb.toString());
      }

      Collections.sort(memberStrings, new MemberComparator());

      for (String memberString : memberStrings) {
        sb.append(indent(memberString));
      }

      if (klass.getClassObjectDescriptor() != null) {
        StringBuilder sbForClassObject = new StringBuilder();
        new FullContentSerialier(sbForClassObject).serialize(klass.getClassObjectDescriptor());
        sb.append(indent(sbForClassObject.toString()));
      }

      sb.append("}\n");
    }