Ejemplo n.º 1
0
  @NotNull
  public TypeProjection transformToTypeProjection(
      @NotNull final PsiType javaType,
      @NotNull final TypeParameterDescriptor typeParameterDescriptor,
      @NotNull final TypeVariableResolver typeVariableByPsiResolver) {
    TypeProjection result =
        javaType.accept(
            new PsiTypeVisitor<TypeProjection>() {

              @Override
              public TypeProjection visitCapturedWildcardType(
                  PsiCapturedWildcardType capturedWildcardType) {
                throw new UnsupportedOperationException(); // TODO
              }

              @Override
              public TypeProjection visitWildcardType(PsiWildcardType wildcardType) {
                if (!wildcardType.isBounded()) {
                  return TypeUtils.makeStarProjection(typeParameterDescriptor);
                }
                Variance variance =
                    wildcardType.isExtends() ? Variance.OUT_VARIANCE : Variance.IN_VARIANCE;

                PsiType bound = wildcardType.getBound();
                assert bound != null;
                return new TypeProjection(
                    variance,
                    transformToType(bound, TypeUsage.UPPER_BOUND, typeVariableByPsiResolver));
              }

              @Override
              public TypeProjection visitType(PsiType type) {
                return new TypeProjection(
                    transformToType(type, TypeUsage.TYPE_ARGUMENT, typeVariableByPsiResolver));
              }
            });
    return result;
  }
Ejemplo n.º 2
0
  @NotNull
  public JetType transformToType(
      @NotNull PsiType javaType, @NotNull final TypeVariableResolver typeVariableResolver) {
    return javaType.accept(
        new PsiTypeVisitor<JetType>() {
          @Override
          public JetType visitClassType(PsiClassType classType) {
            PsiClassType.ClassResolveResult classResolveResult = classType.resolveGenerics();
            PsiClass psiClass = classResolveResult.getElement();
            if (psiClass == null) {
              return ErrorUtils.createErrorType(
                  "Unresolved java class: " + classType.getPresentableText());
            }

            if (psiClass instanceof PsiTypeParameter) {
              PsiTypeParameter typeParameter = (PsiTypeParameter) psiClass;
              TypeParameterDescriptor typeParameterDescriptor =
                  typeVariableResolver.getTypeVariable(typeParameter.getName());
              //                    return
              // TypeUtils.makeNullable(typeParameterDescriptor.getDefaultType());
              return typeParameterDescriptor.getDefaultType();
            } else {
              JetType jetAnalog = getClassTypesMap().get(psiClass.getQualifiedName());
              if (jetAnalog != null) {
                return jetAnalog;
              }

              final JavaDescriptorResolver.ResolverClassData classData =
                  resolver.resolveClassData(psiClass);
              if (classData == null) {
                return ErrorUtils.createErrorType(
                    "Unresolve java class: " + classType.getPresentableText());
              }

              List<TypeProjection> arguments = Lists.newArrayList();
              if (classType.isRaw()) {
                List<TypeParameterDescriptor> parameters =
                    classData.getClassDescriptor().getTypeConstructor().getParameters();
                for (TypeParameterDescriptor parameter : parameters) {
                  arguments.add(TypeUtils.makeStarProjection(parameter));
                }
              } else {
                List<TypeParameterDescriptor> parameters =
                    classData.getClassDescriptor().getTypeConstructor().getParameters();
                PsiType[] psiArguments = classType.getParameters();

                if (parameters.size() != psiArguments.length) {
                  throw new IllegalStateException();
                }

                for (int i = 0; i < parameters.size(); i++) {
                  PsiType psiArgument = psiArguments[i];
                  TypeParameterDescriptor typeParameterDescriptor = parameters.get(i);

                  arguments.add(
                      transformToTypeProjection(
                          psiArgument, typeParameterDescriptor, typeVariableResolver));
                }
              }
              return new JetTypeImpl(
                  Collections.<AnnotationDescriptor>emptyList(),
                  classData.getClassDescriptor().getTypeConstructor(),
                  true,
                  arguments,
                  classData.getClassDescriptor().getMemberScope(arguments));
            }
          }

          @Override
          public JetType visitPrimitiveType(PsiPrimitiveType primitiveType) {
            String canonicalText = primitiveType.getCanonicalText();
            JetType type = getPrimitiveTypesMap().get(canonicalText);
            assert type != null : canonicalText;
            return type;
          }

          @Override
          public JetType visitArrayType(PsiArrayType arrayType) {
            PsiType componentType = arrayType.getComponentType();
            if (componentType instanceof PsiPrimitiveType) {
              JetType jetType = getPrimitiveTypesMap().get("[" + componentType.getCanonicalText());
              if (jetType != null) return TypeUtils.makeNullable(jetType);
            }

            JetType type = transformToType(componentType, typeVariableResolver);
            return TypeUtils.makeNullable(standardLibrary.getArrayType(type));
          }

          @Override
          public JetType visitType(PsiType type) {
            throw new UnsupportedOperationException(
                "Unsupported type: " + type.getPresentableText()); // TODO
          }
        });
  }