Search in sources :

Example 21 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class SubstitutionVisitor method addGeneratedByComment.

private static void addGeneratedByComment(CtElement ele, String generatedBy) {
    if (generatedBy == null) {
        return;
    }
    String EOL = System.getProperty("line.separator");
    CtComment comment = getJavaDoc(ele);
    String content = comment.getContent();
    if (content.trim().length() > 0) {
        content += EOL + EOL;
    }
    content += generatedBy;
    comment.setContent(content);
}
Also used : CtComment(spoon.reflect.code.CtComment)

Example 22 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class SubstitutionVisitor method getJavaDoc.

private static CtComment getJavaDoc(CtElement ele) {
    for (CtComment comment : ele.getComments()) {
        if (comment.getCommentType() == CtComment.CommentType.JAVADOC) {
            return comment;
        }
    }
    CtComment c = ele.getFactory().Code().createComment("", CtComment.CommentType.JAVADOC);
    ele.addComment(c);
    return c;
}
Also used : CtComment(spoon.reflect.code.CtComment)

Example 23 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class TestModule method testModuleInfoWithComments.

@Test
public void testModuleInfoWithComments() {
    // contract: documentation on module-info elements should be managed
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.getEnvironment().setCommentEnabled(true);
    launcher.addInputResource(MODULE_RESOURCES_PATH + "/module_with_comments/module-info.java");
    launcher.buildModel();
    assertEquals(2, launcher.getModel().getAllModules().size());
    CtModule module = launcher.getFactory().Module().getModule("module_with_comments");
    assertNotNull(module);
    assertTrue(module.isOpenModule());
    List<CtComment> comments = module.getComments();
    assertEquals(1, comments.size());
    CtComment comment = comments.get(0);
    assertEquals("This is the main module of the application", comment.getContent());
    assertEquals(CtComment.CommentType.JAVADOC, comment.getCommentType());
    assertEquals(3, module.getModuleDirectives().size());
    CtModuleRequirement moduleRequirement = module.getRequiredModules().get(0);
    comments = moduleRequirement.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("this is needed for logging stuff", comment.getContent());
    assertEquals(CtComment.CommentType.INLINE, comment.getCommentType());
    CtProvidedService providedService = module.getProvidedServices().get(0);
    comments = providedService.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("A specific implementation", comment.getContent());
    assertEquals(CtComment.CommentType.JAVADOC, comment.getCommentType());
    CtUsedService usedService = module.getUsedServices().get(0);
    comments = usedService.getComments();
    assertEquals(1, comments.size());
    comment = comments.get(0);
    assertEquals("A simple implementation", comment.getContent());
    assertEquals(CtComment.CommentType.BLOCK, comment.getCommentType());
}
Also used : CtComment(spoon.reflect.code.CtComment) CtProvidedService(spoon.reflect.declaration.CtProvidedService) Launcher(spoon.Launcher) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtUsedService(spoon.reflect.declaration.CtUsedService) Test(org.junit.Test)

Example 24 with CtComment

use of spoon.reflect.code.CtComment in project spoon by INRIA.

the class PrinterTest method testPrinterTokenListener.

@Test
public void testPrinterTokenListener() throws Exception {
    Launcher spoon = new Launcher();
    Factory factory = spoon.createFactory();
    spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/annotation/testclasses/", "./src/test/java/spoon/test/prettyprinter/")).build();
    assertTrue(factory.Type().getAll().size() > 0);
    for (CtType<?> t : factory.Type().getAll()) {
        // create DefaultJavaPrettyPrinter with standard DefaultTokenWriter
        DefaultJavaPrettyPrinter pp = new DefaultJavaPrettyPrinter(factory.getEnvironment());
        pp.calculate(t.getPosition().getCompilationUnit(), Collections.singletonList(t));
        // result of printing using standard DefaultTokenWriter
        String standardPrintedResult = pp.getResult();
        StringBuilder allTokens = new StringBuilder();
        // print type with custom listener
        // 1) register custom TokenWriter which checks the TokenWriter contract
        pp.setPrinterTokenWriter(new TokenWriter() {

            String lastToken;

            PrinterHelper printerHelper = new PrinterHelper(factory.getEnvironment());

            @Override
            public TokenWriter writeSeparator(String separator) {
                checkRepeatingOfTokens("writeSeparator");
                checkTokenWhitespace(separator, false);
                // one of the separators
                assertTrue("Unexpected separator: " + separator, separators.contains(separator));
                handleTabs();
                allTokens.append(separator);
                return this;
            }

            @Override
            public TokenWriter writeOperator(String operator) {
                checkRepeatingOfTokens("writeOperator");
                checkTokenWhitespace(operator, false);
                assertTrue("Unexpected operator: " + operator, operators.contains(operator));
                handleTabs();
                allTokens.append(operator);
                return this;
            }

            @Override
            public TokenWriter writeLiteral(String literal) {
                checkRepeatingOfTokens("writeLiteral");
                assertTrue(literal.length() > 0);
                handleTabs();
                allTokens.append(literal);
                return this;
            }

            @Override
            public TokenWriter writeKeyword(String keyword) {
                checkRepeatingOfTokens("writeKeyword");
                checkTokenWhitespace(keyword, false);
                assertTrue("Unexpected java keyword: " + keyword, javaKeywords.contains(keyword));
                handleTabs();
                allTokens.append(keyword);
                return this;
            }

            @Override
            public TokenWriter writeIdentifier(String identifier) {
                checkRepeatingOfTokens("writeIdentifier");
                checkTokenWhitespace(identifier, false);
                for (int i = 0; i < identifier.length(); i++) {
                    char c = identifier.charAt(i);
                    if (i == 0) {
                        assertTrue(Character.isJavaIdentifierStart(c));
                    } else {
                        assertTrue(Character.isJavaIdentifierPart(c));
                    }
                }
                assertTrue("Keyword found in Identifier: " + identifier, javaKeywords.contains(identifier) == false);
                handleTabs();
                allTokens.append(identifier);
                return this;
            }

            @Override
            public TokenWriter writeComment(CtComment comment) {
                checkRepeatingOfTokens("writeComment");
                DefaultTokenWriter sptw = new DefaultTokenWriter(new PrinterHelper(factory.getEnvironment()));
                PrinterHelper ph = sptw.getPrinterHelper();
                ph.setLineSeparator(getPrinterHelper().getLineSeparator());
                ph.setTabCount(getPrinterHelper().getTabCount());
                sptw.writeComment(comment);
                handleTabs();
                allTokens.append(sptw.getPrinterHelper().toString());
                return this;
            }

            @Override
            public TokenWriter writeln() {
                checkRepeatingOfTokens("writeln");
                allTokens.append(getPrinterHelper().getLineSeparator());
                lastTokenWasEOL = true;
                return this;
            }

            private boolean lastTokenWasEOL = true;

            private int tabCount = 0;

            public TokenWriter handleTabs() {
                if (lastTokenWasEOL) {
                    lastTokenWasEOL = false;
                    for (int i = 0; i < tabCount; i++) {
                        if (factory.getEnvironment().isUsingTabulations()) {
                            allTokens.append('\t');
                        } else {
                            for (int j = 0; j < factory.getEnvironment().getTabulationSize(); j++) {
                                allTokens.append(' ');
                            }
                        }
                    }
                }
                return this;
            }

            @Override
            public TokenWriter writeCodeSnippet(String token) {
                checkRepeatingOfTokens("writeCodeSnippet");
                assertTrue(token.length() > 0);
                handleTabs();
                allTokens.append(token);
                return this;
            }

            @Override
            public TokenWriter incTab() {
                tabCount++;
                return this;
            }

            @Override
            public TokenWriter decTab() {
                tabCount--;
                return this;
            }

            @Override
            public PrinterHelper getPrinterHelper() {
                return printerHelper;
            }

            @Override
            public void reset() {
                printerHelper.reset();
            }

            @Override
            public TokenWriter writeSpace() {
                checkRepeatingOfTokens("writeWhitespace");
                allTokens.append(' ');
                return this;
            }

            // checks that token types are changing. There must be no two tokens of the same type in queue
            private void checkRepeatingOfTokens(String tokenType) {
                if ("writeln".equals(tokenType) || "writeIdentifier".equals(tokenType) || "writeSeparator".equals(tokenType) || "writeWhitespace".equals(tokenType)) {
                // nothing
                } else {
                    // check only other tokens then writeln, which is the only one which can repeat
                    assertTrue("Two tokens of same type current:" + tokenType + " " + allTokens.toString(), tokenType.equals(this.lastToken) == false);
                }
                this.lastToken = tokenType;
            }
        });
        // 2) print type using PrettyPrinter with listener
        pp.calculate(t.getPosition().getCompilationUnit(), Collections.singletonList(t));
        String withEmptyListenerResult = pp.getResult();
        // contract: each printed character is handled by listener. PrinterHelper is not called directly
        // and because PrinterTokenListener above does not use PrinterHelper, the result must be empty
        assertEquals(0, withEmptyListenerResult.length());
        // contract: result built manually from tokens is same like the one made by DefaultTokenWriter
        assertEquals(standardPrintedResult, allTokens.toString());
    }
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultTokenWriter(spoon.reflect.visitor.DefaultTokenWriter) TokenWriter(spoon.reflect.visitor.TokenWriter) Factory(spoon.reflect.factory.Factory) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) ElementPrinterHelper(spoon.reflect.visitor.ElementPrinterHelper) PrinterHelper(spoon.reflect.visitor.PrinterHelper) Launcher(spoon.Launcher) DefaultTokenWriter(spoon.reflect.visitor.DefaultTokenWriter) Test(org.junit.Test)

Aggregations

CtComment (spoon.reflect.code.CtComment)24 Test (org.junit.Test)12 Factory (spoon.reflect.factory.Factory)12 DefaultCoreFactory (spoon.support.DefaultCoreFactory)8 CtClass (spoon.reflect.declaration.CtClass)7 ArrayList (java.util.ArrayList)5 Launcher (spoon.Launcher)4 CtInvocation (spoon.reflect.code.CtInvocation)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 List (java.util.List)3 CtConstructorCall (spoon.reflect.code.CtConstructorCall)3 CtExpression (spoon.reflect.code.CtExpression)3 CtIf (spoon.reflect.code.CtIf)3 CtParameter (spoon.reflect.declaration.CtParameter)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 CtAssignment (spoon.reflect.code.CtAssignment)2 CtBlock (spoon.reflect.code.CtBlock)2