/*
 * Copyright 2003-2005 Dave Griffith
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.siyeh.ig.performance;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.ExpressionInspection;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;

public class SingleCharacterStartsWithInspection extends ExpressionInspection {

    public String getDisplayName() {
        return "Single character .startsWith() or .endsWith()";
    }

    public String getGroupDisplayName() {
        return GroupNames.PERFORMANCE_GROUP_NAME;
    }

    public String buildErrorString(PsiElement location) {
        return "Single character #ref() should be replaced by .charAt() #loc";
    }

    public BaseInspectionVisitor buildVisitor() {
        return new SingleCharacterStartsWithVisitor();
    }

    private static class SingleCharacterStartsWithVisitor extends BaseInspectionVisitor {

        public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
            super.visitMethodCallExpression(call);
            final PsiReferenceExpression methodExpression = call.getMethodExpression();
            final String methodName = methodExpression.getReferenceName();
            if (!"startsWith".equals(methodName) && !"endsWith".equals(methodName)) {
                return;
            }
            final PsiExpressionList argumentList = call.getArgumentList();
            if (argumentList == null) {
                return;
            }
            final PsiExpression[] args = argumentList.getExpressions();
            if (args.length != 1 && args.length != 2) {
                return;
            }
            if (!isSingleCharacterStringLiteral(args[0])) {
                return;
            }
            final PsiExpression qualifier = methodExpression.getQualifierExpression();
            if (qualifier == null) {
                return;
            }
            final PsiType type = qualifier.getType();
            if (!TypeUtils.isJavaLangString(type)) {
                return;
            }
            registerMethodCallError(call);
        }

        private static boolean isSingleCharacterStringLiteral(PsiExpression arg) {
            final PsiType type = arg.getType();
            if (!TypeUtils.isJavaLangString(type)) {
                return false;
            }
            if (!(arg instanceof PsiLiteralExpression)) {
                return false;
            }
            final PsiLiteralExpression literal = (PsiLiteralExpression) arg;
            final String value = (String) literal.getValue();
            if (value == null) {
                return false;
            }
            return value.length() == 1;
        }

    }

}

@@ -1023,16 +1023,17 @@
 aracter 
+'
 .startsW
@@ -1037,20 +1037,22 @@
 tsWith()
+'
  or 
+'
 .endsWit
@@ -1054,16 +1054,17 @@
 dsWith()
+'
 %22;%0A    %7D
@@ -1259,17 +1259,19 @@
 ter 
+'.
 #ref()
- sh
+' c
 ould
@@ -1290,16 +1290,17 @@
  by 
+'
 .charAt(
 ) #l
@@ -1295,17 +1295,30 @@
 .charAt(
-)
+0)' expression
  #loc%22;%0A
@@ -3330,14 +3330,11 @@
   %7D%0A
-%0A
     %7D%0A
-%0A%7D%0A
+%7D

fix for IDEADEV-2309 (documentation modification)