private static void addSuperCall(
     JavaChangeInfo changeInfo, PsiMethod constructor, PsiMethod callee, final UsageInfo[] usages)
     throws IncorrectOperationException {
   final PsiElementFactory factory = JavaPsiFacade.getElementFactory(constructor.getProject());
   PsiExpressionStatement superCall =
       (PsiExpressionStatement) factory.createStatementFromText("super();", constructor);
   PsiCodeBlock body = constructor.getBody();
   assert body != null;
   PsiStatement[] statements = body.getStatements();
   if (statements.length > 0) {
     superCall = (PsiExpressionStatement) body.addBefore(superCall, statements[0]);
   } else {
     superCall = (PsiExpressionStatement) body.add(superCall);
   }
   PsiMethodCallExpression callExpression = (PsiMethodCallExpression) superCall.getExpression();
   final PsiClass aClass = constructor.getContainingClass();
   final PsiClass baseClass = changeInfo.getMethod().getContainingClass();
   final PsiSubstitutor substitutor =
       TypeConversionUtil.getSuperClassSubstitutor(baseClass, aClass, PsiSubstitutor.EMPTY);
   processMethodUsage(
       callExpression.getMethodExpression(), changeInfo, true, false, callee, substitutor, usages);
 }
Exemplo n.º 2
0
  private static void modifySuperCall(
      final PsiMethod subConstructor, final Set<PsiParameter> parametersToPassToSuper) {
    final PsiCodeBlock body = subConstructor.getBody();
    if (body != null) {
      PsiMethodCallExpression superCall = null;
      final PsiStatement[] statements = body.getStatements();
      if (statements.length > 0) {
        if (statements[0] instanceof PsiExpressionStatement) {
          final PsiExpression expression = ((PsiExpressionStatement) statements[0]).getExpression();
          if (expression instanceof PsiMethodCallExpression) {
            final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) expression;
            if ("super".equals(methodCall.getMethodExpression().getText())) {
              superCall = methodCall;
            }
          }
        }
      }

      final PsiElementFactory factory =
          JavaPsiFacade.getInstance(subConstructor.getProject()).getElementFactory();
      try {
        if (superCall == null) {
          PsiExpressionStatement statement =
              (PsiExpressionStatement) factory.createStatementFromText("super();", null);
          statement = (PsiExpressionStatement) body.addAfter(statement, null);
          superCall = (PsiMethodCallExpression) statement.getExpression();
        }

        final PsiExpressionList argList = superCall.getArgumentList();
        for (final PsiParameter parameter : parametersToPassToSuper) {
          argList.add(factory.createExpressionFromText(parameter.getName(), null));
        }
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  }