コード例 #1
0
  @Nullable
  private LocalQuickFix[] createNPEFixes(
      PsiExpression qualifier, PsiExpression expression, boolean onTheFly) {
    if (qualifier == null || expression == null) return null;
    if (qualifier instanceof PsiMethodCallExpression) return null;
    if (qualifier instanceof PsiLiteralExpression
        && ((PsiLiteralExpression) qualifier).getValue() == null) return null;

    try {
      final List<LocalQuickFix> fixes = new SmartList<LocalQuickFix>();

      if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_4)) {
        final Project project = qualifier.getProject();
        final PsiElementFactory elementFactory =
            JavaPsiFacade.getInstance(project).getElementFactory();
        final PsiBinaryExpression binary =
            (PsiBinaryExpression) elementFactory.createExpressionFromText("a != null", null);
        binary.getLOperand().replace(qualifier);
        fixes.add(new AddAssertStatementFix(binary));
      }

      addSurroundWithIfFix(qualifier, fixes, onTheFly);

      if (ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) {
        fixes.add(new ReplaceWithTernaryOperatorFix(qualifier));
      }
      return fixes.toArray(new LocalQuickFix[fixes.size()]);
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
コード例 #2
0
 @Nullable
 public static String checkContract(PsiMethod method, String text) {
   List<MethodContract> contracts;
   try {
     contracts = ControlFlowAnalyzer.parseContract(text);
   } catch (ControlFlowAnalyzer.ParseException e) {
     return e.getMessage();
   }
   int paramCount = method.getParameterList().getParametersCount();
   for (int i = 0; i < contracts.size(); i++) {
     MethodContract contract = contracts.get(i);
     if (contract.arguments.length != paramCount) {
       return "Method takes "
           + paramCount
           + " parameters, while contract clause number "
           + (i + 1)
           + " expects "
           + contract.arguments.length;
     }
   }
   return null;
 }