Search in sources :

Example 1 with CtJavaDoc

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

the class JDTCommentBuilder method defineCommentContent.

/**
 * Define the content of the comment
 * @param comment the comment
 * @param tagContent the tagContent of the tag
 * @param tagType the tag type
 */
private void defineCommentContent(CtComment comment, String tagContent, CtJavaDocTag.TagType tagType) {
    if (tagType != null) {
        CtJavaDocTag docTag = comment.getFactory().Code().createJavaDocTag(tagContent, tagType);
        ((CtJavaDoc) comment).addTag(docTag);
    } else if (!tagContent.isEmpty()) {
        comment.setContent(tagContent.trim());
    }
}
Also used : CtJavaDoc(spoon.reflect.code.CtJavaDoc) CtJavaDocTag(spoon.reflect.code.CtJavaDocTag)

Example 2 with CtJavaDoc

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

the class DefaultCoreFactory method createJavaDoc.

@Override
public CtJavaDoc createJavaDoc() {
    CtJavaDoc e = new CtJavaDocImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtJavaDoc(spoon.reflect.code.CtJavaDoc) CtJavaDocImpl(spoon.support.reflect.code.CtJavaDocImpl)

Example 3 with CtJavaDoc

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

the class CommentTest method testJavaDocEmptyCommentAndTag.

@Test
public void testJavaDocEmptyCommentAndTag() {
    // the sources are checked out with \n even on Windows.
    String EOL = "\n";
    Factory f = getSpoonFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(JavaDocEmptyCommentAndTags.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: content is never null
    assertNotNull(classJavaDoc.getContent());
    // contract: empty content is ""
    assertEquals("", classJavaDoc.getContent());
    CtJavaDoc methodJavaDoc = (CtJavaDoc) type.getMethodsByName("m").get(0).getComments().get(1);
    // contract: content is never null
    assertNotNull(methodJavaDoc.getContent());
    // contract: empty content is ""
    assertEquals("", methodJavaDoc.getContent());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtJavaDoc(spoon.reflect.code.CtJavaDoc) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) JavaDocEmptyCommentAndTags(spoon.test.comment.testclasses.JavaDocEmptyCommentAndTags) Test(org.junit.Test)

Example 4 with CtJavaDoc

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

the class CommentTest method testCommentsInResourcesWithWindowsEOL.

@Test
public void testCommentsInResourcesWithWindowsEOL() throws IOException {
    // contract: the WindowsEOL.java contains MS Windows \r\n as EOL
    try (InputStream is = new FileInputStream(new File("./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java"))) {
        int b;
        boolean lastWasCR = false;
        while ((b = is.read()) != -1) {
            if (lastWasCR) {
                // next must be LF
                assertTrue(b == '\n');
                lastWasCR = false;
            }
            if (b == '\r') {
                lastWasCR = true;
            }
        }
    }
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/comment/testclasses/WindowsEOL.java", "-o", "./target/spooned/", "-c" });
    Factory f = launcher.getFactory();
    CtClass<?> type = (CtClass<?>) f.Type().get(WindowsEOL.class);
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: test that java doc is printed correctly
    String str = classJavaDoc.toString();
    StringTokenizer st = new StringTokenizer(str, System.getProperty("line.separator"));
    boolean first = true;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (first) {
            // first
            first = false;
            assertTrue(line.length() == 3);
            assertEquals("/**", line);
        } else {
            if (st.hasMoreTokens()) {
                // in the middle
                assertTrue(line.length() >= 2);
                assertEquals(" *", line.substring(0, 2));
            } else {
                // last
                assertTrue(line.length() == 3);
                assertEquals(" */", line.substring(0, 3));
            }
        }
    }
    // This test passes on MS Windows too - why spoon uses `\n` on MS Windows too?
    assertEquals("This file contains MS Windows EOL.\n" + "It is here to test whether comments are printed well\n" + "in this case", classJavaDoc.getContent());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FileInputStream(java.io.FileInputStream) CtClass(spoon.reflect.declaration.CtClass) StringTokenizer(java.util.StringTokenizer) CtJavaDoc(spoon.reflect.code.CtJavaDoc) Launcher(spoon.Launcher) WindowsEOL(spoon.test.comment.testclasses.WindowsEOL) File(java.io.File) Test(org.junit.Test)

Example 5 with CtJavaDoc

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

the class CommentTest method testJavaDocComment.

private void testJavaDocComment(CtClass type, String EOL) {
    CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
    // contract: test that java doc is printed correctly
    String str = classJavaDoc.toString();
    StringTokenizer st = new StringTokenizer(str, System.getProperty("line.separator"));
    boolean first = true;
    while (st.hasMoreTokens()) {
        String line = st.nextToken();
        if (first) {
            // first
            first = false;
            assertTrue(line.length() == 3);
            assertEquals("/**", line);
        } else {
            if (st.hasMoreTokens()) {
                // in the middle
                assertTrue(line.length() >= 2);
                assertEquals(" *", line.substring(0, 2));
            } else {
                // last
                assertTrue(line.length() == 3);
                assertEquals(" */", line.substring(0, 3));
            }
        }
    }
    assertEquals("JavaDoc test class." + EOL + EOL + "Long description", classJavaDoc.getContent());
    List<CtJavaDocTag> elements = type.getElements(new TypeFilter<>(CtJavaDocTag.class));
    assertEquals(8, elements.size());
    List<CtJavaDocTag> authorTags = getTagByType(elements, CtJavaDocTag.TagType.AUTHOR);
    assertEquals(1, authorTags.size());
    assertEquals("Thomas Durieux", authorTags.get(0).getContent());
    List<CtJavaDocTag> deprecatedTags = getTagByType(elements, CtJavaDocTag.TagType.DEPRECATED);
    assertEquals(1, deprecatedTags.size());
    assertEquals("", deprecatedTags.get(0).getContent());
    List<CtJavaDocTag> sinceTags = getTagByType(elements, CtJavaDocTag.TagType.SINCE);
    assertEquals(2, sinceTags.size());
    assertEquals("1.3", sinceTags.get(0).getContent());
    assertEquals("1.3", sinceTags.get(1).getContent());
    List<CtJavaDocTag> paramTags = getTagByType(elements, CtJavaDocTag.TagType.PARAM);
    assertEquals(1, paramTags.size());
    assertEquals("the parameters", paramTags.get(0).getContent());
    assertEquals("i", paramTags.get(0).getParam());
    CtJavaDocTag tagClone = paramTags.get(0).clone();
    assertEquals("the parameters", tagClone.getContent());
    assertEquals("i", tagClone.getParam());
    List<CtJavaDocTag> throwsTags = getTagByType(elements, CtJavaDocTag.TagType.THROWS);
    assertEquals(1, throwsTags.size());
    assertEquals("an exception", throwsTags.get(0).getContent());
    assertEquals("RuntimeException", throwsTags.get(0).getParam());
    assertEquals("JavaDoc test class.", classJavaDoc.getShortDescription());
    assertEquals("Long description", classJavaDoc.getLongDescription());
    CtJavaDocTag deprecatedTag = classJavaDoc.getTags().get(0);
    assertTrue(classJavaDoc.toString().indexOf("@deprecated") >= 0);
    classJavaDoc.removeTag(0);
    assertEquals(-1, classJavaDoc.toString().indexOf("@deprecated"));
    classJavaDoc.addTag(deprecatedTag);
    assertTrue(classJavaDoc.toString().indexOf("@deprecated") >= 0);
}
Also used : StringTokenizer(java.util.StringTokenizer) CtJavaDoc(spoon.reflect.code.CtJavaDoc) CtJavaDocTag(spoon.reflect.code.CtJavaDocTag)

Aggregations

CtJavaDoc (spoon.reflect.code.CtJavaDoc)7 Test (org.junit.Test)3 CtJavaDocTag (spoon.reflect.code.CtJavaDocTag)3 CtClass (spoon.reflect.declaration.CtClass)3 Factory (spoon.reflect.factory.Factory)3 DefaultCoreFactory (spoon.support.DefaultCoreFactory)3 StringTokenizer (java.util.StringTokenizer)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Launcher (spoon.Launcher)1 CtComment (spoon.reflect.code.CtComment)1 CtJavaDocImpl (spoon.support.reflect.code.CtJavaDocImpl)1 JavaDocEmptyCommentAndTags (spoon.test.comment.testclasses.JavaDocEmptyCommentAndTags)1 OtherJavaDoc (spoon.test.comment.testclasses.OtherJavaDoc)1 WindowsEOL (spoon.test.comment.testclasses.WindowsEOL)1