Search in sources :

Example 1 with Token

use of net.sourceforge.pmd.lang.java.ast.Token in project pmd by pmd.

the class JavaTokenizer method tokenize.

public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder stringBuilder = sourceCode.getCodeBuffer();
    // Note that Java version is irrelevant for tokenizing
    LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4").getLanguageVersionHandler();
    String fileName = sourceCode.getFileName();
    TokenManager tokenMgr = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).getTokenManager(fileName, new StringReader(stringBuilder.toString()));
    Token currentToken = (Token) tokenMgr.getNextToken();
    TokenDiscarder discarder = new TokenDiscarder(ignoreAnnotations);
    ConstructorDetector constructorDetector = new ConstructorDetector(ignoreIdentifiers);
    while (currentToken.image.length() > 0) {
        discarder.updateState(currentToken);
        if (discarder.isDiscarding()) {
            currentToken = (Token) tokenMgr.getNextToken();
            continue;
        }
        processToken(tokenEntries, fileName, currentToken, constructorDetector);
        currentToken = (Token) tokenMgr.getNextToken();
    }
    tokenEntries.add(TokenEntry.getEOF());
}
Also used : StringReader(java.io.StringReader) Token(net.sourceforge.pmd.lang.java.ast.Token) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) TokenManager(net.sourceforge.pmd.lang.TokenManager)

Example 2 with Token

use of net.sourceforge.pmd.lang.java.ast.Token in project pmd by pmd.

the class AbstractCommentRuleTest method testFilteredCommentIn.

/**
 * Blank lines in comments should not raise an exception. See bug #1048.
 */
@Test
public void testFilteredCommentIn() {
    Token token = new Token();
    token.image = "/* multi line comment with blank lines\n\n\n */";
    String filtered = testSubject.filteredCommentIn(new MultiLineComment(token));
    assertEquals("multi line comment with blank lines", filtered);
    token.image = "/** a formal comment with blank lines\n\n\n */";
    filtered = testSubject.filteredCommentIn(new FormalComment(token));
    assertEquals("a formal comment with blank lines", filtered);
}
Also used : FormalComment(net.sourceforge.pmd.lang.java.ast.FormalComment) MultiLineComment(net.sourceforge.pmd.lang.java.ast.MultiLineComment) Token(net.sourceforge.pmd.lang.java.ast.Token) Test(org.junit.Test)

Aggregations

Token (net.sourceforge.pmd.lang.java.ast.Token)2 StringReader (java.io.StringReader)1 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)1 TokenManager (net.sourceforge.pmd.lang.TokenManager)1 FormalComment (net.sourceforge.pmd.lang.java.ast.FormalComment)1 MultiLineComment (net.sourceforge.pmd.lang.java.ast.MultiLineComment)1 Test (org.junit.Test)1