本文整理汇总了Java中com.puppycrawl.tools.checkstyle.api.DetailAST.addChild方法的典型用法代码示例。如果您正苦于以下问题:Java DetailAST.addChild方法的具体用法?Java DetailAST.addChild怎么用?Java DetailAST.addChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.puppycrawl.tools.checkstyle.api.DetailAST
的用法示例。
在下文中一共展示了DetailAST.addChild方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSlCommentNode
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Create single-line comment from token.
* @param token
* Token object.
* @return DetailAST with SINGLE_LINE_COMMENT type.
*/
private static DetailAST createSlCommentNode(Token token) {
final DetailAST slComment = new DetailAST();
slComment.setType(TokenTypes.SINGLE_LINE_COMMENT);
slComment.setText("//");
// column counting begins from 0
slComment.setColumnNo(token.getColumn() - 1);
slComment.setLineNo(token.getLine());
final DetailAST slCommentContent = new DetailAST();
slCommentContent.setType(TokenTypes.COMMENT_CONTENT);
// column counting begins from 0
// plus length of '//'
slCommentContent.setColumnNo(token.getColumn() - 1 + 2);
slCommentContent.setLineNo(token.getLine());
slCommentContent.setText(token.getText());
slComment.addChild(slCommentContent);
return slComment;
}
示例2: testStatefulFieldsClearedOnBeginTree2
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree2() throws Exception {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.LITERAL_RETURN);
ast.setLineNo(5);
final DetailAST child = new DetailAST();
child.setType(TokenTypes.SEMI);
ast.addChild(child);
final NPathComplexityCheck check = new NPathComplexityCheck();
Assert.assertTrue("Stateful field is not cleared after beginTree",
TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "isAfterValues",
new Predicate<Object>() {
@Override
public boolean test(Object isAfterValues) {
return ((Collection<Context>) isAfterValues).isEmpty();
}
}));
}
示例3: testVisitTokenSwitchReflection
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testVisitTokenSwitchReflection() {
//unexpected parent for ARRAY_DECLARATOR token
final DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile");
final DetailAST astArrayDeclarator = mockAST(TokenTypes.ARRAY_DECLARATOR, "[", "mockfile");
final DetailAST astRightBracket = mockAST(TokenTypes.RBRACK, "[", "mockfile");
astImport.addChild(astArrayDeclarator);
astArrayDeclarator.addChild(astRightBracket);
final NoWhitespaceAfterCheck check = new NoWhitespaceAfterCheck();
try {
check.visitToken(astArrayDeclarator);
fail("no intended exception thrown");
}
catch (IllegalStateException ex) {
assertEquals("Invalid exception message",
"unexpected ast syntax import[0x-1]", ex.getMessage());
}
}
示例4: testVisitTokenSwitchReflection
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test(expected = IllegalStateException.class)
public void testVisitTokenSwitchReflection() {
// Create mock ast
final DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile", 0, 0);
final DetailAST astIdent = mockAST(TokenTypes.IDENT, "myTestImport", "mockfile", 0, 0);
astImport.addChild(astIdent);
final DetailAST astSemi = mockAST(TokenTypes.SEMI, ";", "mockfile", 0, 0);
astIdent.addNextSibling(astSemi);
// Set unsupported option
final ImportOrderCheck mock = new ImportOrderCheck();
final ImportOrderOption importOrderOptionMock = PowerMockito.mock(ImportOrderOption.class);
Whitebox.setInternalState(importOrderOptionMock, "name", "NEW_OPTION_FOR_UT");
Whitebox.setInternalState(importOrderOptionMock, "ordinal", 5);
Whitebox.setInternalState(mock, "option", importOrderOptionMock);
// expecting IllegalStateException
mock.visitToken(astImport);
}
示例5: createBlockCommentNode
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
/**
* Create block comment from token.
* @param token
* Token object.
* @return DetailAST with BLOCK_COMMENT type.
*/
public static DetailAST createBlockCommentNode(Token token) {
final DetailAST blockComment = new DetailAST();
blockComment.initialize(TokenTypes.BLOCK_COMMENT_BEGIN, BLOCK_MULTIPLE_COMMENT_BEGIN);
// column counting begins from 0
blockComment.setColumnNo(token.getColumn() - 1);
blockComment.setLineNo(token.getLine());
final DetailAST blockCommentContent = new DetailAST();
blockCommentContent.setType(TokenTypes.COMMENT_CONTENT);
// column counting begins from 0
// plus length of '/*'
blockCommentContent.setColumnNo(token.getColumn() - 1 + 2);
blockCommentContent.setLineNo(token.getLine());
blockCommentContent.setText(token.getText());
final DetailAST blockCommentClose = new DetailAST();
blockCommentClose.initialize(TokenTypes.BLOCK_COMMENT_END, BLOCK_MULTIPLE_COMMENT_END);
final Map.Entry<Integer, Integer> linesColumns = countLinesColumns(
token.getText(), token.getLine(), token.getColumn());
blockCommentClose.setLineNo(linesColumns.getKey());
blockCommentClose.setColumnNo(linesColumns.getValue());
blockComment.addChild(blockCommentContent);
blockComment.addChild(blockCommentClose);
return blockComment;
}
示例6: testVisitTokenBeforeExpressionRange
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testVisitTokenBeforeExpressionRange() {
// Create first ast
final DetailAST astIf = mockAST(TokenTypes.LITERAL_IF, "if", "mockfile", 2, 2);
final DetailAST astIfLeftParen = mockAST(TokenTypes.LPAREN, "(", "mockfile", 3, 3);
astIf.addChild(astIfLeftParen);
final DetailAST astIfTrue =
mockAST(TokenTypes.LITERAL_TRUE, "true", "mockfile", 3, 3);
astIf.addChild(astIfTrue);
final DetailAST astIfRightParen = mockAST(TokenTypes.RPAREN, ")", "mockfile", 4, 4);
astIf.addChild(astIfRightParen);
// Create ternary ast
final DetailAST astTernary = mockAST(TokenTypes.QUESTION, "?", "mockfile", 1, 1);
final DetailAST astTernaryTrue =
mockAST(TokenTypes.LITERAL_TRUE, "true", "mockfile", 1, 2);
astTernary.addChild(astTernaryTrue);
final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck();
// visiting first ast, set expressionSpatialRange to [2,2 - 4,4]
npathComplexityCheckObj.visitToken(astIf);
final SortedSet<LocalizedMessage> messages1 = npathComplexityCheckObj.getMessages();
Assert.assertEquals("No exception messages expected", 0, messages1.size());
//visiting ternary, it lies before expressionSpatialRange
npathComplexityCheckObj.visitToken(astTernary);
final SortedSet<LocalizedMessage> messages2 = npathComplexityCheckObj.getMessages();
Assert.assertEquals("No exception messages expected", 0, messages2.size());
}
示例7: testGetAllAnnotationValuesWrongArg
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testGetAllAnnotationValuesWrongArg() throws Exception {
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Method getAllAnnotationValues = holder.getClass()
.getDeclaredMethod("getAllAnnotationValues", DetailAST.class);
getAllAnnotationValues.setAccessible(true);
final DetailAST methodDef = new DetailAST();
methodDef.setType(TokenTypes.METHOD_DEF);
methodDef.setText("Method Def");
methodDef.setLineNo(0);
methodDef.setColumnNo(0);
final DetailAST lparen = new DetailAST();
lparen.setType(TokenTypes.LPAREN);
final DetailAST parent = new DetailAST();
parent.addChild(lparen);
parent.addChild(methodDef);
try {
getAllAnnotationValues.invoke(holder, parent);
fail("Exception expected");
}
catch (InvocationTargetException ex) {
assertTrue("Error type is unexpected",
ex.getCause() instanceof IllegalArgumentException);
assertEquals("Error message is unexpected",
"Unexpected AST: Method Def[0x0]", ex.getCause().getMessage());
}
}
示例8: testGetAnnotationTargetWrongArg
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testGetAnnotationTargetWrongArg() throws Exception {
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Method getAnnotationTarget = holder.getClass()
.getDeclaredMethod("getAnnotationTarget", DetailAST.class);
getAnnotationTarget.setAccessible(true);
final DetailAST methodDef = new DetailAST();
methodDef.setType(TokenTypes.METHOD_DEF);
methodDef.setText("Method Def");
final DetailAST parent = new DetailAST();
parent.setType(TokenTypes.ASSIGN);
parent.setText("Parent ast");
parent.addChild(methodDef);
parent.setLineNo(0);
parent.setColumnNo(0);
try {
getAnnotationTarget.invoke(holder, methodDef);
fail("Exception expected");
}
catch (InvocationTargetException ex) {
assertTrue("Error type is unexpected",
ex.getCause() instanceof IllegalArgumentException);
assertEquals("Error message is unexpected",
"Unexpected container AST: Parent ast[0x0]", ex.getCause().getMessage());
}
}
示例9: getNode
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
private static DetailAST getNode(int... nodeTypes) {
DetailAST ast = new DetailAST();
ast.setType(nodeTypes[0]);
for (int i = 1; i < nodeTypes.length; i++) {
final DetailAST astChild = new DetailAST();
astChild.setType(nodeTypes[i]);
ast.addChild(astChild);
ast = astChild;
}
return ast;
}
示例10: testContainsAnnotationFalse2
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testContainsAnnotationFalse2() {
final DetailAST ast = new DetailAST();
ast.setType(1);
final DetailAST ast2 = new DetailAST();
ast2.setType(TokenTypes.MODIFIERS);
ast.addChild(ast2);
Assert.assertFalse("AnnotationUtility should not contain " + ast,
AnnotationUtility.containsAnnotation(ast));
}
示例11: testContainsAnnotationTrue
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testContainsAnnotationTrue() {
final DetailAST ast = new DetailAST();
ast.setType(1);
final DetailAST ast2 = new DetailAST();
ast2.setType(TokenTypes.MODIFIERS);
ast.addChild(ast2);
final DetailAST ast3 = new DetailAST();
ast3.setType(TokenTypes.ANNOTATION);
ast2.addChild(ast3);
assertTrue("AnnotationUtility should contain " + ast,
AnnotationUtility.containsAnnotation(ast));
}
示例12: testElseWithCurly
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testElseWithCurly() {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.ASSIGN);
ast.setText("ASSIGN");
assertFalse("Invalid elseIf check result 'ASSIGN' is not 'else if'",
CheckUtils.isElseIf(ast));
final DetailAST parentAst = new DetailAST();
parentAst.setType(TokenTypes.LCURLY);
parentAst.setText("LCURLY");
final DetailAST ifAst = new DetailAST();
ifAst.setType(TokenTypes.LITERAL_IF);
ifAst.setText("IF");
parentAst.addChild(ifAst);
assertFalse("Invalid elseIf check result: 'IF' is not 'else if'",
CheckUtils.isElseIf(ifAst));
final DetailAST parentAst2 = new DetailAST();
parentAst2.setType(TokenTypes.SLIST);
parentAst2.setText("SLIST");
parentAst2.addChild(ifAst);
assertFalse("Invalid elseIf check result: 'SLIST' is not 'else if'",
CheckUtils.isElseIf(ifAst));
final DetailAST elseAst = new DetailAST();
elseAst.setType(TokenTypes.LITERAL_ELSE);
elseAst.setFirstChild(ifAst);
assertTrue("Invalid elseIf check result", CheckUtils.isElseIf(ifAst));
}
示例13: testGetFirstNode1
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testGetFirstNode1() {
final DetailAST child = new DetailAST();
child.setLineNo(5);
child.setColumnNo(6);
final DetailAST root = new DetailAST();
root.setLineNo(5);
root.setColumnNo(6);
root.addChild(child);
assertEquals("Unexpected node", root, CheckUtils.getFirstNode(root));
}
示例14: testGetFirstNode2
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testGetFirstNode2() {
final DetailAST child = new DetailAST();
child.setLineNo(6);
child.setColumnNo(5);
final DetailAST root = new DetailAST();
root.setLineNo(5);
root.setColumnNo(6);
root.addChild(child);
assertEquals("Unexpected node", root, CheckUtils.getFirstNode(root));
}
示例15: testNullClassLoader
import com.puppycrawl.tools.checkstyle.api.DetailAST; //导入方法依赖的package包/类
@Test
public void testNullClassLoader() throws Exception {
final DetailAST exprAst = new DetailAST();
exprAst.setType(TokenTypes.EXPR);
final DetailAST newAst = new DetailAST();
newAst.setType(TokenTypes.LITERAL_NEW);
newAst.setLineNo(1);
newAst.setColumnNo(1);
final DetailAST identAst = new DetailAST();
identAst.setType(TokenTypes.IDENT);
identAst.setText("Boolean");
final DetailAST lparenAst = new DetailAST();
lparenAst.setType(TokenTypes.LPAREN);
final DetailAST elistAst = new DetailAST();
elistAst.setType(TokenTypes.ELIST);
final DetailAST rparenAst = new DetailAST();
rparenAst.setType(TokenTypes.RPAREN);
exprAst.addChild(newAst);
newAst.addChild(identAst);
identAst.setNextSibling(lparenAst);
lparenAst.setNextSibling(elistAst);
elistAst.setNextSibling(rparenAst);
final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
final File inputFile = new File(getNonCompilablePath("InputIllegalInstantiationLang.java"));
check.setFileContents(new FileContents(new FileText(inputFile,
StandardCharsets.UTF_8.name())));
check.configure(createModuleConfig(IllegalInstantiationCheck.class));
check.setClasses("java.lang.Boolean");
check.visitToken(newAst);
final SortedSet<LocalizedMessage> messages1 = check.getMessages();
Assert.assertEquals("No exception messages expected", 0, messages1.size());
check.finishTree(newAst);
final SortedSet<LocalizedMessage> messages2 = check.getMessages();
final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
"com.puppycrawl.tools.checkstyle.checks.coding.messages", "instantiation.avoid",
new String[] {"java.lang.Boolean"}, null,
getClass(), null);
Assert.assertEquals("Invalid exception message",
addExceptionMessage.getMessage(),
messages2.first().getMessage());
}