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.utils;
021
022import java.lang.reflect.Field;
023import java.lang.reflect.Modifier;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.Locale;
027import java.util.Map;
028import java.util.Optional;
029import java.util.ResourceBundle;
030import java.util.function.Predicate;
031import java.util.stream.Collectors;
032
033import com.puppycrawl.tools.checkstyle.api.DetailAST;
034import com.puppycrawl.tools.checkstyle.api.TokenTypes;
035
036/**
037 * Contains utility methods for tokens.
038 *
039 */
040public final class TokenUtil {
041
042    /** Maps from a token name to value. */
043    private static final Map<String, Integer> TOKEN_NAME_TO_VALUE;
044    /** Maps from a token value to name. */
045    private static final String[] TOKEN_VALUE_TO_NAME;
046
047    /** Array of all token IDs. */
048    private static final int[] TOKEN_IDS;
049
050    /** Prefix for exception when getting token by given id. */
051    private static final String TOKEN_ID_EXCEPTION_PREFIX = "given id ";
052
053    /** Prefix for exception when getting token by given name. */
054    private static final String TOKEN_NAME_EXCEPTION_PREFIX = "given name ";
055
056    // initialise the constants
057    static {
058        TOKEN_NAME_TO_VALUE = nameToValueMapFromPublicIntFields(TokenTypes.class);
059        TOKEN_VALUE_TO_NAME = valueToNameArrayFromNameToValueMap(TOKEN_NAME_TO_VALUE);
060        TOKEN_IDS = TOKEN_NAME_TO_VALUE.values().stream().mapToInt(Integer::intValue).toArray();
061    }
062
063    /** Stop instances being created. **/
064    private TokenUtil() {
065    }
066
067    /**
068     * Gets the value of a static or instance field of type int or of another primitive type
069     * convertible to type int via a widening conversion. Does not throw any checked exceptions.
070     * @param field from which the int should be extracted
071     * @param object to extract the int value from
072     * @return the value of the field converted to type int
073     * @throws IllegalStateException if this Field object is enforcing Java language access control
074     *         and the underlying field is inaccessible
075     * @see Field#getInt(Object)
076     */
077    public static int getIntFromField(Field field, Object object) {
078        try {
079            return field.getInt(object);
080        }
081        catch (final IllegalAccessException exception) {
082            throw new IllegalStateException(exception);
083        }
084    }
085
086    /**
087     * Creates a map of 'field name' to 'field value' from all {@code public} {@code int} fields
088     * of a class.
089     * @param cls source class
090     * @return unmodifiable name to value map
091     */
092    public static Map<String, Integer> nameToValueMapFromPublicIntFields(Class<?> cls) {
093        final Map<String, Integer> map = Arrays.stream(cls.getDeclaredFields())
094            .filter(fld -> Modifier.isPublic(fld.getModifiers()) && fld.getType() == Integer.TYPE)
095            .collect(Collectors.toMap(Field::getName, fld -> getIntFromField(fld, fld.getName())));
096        return Collections.unmodifiableMap(map);
097    }
098
099    /**
100     * Creates an array of map keys for quick value-to-name lookup for the map.
101     * @param map source map
102     * @return array of map keys
103     */
104    public static String[] valueToNameArrayFromNameToValueMap(Map<String, Integer> map) {
105        String[] valueToNameArray = CommonUtil.EMPTY_STRING_ARRAY;
106
107        for (Map.Entry<String, Integer> entry : map.entrySet()) {
108            final int value = entry.getValue();
109            // JavadocTokenTypes.EOF has value '-1' and is handled explicitly.
110            if (value >= 0) {
111                if (value >= valueToNameArray.length) {
112                    final String[] temp = new String[value + 1];
113                    System.arraycopy(valueToNameArray, 0, temp, 0, valueToNameArray.length);
114                    valueToNameArray = temp;
115                }
116                valueToNameArray[value] = entry.getKey();
117            }
118        }
119        return valueToNameArray;
120    }
121
122    /**
123     * Get total number of TokenTypes.
124     * @return total number of TokenTypes.
125     */
126    public static int getTokenTypesTotalNumber() {
127        return TOKEN_IDS.length;
128    }
129
130    /**
131     * Get all token IDs that are available in TokenTypes.
132     * @return array of token IDs
133     */
134    public static int[] getAllTokenIds() {
135        final int[] safeCopy = new int[TOKEN_IDS.length];
136        System.arraycopy(TOKEN_IDS, 0, safeCopy, 0, TOKEN_IDS.length);
137        return safeCopy;
138    }
139
140    /**
141     * Returns the name of a token for a given ID.
142     * @param id the ID of the token name to get
143     * @return a token name
144     */
145    public static String getTokenName(int id) {
146        if (id > TOKEN_VALUE_TO_NAME.length - 1) {
147            throw new IllegalArgumentException(TOKEN_ID_EXCEPTION_PREFIX + id);
148        }
149        final String name = TOKEN_VALUE_TO_NAME[id];
150        if (name == null) {
151            throw new IllegalArgumentException(TOKEN_ID_EXCEPTION_PREFIX + id);
152        }
153        return name;
154    }
155
156    /**
157     * Returns the ID of a token for a given name.
158     * @param name the name of the token ID to get
159     * @return a token ID
160     */
161    public static int getTokenId(String name) {
162        final Integer id = TOKEN_NAME_TO_VALUE.get(name);
163        if (id == null) {
164            throw new IllegalArgumentException(TOKEN_NAME_EXCEPTION_PREFIX + name);
165        }
166        return id;
167    }
168
169    /**
170     * Returns the short description of a token for a given name.
171     * @param name the name of the token ID to get
172     * @return a short description
173     */
174    public static String getShortDescription(String name) {
175        if (!TOKEN_NAME_TO_VALUE.containsKey(name)) {
176            throw new IllegalArgumentException(TOKEN_NAME_EXCEPTION_PREFIX + name);
177        }
178
179        final String tokenTypes =
180            "com.puppycrawl.tools.checkstyle.api.tokentypes";
181        final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
182        return bundle.getString(name);
183    }
184
185    /**
186     * Is argument comment-related type (SINGLE_LINE_COMMENT,
187     * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
188     * @param type
189     *        token type.
190     * @return true if type is comment-related type.
191     */
192    public static boolean isCommentType(int type) {
193        return type == TokenTypes.SINGLE_LINE_COMMENT
194                || type == TokenTypes.BLOCK_COMMENT_BEGIN
195                || type == TokenTypes.BLOCK_COMMENT_END
196                || type == TokenTypes.COMMENT_CONTENT;
197    }
198
199    /**
200     * Is argument comment-related type name (SINGLE_LINE_COMMENT,
201     * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
202     * @param type
203     *        token type name.
204     * @return true if type is comment-related type name.
205     */
206    public static boolean isCommentType(String type) {
207        return isCommentType(getTokenId(type));
208    }
209
210    /**
211     * Finds the first {@link Optional} child token of {@link DetailAST} root node
212     * which matches the given predicate.
213     * @param root root node.
214     * @param predicate predicate.
215     * @return {@link Optional} of {@link DetailAST} node which matches the predicate.
216     */
217    public static Optional<DetailAST> findFirstTokenByPredicate(DetailAST root,
218                                                                Predicate<DetailAST> predicate) {
219        Optional<DetailAST> result = Optional.empty();
220        for (DetailAST ast = root.getFirstChild(); ast != null; ast = ast.getNextSibling()) {
221            if (predicate.test(ast)) {
222                result = Optional.of(ast);
223                break;
224            }
225        }
226        return result;
227    }
228
229}