com.liferay.source.formatter.checkstyle.util.DetailASTUtil Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.source.formatter.checkstyle.util;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import java.util.ArrayList;
import java.util.List;
/**
* @author Hugo Huijser
*/
public class DetailASTUtil {
public static final int ALL_TYPES = -1;
public static List getAllChildTokens(
DetailAST detailAST, boolean recursive, int... tokenTypes) {
return _getAllChildTokens(detailAST, recursive, null, tokenTypes);
}
private static List _getAllChildTokens(
DetailAST detailAST, boolean recursive, List list,
int... tokenTypes) {
if (list == null) {
list = new ArrayList<>();
}
DetailAST childDetailAST = detailAST.getFirstChild();
while (childDetailAST != null) {
if (ArrayUtil.contains(tokenTypes, childDetailAST.getType()) ||
ArrayUtil.contains(tokenTypes, ALL_TYPES)) {
list.add(childDetailAST);
}
if (recursive) {
list = _getAllChildTokens(
childDetailAST, recursive, list, tokenTypes);
}
childDetailAST = childDetailAST.getNextSibling();
}
return list;
}
}