Beispiel #1
0
  @NotNull
  public List<String> createConversions(@NotNull PsiCallExpression expression) {
    PsiExpressionList argumentList = expression.getArgumentList();
    PsiExpression[] arguments =
        argumentList != null ? argumentList.getExpressions() : new PsiExpression[] {};
    List<String> conversions = new LinkedList<String>();
    //noinspection UnusedDeclaration
    for (PsiExpression a : arguments) {
      conversions.add("");
    }

    PsiMethod resolve = expression.resolveMethod();
    if (resolve != null) {
      List<PsiType> expectedTypes = new LinkedList<PsiType>();
      List<PsiType> actualTypes = new LinkedList<PsiType>();

      for (PsiParameter p : resolve.getParameterList().getParameters())
        expectedTypes.add(p.getType());

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

      if (conversions.size() == actualTypes.size() && actualTypes.size() == expectedTypes.size()) {
        for (int i = 0; i < actualTypes.size(); i++)
          conversions.set(i, createConversionForExpression(arguments[i], expectedTypes.get(i)));
      }
    }
    return conversions;
  }
Beispiel #2
0
 private static boolean isNotOpenMethod(@NotNull PsiMethod method) {
   if (method.getParent() instanceof PsiClass) {
     PsiModifierList parentModifierList = ((PsiClass) method.getParent()).getModifierList();
     if ((parentModifierList != null && parentModifierList.hasExplicitModifier(Modifier.FINAL))
         || ((PsiClass) method.getParent()).isEnum()) {
       return true;
     }
   }
   return false;
 }
Beispiel #3
0
 @NotNull
 private ParameterList createFunctionParameters(@NotNull PsiMethod method) {
   List<Parameter> result = new LinkedList<Parameter>();
   for (PsiParameter parameter : method.getParameterList().getParameters()) {
     result.add(
         new Parameter(
             new IdentifierImpl(parameter.getName()),
             typeToType(
                 parameter.getType(),
                 ConverterUtil.isAnnotatedAsNotNull(parameter.getModifierList())),
             ConverterUtil.isReadOnly(parameter, method.getBody())));
   }
   return new ParameterList(result);
 }
Beispiel #4
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;
 }
Beispiel #5
0
  @NotNull
  private Function methodToFunction(@NotNull PsiMethod method, boolean notEmpty) {
    if (isOverrideObjectDirect(method)) {
      dispatcher.setExpressionVisitor(new ExpressionVisitorForDirectObjectInheritors(this));
    } else {
      dispatcher.setExpressionVisitor(new ExpressionVisitor(this));
    }

    methodReturnType = method.getReturnType();

    IdentifierImpl identifier = new IdentifierImpl(method.getName());
    Type returnType =
        typeToType(
            method.getReturnType(), ConverterUtil.isAnnotatedAsNotNull(method.getModifierList()));
    Block body =
        hasFlag(J2KConverterFlags.SKIP_BODIES)
            ? Block.EMPTY_BLOCK
            : blockToBlock(method.getBody(), notEmpty); // #TODO
    Element params = createFunctionParameters(method);
    List<Element> typeParameters = elementsToElementList(method.getTypeParameters());

    Set<String> modifiers = modifiersListToModifiersSet(method.getModifierList());
    if (isOverrideAnyMethodExceptMethodsFromObject(method)) {
      modifiers.add(Modifier.OVERRIDE);
    }
    if (method.getParent() instanceof PsiClass && ((PsiClass) method.getParent()).isInterface()) {
      modifiers.remove(Modifier.ABSTRACT);
    }
    if (isNotOpenMethod(method)) {
      modifiers.add(Modifier.NOT_OPEN);
    }

    if (method.isConstructor()) { // TODO: simplify
      boolean isPrimary = isConstructorPrimary(method);
      return new Constructor(
          identifier,
          modifiers,
          returnType,
          typeParameters,
          params,
          new Block(removeEmpty(body.getStatements()), false),
          isPrimary);
    }
    return new Function(identifier, modifiers, returnType, typeParameters, params, body);
  }
Beispiel #6
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;
 }
Beispiel #7
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;
 }
Beispiel #8
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;
 }
Beispiel #9
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;
 }