コード例 #1
0
  @NotNull
  private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
    VariableInfo[] outputVars = helper.getOutputVariableInfos();

    PsiType type = helper.getOutputType();
    GrStatement[] statements = helper.getStatements();
    GrMethodCallExpression callExpression = createMethodCall(helper);

    if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
      return new GrStatement[] {callExpression};
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    if (helper.hasReturnValue()) {
      return new GrStatement[] {
        factory.createStatementFromText("return " + callExpression.getText())
      };
    }

    LOG.assertTrue(outputVars.length > 0);

    final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
    if (mustAdd.size() == 0) {
      return new GrStatement[] {createAssignment(outputVars, callExpression, helper.getProject())};
    } else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
      return new GrVariableDeclaration[] {
        factory.createVariableDeclaration(
            ArrayUtil.EMPTY_STRING_ARRAY,
            callExpression,
            outputVars[0].getType(),
            outputVars[0].getName())
      };
    } else if (varsAreEqual(mustAdd, outputVars)) {
      return createTupleDeclaration(outputVars, callExpression, helper.getProject());
    } else {
      final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
      result.add(createAssignment(outputVars, callExpression, helper.getProject()));
      return result.toArray(new GrStatement[result.size()]);
    }
  }