IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / portability / HardcodedLineSeparatorsInspection.java
blob019f3b592945cc8c1de5e2fd73868173bd11f873
1 /*
2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.siyeh.ig.portability;
18 import com.intellij.psi.PsiLiteralExpression;
19 import com.intellij.psi.PsiType;
20 import com.siyeh.InspectionGadgetsBundle;
21 import com.siyeh.ig.BaseInspection;
22 import com.siyeh.ig.BaseInspectionVisitor;
23 import com.siyeh.ig.psiutils.TypeUtils;
24 import org.jetbrains.annotations.NotNull;
26 public class HardcodedLineSeparatorsInspection extends BaseInspection {
28 @NotNull
29 public String getDisplayName() {
30 return InspectionGadgetsBundle.message(
31 "hardcoded.line.separator.display.name");
34 @NotNull
35 public String getID(){
36 return "HardcodedLineSeparator";
39 @NotNull
40 public String buildErrorString(Object... infos) {
41 return InspectionGadgetsBundle.message(
42 "hardcoded.line.separator.problem.descriptor");
45 public BaseInspectionVisitor buildVisitor() {
46 return new HardcodedLineSeparatorsVisitor();
49 private static class HardcodedLineSeparatorsVisitor
50 extends BaseInspectionVisitor {
52 private static final char NEW_LINE_CHAR = '\n';
53 private static final char RETURN_CHAR = '\r';
55 @Override public void visitLiteralExpression(
56 @NotNull PsiLiteralExpression expression) {
57 super.visitLiteralExpression(expression);
58 final PsiType type = expression.getType();
59 if (type == null) {
60 return;
62 if (TypeUtils.isJavaLangString(type)) {
63 final String value = (String) expression.getValue();
64 if (value == null) {
65 return;
67 if (value.indexOf(NEW_LINE_CHAR) >= 0 ||
68 value.indexOf(RETURN_CHAR) >= 0) {
69 registerError(expression);
71 } else if (type.equals(PsiType.CHAR)) {
72 final Character value = (Character) expression.getValue();
73 if (value == null) {
74 return;
76 final char unboxedValue = value.charValue();
77 if (unboxedValue == NEW_LINE_CHAR
78 || unboxedValue == RETURN_CHAR) {
79 registerError(expression);