Source code: com/puppycrawl/tools/checkstyle/checks/CheckUtils.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.checks;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.FullIdent;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24
25 /**
26 * Contains utility methods for the checks.
27 *
28 * @author Oliver Burn
29 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
30 */
31 public final class CheckUtils
32 {
33 // TODO: Make this a regex?
34 // private static final String EQUALS_METHOD_NAME = "equals";
35
36 /** prevent instances */
37 private CheckUtils()
38 {
39 throw new UnsupportedOperationException();
40 }
41
42 // public static FullIdent createFullType(DetailAST typeAST) {
43 // DetailAST arrayDeclAST =
44 // typeAST.findFirstToken(TokenTypes.ARRAY_DECLARATOR);
45 //
46 // return createFullTypeNoArrays(
47 // arrayDeclAST == null ? typeAST : arrayDeclAST);
48 // }
49 //
50 // public static boolean isEqualsMethod(DetailAST detailAST) {
51 // return detailAST.findFirstToken(TokenTypes.IDENT).getText().equals(
52 // EQUALS_METHOD_NAME);
53 // }
54 //
55 // public static boolean isFinal(DetailAST detailAST) {
56 // DetailAST modifiersAST =
57 //detailAST.findFirstToken(TokenTypes.MODIFIERS);
58 //
59 // return modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
60 // }
61 //
62 // public static boolean isInObjBlock(DetailAST detailAST) {
63 // return detailAST.getParent().getType() == TokenTypes.OBJBLOCK;
64 // }
65 //
66 // public static String getIdentText(DetailAST detailAST) {
67 // return detailAST.findFirstToken(TokenTypes.IDENT).getText();
68 // }
69 //
70 // private static FullIdent createFullTypeNoArrays(DetailAST typeAST) {
71 // return FullIdent.createFullIdent((DetailAST) typeAST.getFirstChild());
72 // }
73
74
75 /**
76 * Returns whether a token represents an ELSE as part of an ELSE / IF set.
77 * @param aAST the token to check
78 * @return whether it is
79 */
80 public static boolean isElseIf(DetailAST aAST)
81 {
82 final DetailAST parentAST = aAST.getParent();
83
84 return (aAST.getType() == TokenTypes.LITERAL_IF)
85 && (isElse(parentAST) || isElseWithCurlyBraces(parentAST));
86 }
87
88 /**
89 * Returns whether a token represents an ELSE.
90 * @param aAST the token to check
91 * @return whether the token represents an ELSE
92 */
93 private static boolean isElse(DetailAST aAST)
94 {
95 return aAST.getType() == TokenTypes.LITERAL_ELSE;
96 }
97
98 /**
99 * Returns whether a token represents an SLIST as part of an ELSE
100 * statement.
101 * @param aAST the token to check
102 * @return whether the toke does represent an SLIST as part of an ELSE
103 */
104 private static boolean isElseWithCurlyBraces(DetailAST aAST)
105 {
106 return (aAST.getType() == TokenTypes.SLIST)
107 && (aAST.getChildCount() == 2)
108 && isElse(aAST.getParent());
109 }
110
111 /**
112 * Creates <code>FullIdent</code> for given type node.
113 * @param aTypeAST a type node.
114 * @return <code>FullIdent</code> for given type.
115 */
116 public static FullIdent createFullType(DetailAST aTypeAST)
117 {
118 DetailAST arrayDeclAST =
119 aTypeAST.findFirstToken(TokenTypes.ARRAY_DECLARATOR);
120
121 return createFullTypeNoArrays(arrayDeclAST == null ? aTypeAST
122 : arrayDeclAST);
123 }
124
125 /**
126 * @param aTypeAST a type node (no array)
127 * @return <code>FullIdent</code> for given type.
128 */
129 private static FullIdent createFullTypeNoArrays(DetailAST aTypeAST)
130 {
131 return FullIdent.createFullIdent((DetailAST) aTypeAST.getFirstChild());
132 }
133
134 // constants for parseFloat()
135 /** octal radix */
136 private static final int BASE_8 = 8;
137
138 /** decimal radix */
139 private static final int BASE_10 = 10;
140
141 /** hex radix */
142 private static final int BASE_16 = 16;
143
144 /**
145 * Returns the value represented by the specified string of the specified
146 * type. Returns 0 for types other than float, double, int, and long.
147 * @param aText the string to be parsed.
148 * @param aType the token type of the text. Should be a constant of
149 * {@link com.puppycrawl.tools.checkstyle.api.TokenTypes}.
150 * @return the float value represented by the string argument.
151 */
152 public static float parseFloat(String aText, int aType)
153 {
154 float result = 0;
155 switch (aType) {
156 case TokenTypes.NUM_FLOAT:
157 case TokenTypes.NUM_DOUBLE:
158 result = (float) Double.parseDouble(aText);
159 break;
160 case TokenTypes.NUM_INT:
161 case TokenTypes.NUM_LONG:
162 int radix = BASE_10;
163 if (aText.startsWith("0x") || aText.startsWith("0X")) {
164 radix = BASE_16;
165 aText = aText.substring(2);
166 }
167 else if (aText.charAt(0) == '0') {
168 radix = BASE_8;
169 aText = aText.substring(1);
170 }
171 // Long.parseLong requires that the text ends with neither 'L'
172 // nor 'l'.
173 if ((aText.endsWith("L")) || (aText.endsWith("l"))) {
174 aText = aText.substring(0, aText.length() - 1);
175 }
176 if (aText.length() > 0) {
177 result = (float) Long.parseLong(aText, radix);
178 }
179 break;
180 default:
181 break;
182 }
183 return result;
184 }
185 }