示例#1
0
 @NotNull
 private File fileToFile(PsiJavaFile javaFile, List<String> additionalImports) {
   PsiImportList importList = javaFile.getImportList();
   List<Import> imports =
       importList == null
           ? Collections.<Import>emptyList()
           : importsToImportList(importList.getAllImportStatements());
   for (String i : additionalImports) imports.add(new Import(i));
   return new File(
       quoteKeywords(javaFile.getPackageName()),
       imports,
       classesToClassList(javaFile.getClasses()),
       createMainFunction(javaFile));
 }
示例#2
0
  @NotNull
  public List<String> createConversions(
      @NotNull PsiPolyadicExpression expression, PsiType expectedType) {
    PsiExpression[] arguments = expression.getOperands();
    int length = arguments.length;
    List<String> conversions = new LinkedList<String>();

    List<PsiType> expectedTypes = Collections.nCopies(length, expectedType);
    List<PsiType> actualTypes = new LinkedList<PsiType>();

    for (PsiExpression e : arguments) actualTypes.add(e.getType());

    assert actualTypes.size() == expectedTypes.size() : "The type list must have the same length";

    for (int i = 0; i < actualTypes.size(); i++)
      conversions.add(i, createConversionForExpression(arguments[i], expectedTypes.get(i)));

    return conversions;
  }
示例#3
0
 @NotNull
 public Set<String> getClassIdentifiers() {
   return Collections.unmodifiableSet(classIdentifiers);
 }
示例#4
0
  @NotNull
  public Class classToClass(@NotNull PsiClass psiClass) {
    Set<String> modifiers = modifiersListToModifiersSet(psiClass.getModifierList());
    List<Field> fields = fieldsToFieldList(psiClass.getFields(), psiClass);
    List<Element> typeParameters = elementsToElementList(psiClass.getTypeParameters());
    List<Type> implementsTypes = typesToNotNullableTypeList(psiClass.getImplementsListTypes());
    List<Type> extendsTypes = typesToNotNullableTypeList(psiClass.getExtendsListTypes());
    IdentifierImpl name = new IdentifierImpl(psiClass.getName());
    List<Expression> baseClassParams = new LinkedList<Expression>();

    List<Member> members = getMembers(psiClass);

    // we try to find super() call and generate class declaration like that: class A(name: String, i
    // : Int) : Base(name)
    SuperVisitor visitor = new SuperVisitor();
    psiClass.accept(visitor);
    Collection<PsiExpressionList> resolvedSuperCallParameters =
        visitor.getResolvedSuperCallParameters();
    if (resolvedSuperCallParameters.size() == 1) {
      baseClassParams.addAll(
          expressionsToExpressionList(
              resolvedSuperCallParameters.toArray(new PsiExpressionList[1])[0].getExpressions()));
    }

    // we create primary constructor from all non final fields and fields without initializers
    if (!psiClass.isEnum()
        && !psiClass.isInterface()
        && psiClass.getConstructors().length > 1
        && getPrimaryConstructorForThisCase(psiClass) == null) {
      List<Field> finalOrWithEmptyInitializer = getFinalOrWithEmptyInitializer(fields);
      Map<String, String> initializers = new HashMap<String, String>();

      for (Member m : members) {
        // and modify secondaries
        if (m.getKind() == INode.Kind.CONSTRUCTOR) {
          Function f = (Function) m;
          if (!((Constructor) f).isPrimary()) {
            for (Field fo : finalOrWithEmptyInitializer) {
              String init = getDefaultInitializer(fo);
              initializers.put(fo.getIdentifier().toKotlin(), init);
            }

            List<Statement> newStatements = new LinkedList<Statement>();

            for (Statement s : f.getBlock().getStatements()) {
              boolean isRemoved = false;

              if (s.getKind() == INode.Kind.ASSIGNMENT_EXPRESSION) {
                AssignmentExpression assignmentExpression = (AssignmentExpression) s;
                if (assignmentExpression.getLeft().getKind() == INode.Kind.CALL_CHAIN) {
                  for (Field fo : finalOrWithEmptyInitializer) {
                    String id = fo.getIdentifier().toKotlin();
                    if (((CallChainExpression) assignmentExpression.getLeft())
                        .getIdentifier()
                        .toKotlin()
                        .endsWith("." + id)) {
                      initializers.put(id, assignmentExpression.getRight().toKotlin());
                      isRemoved = true;
                    }
                  }
                }
              }
              if (!isRemoved) {
                newStatements.add(s);
              }
            }

            newStatements.add(
                0,
                new DummyStringExpression(
                    "val __ = "
                        + createPrimaryConstructorInvocation(
                            name.toKotlin(), finalOrWithEmptyInitializer, initializers)));

            f.setBlock(new Block(newStatements));
          }
        }
      }

      members.add(
          new Constructor(
              Identifier.EMPTY_IDENTIFIER,
              Collections.<String>emptySet(),
              new ClassType(name),
              Collections.<Element>emptyList(),
              new ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
              new Block(createInitStatementsFromFields(finalOrWithEmptyInitializer)),
              true));
    }

    if (psiClass.isInterface()) {
      return new Trait(
          this,
          name,
          modifiers,
          typeParameters,
          extendsTypes,
          Collections.<Expression>emptyList(),
          implementsTypes,
          members);
    }
    if (psiClass.isEnum()) {
      return new Enum(
          this,
          name,
          modifiers,
          typeParameters,
          Collections.<Type>emptyList(),
          Collections.<Expression>emptyList(),
          implementsTypes,
          members);
    }
    return new Class(
        this,
        name,
        modifiers,
        typeParameters,
        extendsTypes,
        baseClassParams,
        implementsTypes,
        members);
  }
示例#5
0
 @NotNull
 public File fileToFileWithCompatibilityImport(@NotNull PsiJavaFile javaFile) {
   return fileToFile(javaFile, Collections.singletonList("kotlin.compatibility.*"));
 }
示例#6
0
 @NotNull
 public File fileToFile(@NotNull PsiJavaFile javaFile) {
   return fileToFile(javaFile, Collections.<String>emptyList());
 }