001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.whitespace;
021
022import java.util.Locale;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
029
030/**
031 * <p>
032 * Checks line wrapping for operators.
033 * The policy to verify is specified using the {@link WrapOption} class
034 * and defaults to {@link WrapOption#NL}.
035 * </p>
036 * <p> By default the check will check the following operators:
037 *  {@link TokenTypes#BAND BAND},
038 *  {@link TokenTypes#BOR BOR},
039 *  {@link TokenTypes#BSR BSR},
040 *  {@link TokenTypes#BXOR BXOR},
041 *  {@link TokenTypes#COLON COLON},
042 *  {@link TokenTypes#DIV DIV},
043 *  {@link TokenTypes#EQUAL EQUAL},
044 *  {@link TokenTypes#GE GE},
045 *  {@link TokenTypes#GT GT},
046 *  {@link TokenTypes#LAND LAND},
047 *  {@link TokenTypes#LE LE},
048 *  {@link TokenTypes#LITERAL_INSTANCEOF LITERAL_INSTANCEOF},
049 *  {@link TokenTypes#LOR LOR},
050 *  {@link TokenTypes#LT LT},
051 *  {@link TokenTypes#MINUS MINUS},
052 *  {@link TokenTypes#MOD MOD},
053 *  {@link TokenTypes#NOT_EQUAL NOT_EQUAL},
054 *  {@link TokenTypes#PLUS PLUS},
055 *  {@link TokenTypes#QUESTION QUESTION},
056 *  {@link TokenTypes#SL SL},
057 *  {@link TokenTypes#SR SR},
058 *  {@link TokenTypes#STAR STAR}.
059 * Other acceptable tokens are
060 *  {@link TokenTypes#ASSIGN ASSIGN},
061 *  {@link TokenTypes#BAND_ASSIGN BAND_ASSIGN},
062 *  {@link TokenTypes#BOR_ASSIGN BOR_ASSIGN},
063 *  {@link TokenTypes#BSR_ASSIGN BSR_ASSIGN},
064 *  {@link TokenTypes#BXOR_ASSIGN BXOR_ASSIGN},
065 *  {@link TokenTypes#DIV_ASSIGN DIV_ASSIGN},
066 *  {@link TokenTypes#MINUS_ASSIGN MINUS_ASSIGN},
067 *  {@link TokenTypes#MOD_ASSIGN MOD_ASSIGN},
068 *  {@link TokenTypes#PLUS_ASSIGN PLUS_ASSIGN},
069 *  {@link TokenTypes#SL_ASSIGN SL_ASSIGN},
070 *  {@link TokenTypes#SR_ASSIGN SR_ASSIGN},
071 *  {@link TokenTypes#STAR_ASSIGN STAR_ASSIGN}.
072 *  {@link TokenTypes#METHOD_REF METHOD_REF}.
073 * </p>
074 *  <p>
075 * An example of how to configure the check is:
076 * </p>
077 * <pre>
078 * &lt;module name="OperatorWrap"/&gt;
079 * </pre>
080 * <p> An example of how to configure the check for assignment operators at the
081 * end of a line is:
082 * </p>
083 * <pre>
084 * &lt;module name="OperatorWrap"&gt;
085 *     &lt;property name="tokens"
086 *               value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,MOD_ASSIGN
087 *               ,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,BAND_ASSIGN"/&gt;
088 *     &lt;property name="option" value="eol"/&gt;
089 * &lt;/module&gt;
090 * </pre>
091 *
092 */
093@StatelessCheck
094public class OperatorWrapCheck
095    extends AbstractCheck {
096
097    /**
098     * A key is pointing to the warning message text in "messages.properties"
099     * file.
100     */
101    public static final String MSG_LINE_NEW = "line.new";
102
103    /**
104     * A key is pointing to the warning message text in "messages.properties"
105     * file.
106     */
107    public static final String MSG_LINE_PREVIOUS = "line.previous";
108
109    /** The policy to enforce. */
110    private WrapOption option = WrapOption.NL;
111
112    /**
113     * Set the option to enforce.
114     * @param optionStr string to decode option from
115     * @throws IllegalArgumentException if unable to decode
116     */
117    public void setOption(String optionStr) {
118        try {
119            option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
120        }
121        catch (IllegalArgumentException iae) {
122            throw new IllegalArgumentException("unable to parse " + optionStr, iae);
123        }
124    }
125
126    @Override
127    public int[] getDefaultTokens() {
128        return new int[] {
129            TokenTypes.QUESTION,          // '?'
130            TokenTypes.COLON,             // ':' (not reported for a case)
131            TokenTypes.EQUAL,             // "=="
132            TokenTypes.NOT_EQUAL,         // "!="
133            TokenTypes.DIV,               // '/'
134            TokenTypes.PLUS,              //' +' (unary plus is UNARY_PLUS)
135            TokenTypes.MINUS,             // '-' (unary minus is UNARY_MINUS)
136            TokenTypes.STAR,              // '*'
137            TokenTypes.MOD,               // '%'
138            TokenTypes.SR,                // ">>"
139            TokenTypes.BSR,               // ">>>"
140            TokenTypes.GE,                // ">="
141            TokenTypes.GT,                // ">"
142            TokenTypes.SL,                // "<<"
143            TokenTypes.LE,                // "<="
144            TokenTypes.LT,                // '<'
145            TokenTypes.BXOR,              // '^'
146            TokenTypes.BOR,               // '|'
147            TokenTypes.LOR,               // "||"
148            TokenTypes.BAND,              // '&'
149            TokenTypes.LAND,              // "&&"
150            TokenTypes.TYPE_EXTENSION_AND,
151            TokenTypes.LITERAL_INSTANCEOF,
152        };
153    }
154
155    @Override
156    public int[] getAcceptableTokens() {
157        return new int[] {
158            TokenTypes.QUESTION,          // '?'
159            TokenTypes.COLON,             // ':' (not reported for a case)
160            TokenTypes.EQUAL,             // "=="
161            TokenTypes.NOT_EQUAL,         // "!="
162            TokenTypes.DIV,               // '/'
163            TokenTypes.PLUS,              //' +' (unary plus is UNARY_PLUS)
164            TokenTypes.MINUS,             // '-' (unary minus is UNARY_MINUS)
165            TokenTypes.STAR,              // '*'
166            TokenTypes.MOD,               // '%'
167            TokenTypes.SR,                // ">>"
168            TokenTypes.BSR,               // ">>>"
169            TokenTypes.GE,                // ">="
170            TokenTypes.GT,                // ">"
171            TokenTypes.SL,                // "<<"
172            TokenTypes.LE,                // "<="
173            TokenTypes.LT,                // '<'
174            TokenTypes.BXOR,              // '^'
175            TokenTypes.BOR,               // '|'
176            TokenTypes.LOR,               // "||"
177            TokenTypes.BAND,              // '&'
178            TokenTypes.LAND,              // "&&"
179            TokenTypes.LITERAL_INSTANCEOF,
180            TokenTypes.TYPE_EXTENSION_AND,
181            TokenTypes.ASSIGN,            // '='
182            TokenTypes.DIV_ASSIGN,        // "/="
183            TokenTypes.PLUS_ASSIGN,       // "+="
184            TokenTypes.MINUS_ASSIGN,      //"-="
185            TokenTypes.STAR_ASSIGN,       // "*="
186            TokenTypes.MOD_ASSIGN,        // "%="
187            TokenTypes.SR_ASSIGN,         // ">>="
188            TokenTypes.BSR_ASSIGN,        // ">>>="
189            TokenTypes.SL_ASSIGN,         // "<<="
190            TokenTypes.BXOR_ASSIGN,       // "^="
191            TokenTypes.BOR_ASSIGN,        // "|="
192            TokenTypes.BAND_ASSIGN,       // "&="
193            TokenTypes.METHOD_REF,        // "::"
194        };
195    }
196
197    @Override
198    public int[] getRequiredTokens() {
199        return CommonUtils.EMPTY_INT_ARRAY;
200    }
201
202    @Override
203    public void visitToken(DetailAST ast) {
204        final DetailAST parent = ast.getParent();
205        //we do not want to check colon for cases and defaults
206        if (ast.getType() != TokenTypes.COLON
207                || parent.getType() != TokenTypes.LITERAL_DEFAULT
208                    && parent.getType() != TokenTypes.LITERAL_CASE) {
209            final String text = ast.getText();
210            final int colNo = ast.getColumnNo();
211            final int lineNo = ast.getLineNo();
212            final String currentLine = getLine(lineNo - 1);
213
214            // Check if rest of line is whitespace, and not just the operator
215            // by itself. This last bit is to handle the operator on a line by
216            // itself.
217            if (option == WrapOption.NL
218                    && !text.equals(currentLine.trim())
219                    && CommonUtils.isBlank(currentLine.substring(colNo + text.length()))) {
220                log(lineNo, colNo, MSG_LINE_NEW, text);
221            }
222            else if (option == WrapOption.EOL
223                    && CommonUtils.hasWhitespaceBefore(colNo - 1, currentLine)) {
224                log(lineNo, colNo, MSG_LINE_PREVIOUS, text);
225            }
226        }
227    }
228
229}