コード例 #1
0
  /*
  To declare or not a variable to which method call result will be assigned.
   */
  private static List<VariableInfo> mustAddVariableDeclaration(
      @NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
    Map<String, VariableInfo> names = new HashMap<String, VariableInfo>();
    for (VariableInfo var : vars) {
      names.put(var.getName(), var);
    }
    List<VariableInfo> result = new ArrayList<VariableInfo>();

    for (GrStatement statement : statements) {
      if (statement instanceof GrVariableDeclaration) {
        GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
        for (GrVariable variable : declaration.getVariables()) {
          final VariableInfo removed = names.remove(variable.getName());
          if (removed != null) {
            result.add(removed);
          }
        }
      }
    }
    for (String varName : names.keySet()) {
      if (ResolveUtil.resolveProperty(statements[0], varName) == null) {
        result.add(names.get(varName));
      }
    }

    return result;
  }
コード例 #2
0
  private static List<GrStatement> generateVarDeclarations(
      List<VariableInfo> varInfos, Project project, @Nullable GrExpression initializer) {
    List<GrStatement> result = new ArrayList<GrStatement>();
    if (varInfos.size() == 0) return result;

    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    boolean distinctDeclaration = haveDifferentTypes(varInfos);

    if (distinctDeclaration) {
      for (VariableInfo info : varInfos) {
        result.add(
            factory.createVariableDeclaration(
                ArrayUtil.EMPTY_STRING_ARRAY, null, info.getType(), info.getName()));
      }
    } else {
      String[] names = new String[varInfos.size()];
      for (int i = 0, mustAddLength = varInfos.size(); i < mustAddLength; i++) {
        names[i] = varInfos.get(i).getName();
      }
      result.add(
          factory.createVariableDeclaration(
              ArrayUtil.EMPTY_STRING_ARRAY, initializer, varInfos.get(0).getType(), names));
    }
    return result;
  }
コード例 #3
0
 private static boolean haveDifferentTypes(List<VariableInfo> varInfos) {
   if (varInfos.size() < 2) return true;
   Set<String> diffTypes = new HashSet<String>();
   for (VariableInfo info : varInfos) {
     final PsiType t = info.getType();
     diffTypes.add(t == null ? null : TypesUtil.unboxPrimitiveTypeWrapper(t).getCanonicalText());
   }
   return diffTypes.size() > 1;
 }
コード例 #4
0
  private static boolean varsAreEqual(List<VariableInfo> toAdd, VariableInfo[] outputVars) {
    if (toAdd.size() != outputVars.length) return false;
    Set<String> names = ContainerUtil.newHashSet();
    for (VariableInfo info : toAdd) {
      names.add(info.getName());
    }

    for (VariableInfo var : outputVars) {
      if (!names.contains(var.getName())) return false;
    }

    return true;
  }
コード例 #5
0
 private static GrStatement createAssignment(
     VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
   StringBuilder text = new StringBuilder();
   if (infos.length > 1) text.append('(');
   for (VariableInfo info : infos) {
     text.append(info.getName()).append(", ");
   }
   if (infos.length > 1) {
     text.replace(text.length() - 2, text.length(), ") =");
   } else {
     text.replace(text.length() - 2, text.length(), " = ");
   }
   text.append(callExpression.getText());
   return GroovyPsiElementFactory.getInstance(project).createExpressionFromText(text.toString());
 }
コード例 #6
0
  private static GrStatement[] createTupleDeclaration(
      final VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);

    StringBuilder tuple = new StringBuilder();
    tuple.append("def (");
    for (VariableInfo info : infos) {
      final PsiType type = info.getType();
      if (type != null) {
        final PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(type);
        tuple.append(unboxed.getCanonicalText());
        tuple.append(' ');
      }
      tuple.append(info.getName());
      tuple.append(",");
    }
    StringUtil.trimEnd(tuple, ",");
    tuple.append(")=");
    tuple.append(callExpression.getText());

    return new GrStatement[] {factory.createStatementFromText(tuple)};
  }
コード例 #7
0
  public static void generateBody(
      ExtractInfoHelper helper, boolean isVoid, StringBuilder buffer, boolean forceReturn) {
    VariableInfo[] outputInfos = helper.getOutputVariableInfos();

    ParameterInfo[] infos = helper.getParameterInfos();

    Set<String> declaredVars = new HashSet<String>();
    for (ParameterInfo info : infos) {
      declaredVars.add(info.getName());
    }

    for (VariableInfo info : mustAddVariableDeclaration(helper.getStatements(), outputInfos)) {
      declaredVars.add(info.getName());
    }

    List<VariableInfo> genDecl = new ArrayList<VariableInfo>();
    final Collection<GrVariable> outside =
        collectUsedLocalVarsOrParamsDeclaredOutside(helper.getStatements());

    for (final GrVariable variable : outside) {
      if (!declaredVars.contains(variable.getName())) {
        genDecl.add(
            new VariableInfo() {
              @NotNull
              @Override
              public String getName() {
                return variable.getName();
              }

              @Override
              public PsiType getType() {
                return variable.getDeclaredType();
              }
            });
      }
    }
    final List<GrStatement> statements =
        generateVarDeclarations(genDecl, helper.getProject(), null);
    for (GrStatement statement : statements) {
      buffer.append(statement.getText()).append('\n');
    }

    if (!isSingleExpression(helper.getStatements()) || statements.size() > 0) {
      for (PsiElement element : helper.getInnerElements()) {
        buffer.append(element.getText());
      }
      // append return statement
      if (!isVoid && outputInfos.length > 0) {
        buffer.append('\n');
        if (forceReturn) {
          buffer.append("return ");
        }
        if (outputInfos.length > 1) buffer.append('[');
        for (VariableInfo info : outputInfos) {
          buffer.append(info.getName()).append(", ");
        }
        buffer.delete(buffer.length() - 2, buffer.length());
        if (outputInfos.length > 1) buffer.append(']');
      }
    } else {
      GrExpression expr = (GrExpression) PsiUtil.skipParentheses(helper.getStatements()[0], false);
      boolean addReturn = !isVoid && forceReturn;
      if (addReturn) {
        buffer.append("return ");
        expr = ApplicationStatementUtil.convertToMethodCallExpression(expr);
        buffer.append(expr.getText());
      } else {
        buffer.append(expr != null ? expr.getText() : "");
      }
    }
  }