private static DetailAST create(int tokenType, String text) { final DetailAST ast = create(tokenType); ast.setText(text); return ast; }
/** * Create block comment from string content. * @param content comment content. * @return DetailAST block comment */ public static DetailAST createBlockCommentNode(String content) { final DetailAST blockCommentBegin = new DetailAST(); blockCommentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN); blockCommentBegin.setText(BLOCK_MULTIPLE_COMMENT_BEGIN); blockCommentBegin.setLineNo(0); blockCommentBegin.setColumnNo(-JAVADOC_START.length()); final DetailAST commentContent = new DetailAST(); commentContent.setType(TokenTypes.COMMENT_CONTENT); commentContent.setText("*" + content); commentContent.setLineNo(0); // javadoc should starts at 0 column, so COMMENT_CONTENT node // that contains javadoc identifier has -1 column commentContent.setColumnNo(-1); final DetailAST blockCommentEnd = new DetailAST(); blockCommentEnd.setType(TokenTypes.BLOCK_COMMENT_END); blockCommentEnd.setText(BLOCK_MULTIPLE_COMMENT_END); blockCommentBegin.setFirstChild(commentContent); commentContent.setNextSibling(blockCommentEnd); return blockCommentBegin; }
@Test public void testUnusedMethod() throws Exception { final DetailAST ident = new DetailAST(); ident.setText("testName"); final Class<?> cls = Class.forName(RequireThisCheck.class.getName() + "$CatchFrame"); final Constructor<?> constructor = cls.getDeclaredConstructors()[0]; constructor.setAccessible(true); final Object o = constructor.newInstance(null, ident); Assert.assertEquals("expected ident token", ident, TestUtil.getClassDeclaredMethod(cls, "getFrameNameIdent").invoke(o)); Assert.assertEquals("expected catch frame type", "CATCH_FRAME", TestUtil.getClassDeclaredMethod(cls, "getType").invoke(o).toString()); }
@Override public void initialize(AST ast) { final DetailAST detailAst = (DetailAST) ast; setText(detailAst.getText()); setType(detailAst.getType()); lineNo = detailAst.getLineNo(); columnNo = detailAst.getColumnNo(); hiddenAfter = detailAst.getHiddenAfter(); hiddenBefore = detailAst.getHiddenBefore(); }
@Test public void testGetAnnotationValuesWrongArg() throws ReflectiveOperationException { final SuppressWarningsHolder holder = new SuppressWarningsHolder(); final Method getAllAnnotationValues = holder.getClass() .getDeclaredMethod("getAnnotationValues", 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); try { getAllAnnotationValues.invoke(holder, methodDef); fail("Exception expected"); } catch (InvocationTargetException ex) { assertTrue("Error type is unexpected", ex.getCause() instanceof IllegalArgumentException); assertEquals("Error message is unexpected", "Expression or annotation array" + " initializer AST expected: Method Def[0x0]", ex.getCause().getMessage()); } }
/** * Create single-line comment from token. * @param token to create the AST * @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; }
@Test public void testEmptyBlockCommentAst() { final DetailAST commentBegin = new DetailAST(); commentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN); commentBegin.setText("/*"); final DetailAST commentContent = new DetailAST(); commentContent.setType(TokenTypes.COMMENT_CONTENT); commentContent.setText(""); final DetailAST commentEnd = new DetailAST(); commentEnd.setType(TokenTypes.BLOCK_COMMENT_END); commentEnd.setText("*/"); commentBegin.setFirstChild(commentContent); commentContent.setNextSibling(commentEnd); assertFalse("Should return false when empty block comment is passed", JavadocUtil.isJavadocComment(commentBegin)); }
@Test public void testFindFirstTokenByPredicate() { final DetailAST astForTest = new DetailAST(); final DetailAST child = new DetailAST(); final DetailAST firstSibling = new DetailAST(); final DetailAST secondSibling = new DetailAST(); final DetailAST thirdSibling = new DetailAST(); firstSibling.setText("first"); secondSibling.setText("second"); thirdSibling.setText("third"); secondSibling.setNextSibling(thirdSibling); firstSibling.setNextSibling(secondSibling); child.setNextSibling(firstSibling); astForTest.setFirstChild(child); final Optional<DetailAST> result = TokenUtil.findFirstTokenByPredicate(astForTest, ast -> "second".equals(ast.getText())); assertEquals("Invalid second sibling", secondSibling, result.orElse(null)); }
private static DetailAST getNodeWithParentScope(int literal, String scope, int parentTokenType) { final DetailAST ast = getNode(parentTokenType, TokenTypes.MODIFIERS, literal); ast.setText(scope); final DetailAST ast2 = getNode(TokenTypes.OBJBLOCK); ast.getParent().getParent().addChild(ast2); return ast; }
@Test public void testGetJavadocCommentContent() { final DetailAST detailAST = new DetailAST(); final DetailAST javadoc = new DetailAST(); javadoc.setText("1javadoc"); detailAST.setFirstChild(javadoc); final String commentContent = JavadocUtil.getJavadocCommentContent(detailAST); assertEquals("Invalid comment content", "javadoc", commentContent); }
@Test public void testGetAllAnnotationValuesWrongArg() throws ReflectiveOperationException { 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()); } }
@Test public void testVisitToken() { final CommentsIndentationCheck check = new CommentsIndentationCheck(); final DetailAST methodDef = new DetailAST(); methodDef.setType(TokenTypes.METHOD_DEF); methodDef.setText("methodStub"); try { check.visitToken(methodDef); Assert.fail("IllegalArgumentException should have been thrown!"); } catch (IllegalArgumentException ex) { final String msg = ex.getMessage(); Assert.assertEquals("Invalid exception message", "Unexpected token type: methodStub", msg); } }
@Test public void testToString() { final DetailAST ast = new DetailAST(); ast.setText("text"); ast.setColumnNo(0); ast.setLineNo(0); assertEquals("Invalid text", "text[0x0]", ast.toString()); }
private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.DOT); ast.setColumnNo(1); ast.setLineNo(2); ast.setText("Root"); final DetailAST ast2 = new DetailAST(); ast2.setType(TokenTypes.LE); ast2.setColumnNo(columnNo); ast2.setLineNo(lineNo); ast2.setText("MyTestik"); final DetailAST ast1 = new DetailAST(); ast1.setType(TokenTypes.LITERAL_NEW); ast1.setColumnNo(14); ast1.setLineNo(15); ast1.setText("MyTest"); ast.addChild(ast1); ast.addChild(ast2); return FullIdent.createFullIdent(ast); }
@Test public void testSerialData() { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.METHOD_DEF); final DetailAST astChild = new DetailAST(); astChild.setType(TokenTypes.IDENT); astChild.setText("writeObject"); ast.setFirstChild(astChild); final String[] validNames = { "writeObject", "readObject", "writeExternal", "readExternal", "writeReplace", "readResolve", }; for (String name: validNames) { astChild.setText(name); assertTrue("Invalid ast type for current tag: " + ast.getType(), JavadocTagInfo.SERIAL_DATA.isValidOn(ast)); } astChild.setText("1111"); assertFalse("Should return false when ast type is invalid for current tag", JavadocTagInfo.SERIAL_DATA.isValidOn(ast)); ast.setType(TokenTypes.LAMBDA); assertFalse("Should return false when ast type is invalid for current tag", JavadocTagInfo.SERIAL_DATA.isValidOn(ast)); }
@Test public void testEmptyJavadocCommentAst() { final DetailAST commentBegin = new DetailAST(); commentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN); commentBegin.setText("/*"); final DetailAST javadocCommentContent = new DetailAST(); javadocCommentContent.setType(TokenTypes.COMMENT_CONTENT); javadocCommentContent.setText("*"); final DetailAST commentEnd = new DetailAST(); commentEnd.setType(TokenTypes.BLOCK_COMMENT_END); commentEnd.setText("*/"); commentBegin.setFirstChild(javadocCommentContent); javadocCommentContent.setNextSibling(commentEnd); final DetailAST commentBeginParent = new DetailAST(); commentBeginParent.setType(TokenTypes.MODIFIERS); commentBeginParent.setFirstChild(commentBegin); final DetailAST aJavadocPosition = new DetailAST(); aJavadocPosition.setType(TokenTypes.METHOD_DEF); aJavadocPosition.setFirstChild(commentBeginParent); assertTrue("Should return true when empty javadoc comment ast is passed", JavadocUtil.isJavadocComment(commentBegin)); }
@Test public void testToString() { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.LITERAL_NEW); ast.setColumnNo(14); ast.setLineNo(15); ast.setText("MyTest"); final FullIdent indent = FullIdent.createFullIdent(ast); Assert.assertEquals("Invalid full indent", "MyTest[15x14]", indent.toString()); }
@Test public void testContainsAnnotation() { final DetailAST astForTest = new DetailAST(); astForTest.setType(TokenTypes.PACKAGE_DEF); final DetailAST child = new DetailAST(); final DetailAST annotations = new DetailAST(); final DetailAST annotation = new DetailAST(); final DetailAST annotationNameHolder = new DetailAST(); final DetailAST annotationName = new DetailAST(); annotations.setType(TokenTypes.ANNOTATIONS); annotation.setType(TokenTypes.ANNOTATION); annotationNameHolder.setType(TokenTypes.AT); annotationName.setText("Annotation"); annotationNameHolder.setNextSibling(annotationName); annotation.setFirstChild(annotationNameHolder); annotations.setFirstChild(annotation); child.setNextSibling(annotations); astForTest.setFirstChild(child); assertTrue("Annotation should contain " + astForTest, AnnotationUtil.containsAnnotation(astForTest, "Annotation")); }
@Test public void testContainsAnnotationWithStringFalse() { final DetailAST astForTest = new DetailAST(); astForTest.setType(TokenTypes.PACKAGE_DEF); final DetailAST child = new DetailAST(); final DetailAST annotations = new DetailAST(); final DetailAST annotation = new DetailAST(); final DetailAST annotationNameHolder = new DetailAST(); final DetailAST annotationName = new DetailAST(); annotations.setType(TokenTypes.ANNOTATIONS); annotation.setType(TokenTypes.ANNOTATION); annotationNameHolder.setType(TokenTypes.AT); annotationName.setText("Annotation"); annotationNameHolder.setNextSibling(annotationName); annotation.setFirstChild(annotationNameHolder); annotations.setFirstChild(annotation); child.setNextSibling(annotations); astForTest.setFirstChild(child); assertFalse("Annotation should not contain " + astForTest, AnnotationUtil.containsAnnotation(astForTest, "AnnotationBad")); }
@Test public void testContainsAnnotationWithComment() { final DetailAST astForTest = new DetailAST(); astForTest.setType(TokenTypes.PACKAGE_DEF); final DetailAST child = new DetailAST(); final DetailAST annotations = new DetailAST(); final DetailAST annotation = new DetailAST(); final DetailAST annotationNameHolder = new DetailAST(); final DetailAST annotationName = new DetailAST(); final DetailAST comment = new DetailAST(); annotations.setType(TokenTypes.ANNOTATIONS); annotation.setType(TokenTypes.ANNOTATION); annotationNameHolder.setType(TokenTypes.AT); comment.setType(TokenTypes.BLOCK_COMMENT_BEGIN); annotationName.setText("Annotation"); annotationNameHolder.setNextSibling(annotationName); annotation.setFirstChild(comment); comment.setNextSibling(annotationNameHolder); annotations.setFirstChild(annotation); child.setNextSibling(annotations); astForTest.setFirstChild(child); assertTrue("Annotation should contain " + astForTest, AnnotationUtil.containsAnnotation(astForTest, "Annotation")); }