001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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.util.Locale;
024import java.util.ResourceBundle;
025
026import com.google.common.collect.ImmutableMap;
027import com.google.common.primitives.Ints;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029
030/**
031 * Contains utility methods for tokens.
032 *
033 * @author <a href="mailto:nesterenko-aleksey@list.ru">Aleksey Nesterenko</a>
034 */
035public final class TokenUtils {
036
037    /** Maps from a token name to value. */
038    private static final ImmutableMap<String, Integer> TOKEN_NAME_TO_VALUE;
039    /** Maps from a token value to name. */
040    private static final String[] TOKEN_VALUE_TO_NAME;
041
042    /** Array of all token IDs. */
043    private static final int[] TOKEN_IDS;
044
045    /** Prefix for exception when getting token by given id. */
046    private static final String TOKEN_ID_EXCEPTION_PREFIX = "given id ";
047
048    /** Prefix for exception when getting token by given name. */
049    private static final String TOKEN_NAME_EXCEPTION_PREFIX = "given name ";
050
051    // initialise the constants
052    static {
053        final ImmutableMap.Builder<String, Integer> builder =
054                ImmutableMap.builder();
055        final Field[] fields = TokenTypes.class.getDeclaredFields();
056        String[] tempTokenValueToName = CommonUtils.EMPTY_STRING_ARRAY;
057        for (final Field field : fields) {
058            // Only process the int declarations.
059            if (field.getType() != Integer.TYPE) {
060                continue;
061            }
062
063            final String name = field.getName();
064            final int tokenValue = getIntFromField(field, name);
065            builder.put(name, tokenValue);
066            if (tokenValue > tempTokenValueToName.length - 1) {
067                final String[] temp = new String[tokenValue + 1];
068                System.arraycopy(tempTokenValueToName, 0,
069                        temp, 0, tempTokenValueToName.length);
070                tempTokenValueToName = temp;
071            }
072            tempTokenValueToName[tokenValue] = name;
073        }
074
075        TOKEN_NAME_TO_VALUE = builder.build();
076        TOKEN_VALUE_TO_NAME = tempTokenValueToName;
077        TOKEN_IDS = Ints.toArray(TOKEN_NAME_TO_VALUE.values());
078    }
079
080    /** Stop instances being created. **/
081    private TokenUtils() {
082    }
083
084    /**
085     * Gets the value of a static or instance field of type int or of another primitive type
086     * convertible to type int via a widening conversion. Does not throw any checked exceptions.
087     * @param field from which the int should be extracted
088     * @param object to extract the int value from
089     * @return the value of the field converted to type int
090     * @throws IllegalStateException if this Field object is enforcing Java language access control
091     *         and the underlying field is inaccessible
092     * @see Field#getInt(Object)
093     */
094    public static int getIntFromField(Field field, Object object) {
095        try {
096            return field.getInt(object);
097        }
098        catch (final IllegalAccessException exception) {
099            throw new IllegalStateException(exception);
100        }
101    }
102
103    /**
104     * Get all token IDs that are available in TokenTypes.
105     * @return array of token IDs
106     */
107    public static int[] getAllTokenIds() {
108        final int[] safeCopy = new int[TOKEN_IDS.length];
109        System.arraycopy(TOKEN_IDS, 0, safeCopy, 0, TOKEN_IDS.length);
110        return safeCopy;
111    }
112
113    /**
114     * Returns the name of a token for a given ID.
115     * @param id the ID of the token name to get
116     * @return a token name
117     */
118    public static String getTokenName(int id) {
119        if (id > TOKEN_VALUE_TO_NAME.length - 1) {
120            throw new IllegalArgumentException(TOKEN_ID_EXCEPTION_PREFIX + id);
121        }
122        final String name = TOKEN_VALUE_TO_NAME[id];
123        if (name == null) {
124            throw new IllegalArgumentException(TOKEN_ID_EXCEPTION_PREFIX + id);
125        }
126        return name;
127    }
128
129    /**
130     * Returns the ID of a token for a given name.
131     * @param name the name of the token ID to get
132     * @return a token ID
133     */
134    public static int getTokenId(String name) {
135        final Integer id = TOKEN_NAME_TO_VALUE.get(name);
136        if (id == null) {
137            throw new IllegalArgumentException(TOKEN_NAME_EXCEPTION_PREFIX + name);
138        }
139        return id;
140    }
141
142    /**
143     * Returns the short description of a token for a given name.
144     * @param name the name of the token ID to get
145     * @return a short description
146     */
147    public static String getShortDescription(String name) {
148        if (!TOKEN_NAME_TO_VALUE.containsKey(name)) {
149            throw new IllegalArgumentException(TOKEN_NAME_EXCEPTION_PREFIX + name);
150        }
151
152        final String tokenTypes =
153            "com.puppycrawl.tools.checkstyle.api.tokentypes";
154        final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
155        return bundle.getString(name);
156    }
157
158    /**
159     * Is argument comment-related type (SINGLE_LINE_COMMENT,
160     * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
161     * @param type
162     *        token type.
163     * @return true if type is comment-related type.
164     */
165    public static boolean isCommentType(int type) {
166        return type == TokenTypes.SINGLE_LINE_COMMENT
167                || type == TokenTypes.BLOCK_COMMENT_BEGIN
168                || type == TokenTypes.BLOCK_COMMENT_END
169                || type == TokenTypes.COMMENT_CONTENT;
170    }
171
172    /**
173     * Is argument comment-related type name (SINGLE_LINE_COMMENT,
174     * BLOCK_COMMENT_BEGIN, BLOCK_COMMENT_END, COMMENT_CONTENT).
175     * @param type
176     *        token type name.
177     * @return true if type is comment-related type name.
178     */
179    public static boolean isCommentType(String type) {
180        return isCommentType(getTokenId(type));
181    }
182
183}