コード例 #1
0
  private String renderDefaultType(JetType type, boolean shortNamesOnly) {
    StringBuilder sb = new StringBuilder();
    ClassifierDescriptor cd = type.getConstructor().getDeclarationDescriptor();

    Object typeNameObject;

    if (cd == null || cd instanceof TypeParameterDescriptor) {
      typeNameObject = type.getConstructor();
    } else {
      if (shortNamesOnly) {
        // for nested classes qualified name should be used
        typeNameObject = cd.getName();
        DeclarationDescriptor parent = cd.getContainingDeclaration();
        while (parent instanceof ClassDescriptor) {
          typeNameObject = parent.getName() + "." + typeNameObject;
          parent = parent.getContainingDeclaration();
        }
      } else {
        typeNameObject = DescriptorUtils.getFQName(cd);
      }
    }

    sb.append(typeNameObject);
    if (!type.getArguments().isEmpty()) {
      sb.append("<");
      appendTypeProjections(sb, type.getArguments(), shortNamesOnly);
      sb.append(">");
    }
    if (type.isNullable()) {
      sb.append("?");
    }
    return sb.toString();
  }
コード例 #2
0
  // Returns list with type arguments info from supertypes
  // Example:
  //     - Foo<A, B> is a subtype of Bar<A, List<B>>, Baz<Boolean, A>
  //     - input: klass = Foo, typesFromSuper = [Bar<String, List<Int>>, Baz<Boolean, CharSequence>]
  //     - output[0] = [String, CharSequence], output[1] = []
  private static List<List<TypeProjectionAndVariance>> calculateTypeArgumentsFromSuper(
      @NotNull ClassDescriptor klass, @NotNull Collection<TypeAndVariance> typesFromSuper) {
    // For each superclass of klass and its parameters, hold their mapping to klass' parameters
    // #0 of Bar ->  A
    // #1 of Bar ->  List<B>
    // #0 of Baz ->  Boolean
    // #1 of Baz ->  A
    // #0 of Foo ->  A (mapped to itself)
    // #1 of Foo ->  B (mapped to itself)
    Multimap<TypeConstructor, TypeProjection> substitution =
        SubstitutionUtils.buildDeepSubstitutionMultimap(
            TypeUtils.makeUnsubstitutedType(klass, JetScope.EMPTY));

    // for each parameter of klass, hold arguments in corresponding supertypes
    List<List<TypeProjectionAndVariance>> parameterToArgumentsFromSuper = Lists.newArrayList();
    for (TypeParameterDescriptor ignored : klass.getTypeConstructor().getParameters()) {
      parameterToArgumentsFromSuper.add(new ArrayList<TypeProjectionAndVariance>());
    }

    // Enumerate all types from super and all its parameters
    for (TypeAndVariance typeFromSuper : typesFromSuper) {
      for (TypeParameterDescriptor parameter :
          typeFromSuper.type.getConstructor().getParameters()) {
        TypeProjection argument = typeFromSuper.type.getArguments().get(parameter.getIndex());

        // for given example, this block is executed four times:
        // 1. typeFromSuper = Bar<String, List<Int>>,      parameter = "#0 of Bar",  argument =
        // String
        // 2. typeFromSuper = Bar<String, List<Int>>,      parameter = "#1 of Bar",  argument =
        // List<Int>
        // 3. typeFromSuper = Baz<Boolean, CharSequence>,  parameter = "#0 of Baz",  argument =
        // Boolean
        // 4. typeFromSuper = Baz<Boolean, CharSequence>,  parameter = "#1 of Baz",  argument =
        // CharSequence

        // if it is mapped to klass' parameter, then store it into map
        for (TypeProjection projection : substitution.get(parameter.getTypeConstructor())) {
          // 1. projection = A
          // 2. projection = List<B>
          // 3. projection = Boolean
          // 4. projection = A
          ClassifierDescriptor classifier =
              projection.getType().getConstructor().getDeclarationDescriptor();

          // this condition is true for 1 and 4, false for 2 and 3
          if (classifier instanceof TypeParameterDescriptor
              && classifier.getContainingDeclaration() == klass) {
            int parameterIndex = ((TypeParameterDescriptor) classifier).getIndex();
            Variance effectiveVariance =
                parameter.getVariance().superpose(typeFromSuper.varianceOfPosition);
            parameterToArgumentsFromSuper
                .get(parameterIndex)
                .add(new TypeProjectionAndVariance(argument, effectiveVariance));
          }
        }
      }
    }
    return parameterToArgumentsFromSuper;
  }
コード例 #3
0
ファイル: JsDescriptorUtils.java プロジェクト: hhariri/kotlin
  @Nullable
  public static Name getNameIfStandardType(@NotNull JetType type) {
    ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
    if (descriptor != null
        && descriptor.getContainingDeclaration()
            == KotlinBuiltIns.getInstance().getBuiltInsPackageFragment()) {
      return descriptor.getName();
    }

    return null;
  }