Exemplo n.º 1
0
 private static boolean isOverrideObjectDirect(@NotNull PsiMethod method) {
   List<HierarchicalMethodSignature> superSignatures =
       method.getHierarchicalMethodSignature().getSuperSignatures();
   if (superSignatures.size() == 1) {
     PsiClass containingClass = superSignatures.get(0).getMethod().getContainingClass();
     String qualifiedName = containingClass != null ? containingClass.getQualifiedName() : "";
     if (qualifiedName != null && qualifiedName.equals(JAVA_LANG_OBJECT)) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 2
0
 private static boolean normalCase(@NotNull PsiMethod method) {
   int counter = 0;
   for (HierarchicalMethodSignature s :
       method.getHierarchicalMethodSignature().getSuperSignatures()) {
     PsiClass containingClass = s.getMethod().getContainingClass();
     String qualifiedName = containingClass != null ? containingClass.getQualifiedName() : "";
     if (qualifiedName != null && !qualifiedName.equals(JAVA_LANG_OBJECT)) {
       counter++;
     }
   }
   return counter > 0;
 }
Exemplo n.º 3
0
 private static boolean isInheritFromObject(@NotNull PsiMethod method) {
   List<HierarchicalMethodSignature> superSignatures =
       method.getHierarchicalMethodSignature().getSuperSignatures();
   for (HierarchicalMethodSignature s : superSignatures) {
     PsiClass containingClass = s.getMethod().getContainingClass();
     String qualifiedName = containingClass != null ? containingClass.getQualifiedName() : "";
     if (qualifiedName != null && qualifiedName.equals(JAVA_LANG_OBJECT)) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 4
0
 private boolean caseForObject(@NotNull PsiMethod method) {
   PsiClass containing = method.getContainingClass();
   if (containing != null) {
     for (PsiClassType s : containing.getSuperTypes()) {
       String canonicalText = s.getCanonicalText();
       if (!canonicalText.equals(JAVA_LANG_OBJECT)
           && !getClassIdentifiers().contains(canonicalText)) {
         return true;
       }
     }
   }
   return false;
 }
Exemplo n.º 5
0
 public static boolean isConstructorPrimary(@NotNull PsiMethod constructor) {
   if (constructor.getParent() instanceof PsiClass) {
     PsiClass parent = (PsiClass) constructor.getParent();
     if (parent.getConstructors().length == 1) {
       return true;
     } else {
       PsiMethod c =
           getPrimaryConstructorForThisCase(parent); // TODO: move up to classToClass() method
       if (c != null && c.hashCode() == constructor.hashCode()) {
         return true;
       }
     }
   }
   return false;
 }
Exemplo n.º 6
0
 @NotNull
 private List<Member> getMembers(@NotNull PsiClass psiClass) {
   List<Member> members = new LinkedList<Member>();
   for (PsiElement e : psiClass.getChildren()) {
     if (e instanceof PsiMethod) {
       members.add(methodToFunction((PsiMethod) e, true));
     } else if (e instanceof PsiField) {
       members.add(fieldToField((PsiField) e, psiClass));
     } else if (e instanceof PsiClass) {
       members.add(classToClass((PsiClass) e));
     } else if (e instanceof PsiClassInitializer) {
       members.add(initializerToInitializer((PsiClassInitializer) e));
     } else if (e instanceof PsiMember) {
       // System.out.println(e.getClass() + " " + e.getText());
     }
   }
   return members;
 }
Exemplo n.º 7
0
 @Nullable
 private static PsiMethod getPrimaryConstructorForThisCase(@NotNull PsiClass psiClass) {
   ThisVisitor tv = new ThisVisitor();
   psiClass.accept(tv);
   return tv.getPrimaryConstructor();
 }
Exemplo n.º 8
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);
  }